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) |
36 , m_firstLetter(nullptr) | 39 , m_isRemainingTextRenderer(false) |
| 40 , m_firstLetterPseudoElement(nullptr) |
37 { | 41 { |
38 } | 42 } |
39 | 43 |
40 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) | 44 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) |
41 : RenderText(node, str) | 45 : RenderText(node, str) |
42 , m_start(0) | 46 , m_start(0) |
43 , m_end(str ? str->length() : 0) | 47 , m_end(str ? str->length() : 0) |
| 48 , m_isRemainingTextRenderer(false) |
44 , m_contentString(str) | 49 , m_contentString(str) |
45 , m_firstLetter(nullptr) | 50 , m_firstLetterPseudoElement(nullptr) |
46 { | 51 { |
47 } | 52 } |
48 | 53 |
49 RenderTextFragment::~RenderTextFragment() | 54 RenderTextFragment::~RenderTextFragment() |
50 { | 55 { |
| 56 if (m_isRemainingTextRenderer && m_firstLetterPseudoElement) |
| 57 m_firstLetterPseudoElement->setRemainingTextRenderer(nullptr); |
| 58 m_firstLetterPseudoElement = nullptr; |
51 } | 59 } |
52 | 60 |
53 void RenderTextFragment::trace(Visitor* visitor) | 61 void RenderTextFragment::trace(Visitor* visitor) |
54 { | 62 { |
55 visitor->trace(m_firstLetter); | 63 visitor->trace(m_firstLetterPseudoElement); |
56 RenderText::trace(visitor); | 64 RenderText::trace(visitor); |
57 } | 65 } |
58 | 66 |
59 RenderText* RenderTextFragment::firstRenderTextInFirstLetter() const | 67 PassRefPtr<StringImpl> RenderTextFragment::completeText() const |
60 { | 68 { |
61 for (RenderObject* current = m_firstLetter; current; current = current->next
InPreOrder(m_firstLetter)) { | 69 Text* text = associatedTextNode(); |
62 if (current->isText()) | 70 return text ? text->dataImpl() : contentString(); |
63 return toRenderText(current); | |
64 } | |
65 return 0; | |
66 } | 71 } |
67 | 72 |
68 PassRefPtr<StringImpl> RenderTextFragment::originalText() const | 73 PassRefPtr<StringImpl> RenderTextFragment::originalText() const |
69 { | 74 { |
70 Node* e = node(); | 75 RefPtr<StringImpl> result = completeText(); |
71 RefPtr<StringImpl> result = ((e && e->isTextNode()) ? toText(e)->dataImpl()
: contentString()); | |
72 if (!result) | 76 if (!result) |
73 return nullptr; | 77 return nullptr; |
74 return result->substring(start(), end()); | 78 return result->substring(start(), end()); |
75 } | 79 } |
76 | 80 |
77 void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle*
oldStyle) | |
78 { | |
79 RenderText::styleDidChange(diff, oldStyle); | |
80 | |
81 if (RenderBlock* block = blockForAccompanyingFirstLetter()) { | |
82 block->style()->removeCachedPseudoStyle(FIRST_LETTER); | |
83 block->updateFirstLetter(); | |
84 } | |
85 } | |
86 | |
87 void RenderTextFragment::willBeDestroyed() | |
88 { | |
89 if (m_firstLetter) | |
90 m_firstLetter->destroy(); | |
91 RenderText::willBeDestroyed(); | |
92 } | |
93 | |
94 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) | 81 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) |
95 { | 82 { |
96 RenderText::setText(text, force); | 83 RenderText::setText(text, force); |
97 | 84 |
98 m_start = 0; | 85 m_start = 0; |
99 m_end = textLength(); | 86 m_end = textLength(); |
100 if (m_firstLetter) { | |
101 // FIXME: We should not modify the structure of the render tree during | |
102 // layout. crbug.com/370458 | |
103 DeprecatedDisableModifyRenderTreeStructureAsserts disabler; | |
104 | 87 |
105 ASSERT(!m_contentString); | 88 // If we're the remaining text from a first letter then we have to tell the |
106 m_firstLetter->destroy(); | 89 // first letter pseudo element to reattach itself so it can re-calculate the |
107 m_firstLetter = nullptr; | 90 // correct first-letter settings. |
108 if (Node* t = node()) { | 91 if (RenderObject* previous = previousSibling()) { |
109 ASSERT(!t->renderer()); | 92 if (!previous->isPseudoElement() || !previous->node()->isFirstLetterPseu
doElement()) |
110 t->setRenderer(this); | 93 return; |
111 } | 94 |
| 95 // Tell the first letter container node, and the first-letter node |
| 96 // that their style may have changed. |
| 97 // e.g. fast/css/first-letter-detach.html |
| 98 toFirstLetterPseudoElement(previous->node())->setNeedsUpdate(); |
112 } | 99 } |
113 } | 100 } |
114 | 101 |
115 void RenderTextFragment::transformText() | 102 void RenderTextFragment::transformText() |
116 { | 103 { |
117 // Don't reset first-letter here because we are only transforming the trunca
ted fragment. | 104 // Note, we have to call RenderText::setText here because, if we use our |
| 105 // version we will, potentially, screw up the first-letter settings where |
| 106 // we only use portions of the string. |
118 if (RefPtr<StringImpl> textToTransform = originalText()) | 107 if (RefPtr<StringImpl> textToTransform = originalText()) |
119 RenderText::setText(textToTransform.release(), true); | 108 RenderText::setText(textToTransform.release(), true); |
120 } | 109 } |
121 | 110 |
122 UChar RenderTextFragment::previousCharacter() const | 111 UChar RenderTextFragment::previousCharacter() const |
123 { | 112 { |
124 if (start()) { | 113 if (start()) { |
125 Node* e = node(); | 114 StringImpl* original = completeText().get(); |
126 StringImpl* original = ((e && e->isTextNode()) ? toText(e)->dataImpl() :
contentString()); | |
127 if (original && start() <= original->length()) | 115 if (original && start() <= original->length()) |
128 return (*original)[start() - 1]; | 116 return (*original)[start() - 1]; |
129 } | 117 } |
130 | 118 |
131 return RenderText::previousCharacter(); | 119 return RenderText::previousCharacter(); |
132 } | 120 } |
133 | 121 |
134 RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter() const | 122 // If this is the renderer for a first-letter pseudoNode then we have to look |
| 123 // at the node for the remaining text to find our content. |
| 124 Text* RenderTextFragment::associatedTextNode() const |
135 { | 125 { |
136 if (!m_firstLetter) | 126 Node* node = m_isRemainingTextRenderer ? this->node() : this->firstLetterPse
udoElement(); |
137 return 0; | 127 if (!node) |
138 for (RenderObject* block = m_firstLetter->parent(); block; block = block->pa
rent()) { | 128 return nullptr; |
139 if (block->style()->hasPseudoStyle(FIRST_LETTER) && block->canHaveChildr
en() && block->isRenderBlock()) | 129 |
140 return toRenderBlock(block); | 130 if (node->isFirstLetterPseudoElement()) { |
| 131 FirstLetterPseudoElement* pseudo = toFirstLetterPseudoElement(node); |
| 132 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRe
nderer(*pseudo); |
| 133 if (!nextRenderer) |
| 134 return nullptr; |
| 135 node = nextRenderer->node(); |
141 } | 136 } |
142 return 0; | 137 return (node && node->isTextNode()) ? toText(node) : nullptr; |
143 } | |
144 | |
145 void RenderTextFragment::updateHitTestResult(HitTestResult& result, const Layout
Point& point) | |
146 { | |
147 if (result.innerNode()) | |
148 return; | |
149 | |
150 RenderObject::updateHitTestResult(result, point); | |
151 if (m_firstLetter || !node()) | |
152 return; | |
153 RenderObject* nodeRenderer = node()->renderer(); | |
154 if (!nodeRenderer || !nodeRenderer->isText() || !toRenderText(nodeRenderer)-
>isTextFragment()) | |
155 return; | |
156 | |
157 if (isDescendantOf(toRenderTextFragment(nodeRenderer)->m_firstLetter)) | |
158 result.setIsFirstLetter(true); | |
159 } | 138 } |
160 | 139 |
161 } // namespace blink | 140 } // namespace blink |
OLD | NEW |