OLD | NEW |
1 /* | 1 /* |
2 * (C) 1999 Lars Knoll (knoll@kde.org) | 2 * (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 2000 Dirk Mueller (mueller@kde.org) | 3 * (C) 2000 Dirk Mueller (mueller@kde.org) |
4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. | 4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
11 * This library is distributed in the hope that it will be useful, | 11 * This library is distributed in the hope that it will be useful, |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 * Library General Public License for more details. | 14 * Library General Public License for more details. |
15 * | 15 * |
16 * You should have received a copy of the GNU Library General Public License | 16 * You should have received a copy of the GNU Library General Public License |
17 * along with this library; see the file COPYING.LIB. If not, write to | 17 * along with this library; see the file COPYING.LIB. If not, write to |
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 * Boston, MA 02110-1301, USA. | 19 * Boston, MA 02110-1301, USA. |
20 * | 20 * |
21 */ | 21 */ |
22 | 22 |
23 #include "config.h" | 23 #include "config.h" |
24 #include "core/rendering/RenderTextFragment.h" | 24 #include "core/rendering/RenderTextFragment.h" |
25 | 25 |
| 26 #include "core/dom/FirstLetterPseudoElement.h" |
| 27 #include "core/dom/PseudoElement.h" |
| 28 #include "core/dom/StyleChangeReason.h" |
26 #include "core/dom/Text.h" | 29 #include "core/dom/Text.h" |
27 #include "core/rendering/HitTestResult.h" | 30 #include "core/rendering/HitTestResult.h" |
28 #include "core/rendering/RenderBlock.h" | 31 #include "core/rendering/RenderBlock.h" |
29 | 32 |
30 namespace blink { | 33 namespace blink { |
31 | 34 |
32 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str, int startOff
set, int length) | 35 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str, int startOff
set, int length) |
33 : RenderText(node, str ? str->substring(startOffset, length) : PassRefPtr<St
ringImpl>(nullptr)) | 36 : RenderText(node, str ? str->substring(startOffset, length) : PassRefPtr<St
ringImpl>(nullptr)) |
34 , m_start(startOffset) | 37 , m_start(startOffset) |
35 , m_end(length) | 38 , m_end(length) |
| 39 , m_isRemainingTextRenderer(false) |
36 , m_contentString(str) | 40 , m_contentString(str) |
37 , m_firstLetter(nullptr) | 41 , m_firstLetterPseudoElement(nullptr) |
38 { | 42 { |
39 } | 43 } |
40 | 44 |
41 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) | 45 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) |
42 : RenderText(node, str) | 46 : RenderText(node, str) |
43 , m_start(0) | 47 , m_start(0) |
44 , m_end(str ? str->length() : 0) | 48 , m_end(str ? str->length() : 0) |
| 49 , m_isRemainingTextRenderer(false) |
45 , m_contentString(str) | 50 , m_contentString(str) |
46 , m_firstLetter(nullptr) | 51 , m_firstLetterPseudoElement(nullptr) |
47 { | 52 { |
48 } | 53 } |
49 | 54 |
50 RenderTextFragment::~RenderTextFragment() | 55 RenderTextFragment::~RenderTextFragment() |
51 { | 56 { |
| 57 ASSERT(!m_firstLetterPseudoElement); |
| 58 } |
| 59 |
| 60 void RenderTextFragment::destroy() |
| 61 { |
| 62 if (m_isRemainingTextRenderer && m_firstLetterPseudoElement) |
| 63 m_firstLetterPseudoElement->setRemainingTextRenderer(nullptr); |
| 64 m_firstLetterPseudoElement = nullptr; |
| 65 RenderText::destroy(); |
52 } | 66 } |
53 | 67 |
54 void RenderTextFragment::trace(Visitor* visitor) | 68 void RenderTextFragment::trace(Visitor* visitor) |
55 { | 69 { |
56 visitor->trace(m_firstLetter); | 70 visitor->trace(m_firstLetterPseudoElement); |
57 RenderText::trace(visitor); | 71 RenderText::trace(visitor); |
58 } | 72 } |
59 | 73 |
60 RenderText* RenderTextFragment::firstRenderTextInFirstLetter() const | 74 PassRefPtr<StringImpl> RenderTextFragment::completeText() const |
61 { | 75 { |
62 for (RenderObject* current = m_firstLetter; current; current = current->next
InPreOrder(m_firstLetter)) { | 76 Text* text = associatedTextNode(); |
63 if (current->isText()) | 77 return text ? text->dataImpl() : contentString(); |
64 return toRenderText(current); | |
65 } | |
66 return 0; | |
67 } | 78 } |
68 | 79 |
69 void RenderTextFragment::setContentString(StringImpl* str) | 80 void RenderTextFragment::setContentString(StringImpl* str) |
70 { | 81 { |
71 m_contentString = str; | 82 m_contentString = str; |
72 setText(str); | 83 setText(str); |
73 } | 84 } |
74 | 85 |
75 PassRefPtr<StringImpl> RenderTextFragment::originalText() const | 86 PassRefPtr<StringImpl> RenderTextFragment::originalText() const |
76 { | 87 { |
77 RefPtr<StringImpl> result = RenderText::originalText(); | 88 RefPtr<StringImpl> result = completeText(); |
78 if (!result) | |
79 result = m_contentString; | |
80 if (!result) | 89 if (!result) |
81 return nullptr; | 90 return nullptr; |
82 return result->substring(start(), end()); | 91 return result->substring(start(), end()); |
83 } | 92 } |
84 | 93 |
85 void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle*
oldStyle) | |
86 { | |
87 RenderText::styleDidChange(diff, oldStyle); | |
88 | |
89 if (RenderBlock* block = blockForAccompanyingFirstLetter()) { | |
90 block->style()->removeCachedPseudoStyle(FIRST_LETTER); | |
91 block->updateFirstLetter(); | |
92 } | |
93 } | |
94 | |
95 void RenderTextFragment::willBeDestroyed() | |
96 { | |
97 if (m_firstLetter) | |
98 m_firstLetter->destroy(); | |
99 RenderText::willBeDestroyed(); | |
100 } | |
101 | |
102 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) | 94 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) |
103 { | 95 { |
104 RenderText::setText(text, force); | 96 RenderText::setText(text, force); |
105 | 97 |
106 m_start = 0; | 98 m_start = 0; |
107 m_end = textLength(); | 99 m_end = textLength(); |
108 if (m_firstLetter) { | |
109 // FIXME: We should not modify the structure of the render tree during | |
110 // layout. crbug.com/370458 | |
111 DeprecatedDisableModifyRenderTreeStructureAsserts disabler; | |
112 | 100 |
113 m_firstLetter->destroy(); | 101 // If we're the remaining text from a first letter then we have to tell the |
114 m_firstLetter = nullptr; | 102 // first letter pseudo element to reattach itself so it can re-calculate the |
115 if (Node* t = node()) { | 103 // correct first-letter settings. |
116 ASSERT(!t->renderer()); | 104 if (RenderObject* previous = previousSibling()) { |
117 t->setRenderer(this); | 105 if (!previous->isPseudoElement() || !previous->node()->isFirstLetterPseu
doElement()) |
118 } | 106 return; |
| 107 |
| 108 // Tell the first letter container node, and the first-letter node |
| 109 // that their style may have changed. |
| 110 // e.g. fast/css/first-letter-detach.html |
| 111 toFirstLetterPseudoElement(previous->node())->setNeedsUpdate(); |
119 } | 112 } |
120 } | 113 } |
121 | 114 |
122 void RenderTextFragment::transformText() | 115 void RenderTextFragment::transformText() |
123 { | 116 { |
124 // Don't reset first-letter here because we are only transforming the trunca
ted fragment. | 117 // Note, we have to call RenderText::setText here because, if we use our |
| 118 // version we will, potentially, screw up the first-letter settings where |
| 119 // we only use portions of the string. |
125 if (RefPtr<StringImpl> textToTransform = originalText()) | 120 if (RefPtr<StringImpl> textToTransform = originalText()) |
126 RenderText::setText(textToTransform.release(), true); | 121 RenderText::setText(textToTransform.release(), true); |
127 } | 122 } |
128 | 123 |
129 UChar RenderTextFragment::previousCharacter() const | 124 UChar RenderTextFragment::previousCharacter() const |
130 { | 125 { |
131 if (start()) { | 126 if (start()) { |
132 RefPtr<StringImpl> original = RenderText::originalText(); | 127 StringImpl* original = completeText().get(); |
133 if (!original) | |
134 original = m_contentString; | |
135 if (original && start() <= original->length()) | 128 if (original && start() <= original->length()) |
136 return (*original)[start() - 1]; | 129 return (*original)[start() - 1]; |
137 } | 130 } |
138 | 131 |
139 return RenderText::previousCharacter(); | 132 return RenderText::previousCharacter(); |
140 } | 133 } |
141 | 134 |
142 RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter() const | 135 // If this is the renderer for a first-letter pseudoNode then we have to look |
| 136 // at the node for the remaining text to find our content. |
| 137 Text* RenderTextFragment::associatedTextNode() const |
143 { | 138 { |
144 if (!m_firstLetter) | 139 Node* node = m_isRemainingTextRenderer ? this->node() : this->firstLetterPse
udoElement(); |
145 return 0; | 140 if (!node) |
146 for (RenderObject* block = m_firstLetter->parent(); block; block = block->pa
rent()) { | 141 return nullptr; |
147 if (block->style()->hasPseudoStyle(FIRST_LETTER) && block->canHaveChildr
en() && block->isRenderBlock()) | 142 |
148 return toRenderBlock(block); | 143 if (node->isFirstLetterPseudoElement()) { |
| 144 FirstLetterPseudoElement* pseudo = toFirstLetterPseudoElement(node); |
| 145 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRe
nderer(*pseudo); |
| 146 if (!nextRenderer) |
| 147 return nullptr; |
| 148 node = nextRenderer->node(); |
149 } | 149 } |
150 return 0; | 150 return (node && node->isTextNode()) ? toText(node) : nullptr; |
151 } | |
152 | |
153 void RenderTextFragment::updateHitTestResult(HitTestResult& result, const Layout
Point& point) | |
154 { | |
155 if (result.innerNode()) | |
156 return; | |
157 | |
158 RenderObject::updateHitTestResult(result, point); | |
159 if (m_firstLetter || !node()) | |
160 return; | |
161 RenderObject* nodeRenderer = node()->renderer(); | |
162 if (!nodeRenderer || !nodeRenderer->isText() || !toRenderText(nodeRenderer)-
>isTextFragment()) | |
163 return; | |
164 | |
165 if (isDescendantOf(toRenderTextFragment(nodeRenderer)->m_firstLetter)) | |
166 result.setIsFirstLetter(true); | |
167 } | 151 } |
168 | 152 |
169 } // namespace blink | 153 } // namespace blink |
OLD | NEW |