Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: Source/core/editing/StyledMarkupSerializer.cpp

Issue 1188693005: Introduce StyledMarkupTraverser to StyledMarkupSerializer (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Renamed StyledMarkupTraverser Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/editing/StyledMarkupSerializer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
3 * Copyright (C) 2008, 2009, 2010, 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2008, 2009, 2010, 2011 Google Inc. All rights reserved.
4 * Copyright (C) 2011 Igalia S.L. 4 * Copyright (C) 2011 Igalia S.L.
5 * Copyright (C) 2011 Motorola Mobility. All rights reserved. 5 * Copyright (C) 2011 Motorola Mobility. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 if (!position.containerNode()->isTextNode()) 56 if (!position.containerNode()->isTextNode())
57 return TextOffset(); 57 return TextOffset();
58 58
59 return TextOffset(toText(position.containerNode()), position.offsetInContain erNode()); 59 return TextOffset(toText(position.containerNode()), position.offsetInContain erNode());
60 } 60 }
61 61
62 } // namespace 62 } // namespace
63 63
64 using namespace HTMLNames; 64 using namespace HTMLNames;
65 65
66 template<typename Strategy>
67 class StyledMarkupTraverser {
68 WTF_MAKE_NONCOPYABLE(StyledMarkupTraverser);
69 STACK_ALLOCATED();
70 public:
71 StyledMarkupTraverser();
72 StyledMarkupTraverser(StyledMarkupAccumulator*, Node*);
73
74 Node* traverseNodesForSerialization(Node*, Node*);
75 void wrapWithNode(ContainerNode&, PassRefPtrWillBeRawPtr<EditingStyle>);
76 RefPtrWillBeRawPtr<EditingStyle> createInlineStyleIfNeeded(Node&);
77
78 private:
79 bool shouldAnnotate() const;
80 bool convertBlocksToInlines() const;
81 void appendStartMarkup(Node&);
82 void appendEndMarkup(Node&);
83 RefPtrWillBeRawPtr<EditingStyle> createInlineStyle(Element&);
84 bool needsInlineStyle(const Element&);
85 bool shouldApplyWrappingStyle(const Node&) const;
86
87 StyledMarkupAccumulator* m_accumulator;
88 RefPtrWillBeMember<Node> m_lastClosed;
89 RefPtrWillBeMember<EditingStyle> m_wrappingStyle;
90 };
91
66 static Position toPositionInDOMTree(const Position& position) 92 static Position toPositionInDOMTree(const Position& position)
67 { 93 {
68 return position; 94 return position;
69 } 95 }
70 96
71 template<typename Strategy> 97 template<typename Strategy>
98 bool StyledMarkupTraverser<Strategy>::shouldAnnotate() const
99 {
100 return m_accumulator->shouldAnnotate();
101 }
102
103 template<typename Strategy>
104 bool StyledMarkupTraverser<Strategy>::convertBlocksToInlines() const
105 {
106 return m_accumulator->convertBlocksToInlines();
107 }
108
109 template<typename Strategy>
72 StyledMarkupSerializer<Strategy>::StyledMarkupSerializer(EAbsoluteURLs shouldRes olveURLs, EAnnotateForInterchange shouldAnnotate, const PositionType& start, con st PositionType& end, Node* highestNodeToBeSerialized, ConvertBlocksToInlines co nvertBlocksToInlines) 110 StyledMarkupSerializer<Strategy>::StyledMarkupSerializer(EAbsoluteURLs shouldRes olveURLs, EAnnotateForInterchange shouldAnnotate, const PositionType& start, con st PositionType& end, Node* highestNodeToBeSerialized, ConvertBlocksToInlines co nvertBlocksToInlines)
73 : m_start(start) 111 : m_start(start)
74 , m_end(end) 112 , m_end(end)
75 , m_shouldResolveURLs(shouldResolveURLs) 113 , m_shouldResolveURLs(shouldResolveURLs)
76 , m_shouldAnnotate(shouldAnnotate) 114 , m_shouldAnnotate(shouldAnnotate)
77 , m_highestNodeToBeSerialized(highestNodeToBeSerialized) 115 , m_highestNodeToBeSerialized(highestNodeToBeSerialized)
78 , m_convertBlocksToInlines(convertBlocksToInlines) 116 , m_convertBlocksToInlines(convertBlocksToInlines)
79 , m_lastClosed(highestNodeToBeSerialized) 117 , m_lastClosed(highestNodeToBeSerialized)
80 { 118 {
81 } 119 }
(...skipping 29 matching lines...) Expand all
111 RefPtrWillBeRawPtr<EditingStyle> style = EditingStyle::create(element->inlin eStyle()); 149 RefPtrWillBeRawPtr<EditingStyle> style = EditingStyle::create(element->inlin eStyle());
112 // FIXME: Having to const_cast here is ugly, but it is quite a bit of work t o untangle 150 // FIXME: Having to const_cast here is ugly, but it is quite a bit of work t o untangle
113 // the non-const-ness of styleFromMatchedRulesForElement. 151 // the non-const-ness of styleFromMatchedRulesForElement.
114 style->mergeStyleFromRules(const_cast<HTMLElement*>(element)); 152 style->mergeStyleFromRules(const_cast<HTMLElement*>(element));
115 return style.release(); 153 return style.release();
116 } 154 }
117 155
118 template<typename Strategy> 156 template<typename Strategy>
119 String StyledMarkupSerializer<Strategy>::createMarkup() 157 String StyledMarkupSerializer<Strategy>::createMarkup()
120 { 158 {
121 StyledMarkupAccumulator markupAccumulator(m_shouldResolveURLs, toTextOffset( m_start.parentAnchoredEquivalent()), toTextOffset(m_end.parentAnchoredEquivalent ()), m_start.document(), m_shouldAnnotate); 159 StyledMarkupAccumulator markupAccumulator(m_shouldResolveURLs, toTextOffset( m_start.parentAnchoredEquivalent()), toTextOffset(m_end.parentAnchoredEquivalent ()), m_start.document(), m_shouldAnnotate, m_convertBlocksToInlines);
122 160
123 Node* pastEnd = m_end.nodeAsRangePastLastNode(); 161 Node* pastEnd = m_end.nodeAsRangePastLastNode();
124 162
125 Node* firstNode = m_start.nodeAsRangeFirstNode(); 163 Node* firstNode = m_start.nodeAsRangeFirstNode();
126 VisiblePosition visibleStart(toPositionInDOMTree(m_start), VP_DEFAULT_AFFINI TY); 164 VisiblePosition visibleStart(toPositionInDOMTree(m_start), VP_DEFAULT_AFFINI TY);
127 VisiblePosition visibleEnd(toPositionInDOMTree(m_end), VP_DEFAULT_AFFINITY); 165 VisiblePosition visibleEnd(toPositionInDOMTree(m_end), VP_DEFAULT_AFFINITY);
128 if (shouldAnnotate() && needInterchangeNewlineAfter(visibleStart)) { 166 if (shouldAnnotate() && needInterchangeNewlineAfter(visibleStart)) {
129 markupAccumulator.appendInterchangeNewline(); 167 markupAccumulator.appendInterchangeNewline();
130 if (visibleStart == visibleEnd.previous()) 168 if (visibleStart == visibleEnd.previous())
131 return markupAccumulator.takeResults(); 169 return markupAccumulator.takeResults();
132 170
133 firstNode = visibleStart.next().deepEquivalent().deprecatedNode(); 171 firstNode = visibleStart.next().deepEquivalent().deprecatedNode();
134 172
135 if (pastEnd && Strategy::PositionType::beforeNode(firstNode).compareTo(S trategy::PositionType::beforeNode(pastEnd)) >= 0) { 173 if (pastEnd && Strategy::PositionType::beforeNode(firstNode).compareTo(S trategy::PositionType::beforeNode(pastEnd)) >= 0) {
136 // This condition hits in editing/pasteboard/copy-display-none.html. 174 // This condition hits in editing/pasteboard/copy-display-none.html.
137 return markupAccumulator.takeResults(); 175 return markupAccumulator.takeResults();
138 } 176 }
139 } 177 }
140 178
141 Node* lastClosed = serializeNodes(firstNode, pastEnd, &markupAccumulator); 179 if (!m_lastClosed)
180 m_lastClosed = StyledMarkupTraverser<Strategy>().traverseNodesForSeriali zation(firstNode, pastEnd);
181 StyledMarkupTraverser<Strategy> traverser(&markupAccumulator, m_lastClosed);
182 Node* lastClosed = traverser.traverseNodesForSerialization(firstNode, pastEn d);
yosin_UTC9 2015/06/18 05:13:29 s/traverseNodesForSerialization/serializeNodes/ or
hajimehoshi 2015/06/18 05:24:55 Done.
142 183
143 if (m_highestNodeToBeSerialized && lastClosed) { 184 if (m_highestNodeToBeSerialized && lastClosed) {
144 // TODO(hajimehoshi): This is calculated at createMarkupInternal too. 185 // TODO(hajimehoshi): This is calculated at createMarkupInternal too.
145 Node* commonAncestor = Strategy::commonAncestor(*m_start.containerNode() , *m_end.containerNode()); 186 Node* commonAncestor = Strategy::commonAncestor(*m_start.containerNode() , *m_end.containerNode());
146 ASSERT(commonAncestor); 187 ASSERT(commonAncestor);
147 HTMLBodyElement* body = toHTMLBodyElement(enclosingElementWithTag(firstP ositionInNode(commonAncestor), bodyTag)); 188 HTMLBodyElement* body = toHTMLBodyElement(enclosingElementWithTag(firstP ositionInNode(commonAncestor), bodyTag));
148 HTMLBodyElement* fullySelectedRoot = nullptr; 189 HTMLBodyElement* fullySelectedRoot = nullptr;
149 // FIXME: Do this for all fully selected blocks, not just the body. 190 // FIXME: Do this for all fully selected blocks, not just the body.
150 if (body && areSameRanges(body, m_start, m_end)) 191 if (body && areSameRanges(body, m_start, m_end))
151 fullySelectedRoot = body; 192 fullySelectedRoot = body;
152 193
153 // Also include all of the ancestors of lastClosed up to this special an cestor. 194 // Also include all of the ancestors of lastClosed up to this special an cestor.
154 // FIXME: What is ancestor? 195 // FIXME: What is ancestor?
155 for (ContainerNode* ancestor = Strategy::parent(*lastClosed); ancestor; ancestor = Strategy::parent(*ancestor)) { 196 for (ContainerNode* ancestor = Strategy::parent(*lastClosed); ancestor; ancestor = Strategy::parent(*ancestor)) {
156 if (ancestor == fullySelectedRoot && !convertBlocksToInlines()) { 197 if (ancestor == fullySelectedRoot && !markupAccumulator.convertBlock sToInlines()) {
157 RefPtrWillBeRawPtr<EditingStyle> fullySelectedRootStyle = styleF romMatchedRulesAndInlineDecl(fullySelectedRoot); 198 RefPtrWillBeRawPtr<EditingStyle> fullySelectedRootStyle = styleF romMatchedRulesAndInlineDecl(fullySelectedRoot);
158 199
159 // Bring the background attribute over, but not as an attribute because a background attribute on a div 200 // Bring the background attribute over, but not as an attribute because a background attribute on a div
160 // appears to have no effect. 201 // appears to have no effect.
161 if ((!fullySelectedRootStyle || !fullySelectedRootStyle->style() || !fullySelectedRootStyle->style()->getPropertyCSSValue(CSSPropertyBackgroundI mage)) 202 if ((!fullySelectedRootStyle || !fullySelectedRootStyle->style() || !fullySelectedRootStyle->style()->getPropertyCSSValue(CSSPropertyBackgroundI mage))
162 && fullySelectedRoot->hasAttribute(backgroundAttr)) 203 && fullySelectedRoot->hasAttribute(backgroundAttr))
163 fullySelectedRootStyle->style()->setProperty(CSSPropertyBack groundImage, "url('" + fullySelectedRoot->getAttribute(backgroundAttr) + "')"); 204 fullySelectedRootStyle->style()->setProperty(CSSPropertyBack groundImage, "url('" + fullySelectedRoot->getAttribute(backgroundAttr) + "')");
164 205
165 if (fullySelectedRootStyle->style()) { 206 if (fullySelectedRootStyle->style()) {
166 // Reset the CSS properties to avoid an assertion error in a ddStyleMarkup(). 207 // Reset the CSS properties to avoid an assertion error in a ddStyleMarkup().
167 // This assertion is caused at least when we select all text of a <body> element whose 208 // This assertion is caused at least when we select all text of a <body> element whose
168 // 'text-decoration' property is "inherit", and copy it. 209 // 'text-decoration' property is "inherit", and copy it.
169 if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->st yle(), CSSPropertyTextDecoration)) 210 if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->st yle(), CSSPropertyTextDecoration))
170 fullySelectedRootStyle->style()->setProperty(CSSProperty TextDecoration, CSSValueNone); 211 fullySelectedRootStyle->style()->setProperty(CSSProperty TextDecoration, CSSValueNone);
171 if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->st yle(), CSSPropertyWebkitTextDecorationsInEffect)) 212 if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->st yle(), CSSPropertyWebkitTextDecorationsInEffect))
172 fullySelectedRootStyle->style()->setProperty(CSSProperty WebkitTextDecorationsInEffect, CSSValueNone); 213 fullySelectedRootStyle->style()->setProperty(CSSProperty WebkitTextDecorationsInEffect, CSSValueNone);
173 markupAccumulator.wrapWithStyleNode(fullySelectedRootStyle-> style()); 214 markupAccumulator.wrapWithStyleNode(fullySelectedRootStyle-> style());
174 } 215 }
175 } else { 216 } else {
176 RefPtrWillBeRawPtr<EditingStyle> style = createInlineStyleIfNeed ed(*ancestor); 217 RefPtrWillBeRawPtr<EditingStyle> style = traverser.createInlineS tyleIfNeeded(*ancestor);
177 // Since this node and all the other ancestors are not in the se lection we want 218 // Since this node and all the other ancestors are not in the se lection we want
178 // styles that affect the exterior of the node not to be not inc luded. 219 // styles that affect the exterior of the node not to be not inc luded.
179 // If the node is not fully selected by the range, then we don't want to keep styles that affect its relationship to the nodes around it 220 // If the node is not fully selected by the range, then we don't want to keep styles that affect its relationship to the nodes around it
180 // only the ones that affect it and the nodes within it. 221 // only the ones that affect it and the nodes within it.
181 if (style && style->style()) 222 if (style && style->style())
182 style->style()->removeProperty(CSSPropertyFloat); 223 style->style()->removeProperty(CSSPropertyFloat);
183 wrapWithNode(markupAccumulator, *ancestor, style); 224 traverser.wrapWithNode(*ancestor, style);
184 } 225 }
185 226
186 if (ancestor == m_highestNodeToBeSerialized) 227 if (ancestor == m_highestNodeToBeSerialized)
187 break; 228 break;
188 } 229 }
189 } 230 }
190 231
191 // FIXME: The interchange newline should be placed in the block that it's in , not after all of the content, unconditionally. 232 // FIXME: The interchange newline should be placed in the block that it's in , not after all of the content, unconditionally.
192 if (shouldAnnotate() && needInterchangeNewlineAt(visibleEnd)) 233 if (shouldAnnotate() && needInterchangeNewlineAt(visibleEnd))
193 markupAccumulator.appendInterchangeNewline(); 234 markupAccumulator.appendInterchangeNewline();
194 235
195 return markupAccumulator.takeResults(); 236 return markupAccumulator.takeResults();
196 } 237 }
197 238
198 template<typename Strategy> 239 template<typename Strategy>
199 Node* StyledMarkupSerializer<Strategy>::serializeNodes(Node* startNode, Node* pa stEnd, StyledMarkupAccumulator* markupAccumulator) 240 StyledMarkupTraverser<Strategy>::StyledMarkupTraverser()
241 : StyledMarkupTraverser(nullptr, nullptr)
200 { 242 {
201 if (!m_lastClosed)
202 m_lastClosed = traverseNodesForSerialization(startNode, pastEnd, nullptr );
203 if (m_lastClosed && Strategy::parent(*m_lastClosed))
204 m_wrappingStyle = EditingStyle::wrappingStyleForSerialization(Strategy:: parent(*m_lastClosed), shouldAnnotate());
205 return traverseNodesForSerialization(startNode, pastEnd, markupAccumulator);
206 } 243 }
207 244
208 template<typename Strategy> 245 template<typename Strategy>
209 Node* StyledMarkupSerializer<Strategy>::traverseNodesForSerialization(Node* star tNode, Node* pastEnd, StyledMarkupAccumulator* markupAccumulator) 246 StyledMarkupTraverser<Strategy>::StyledMarkupTraverser(StyledMarkupAccumulator* accumulator, Node* lastClosed)
247 : m_accumulator(accumulator)
248 , m_lastClosed(lastClosed)
249 , m_wrappingStyle(nullptr)
250 {
251 ASSERT(m_lastClosed || !m_accumulator);
yosin_UTC9 2015/06/18 05:13:29 How about this? if (!m_accumulator) { ASSERT(!m
hajimehoshi 2015/06/18 05:24:55 Done.
252 if (!m_accumulator || !m_lastClosed || !Strategy::parent(*m_lastClosed))
253 return;
254 m_wrappingStyle = EditingStyle::wrappingStyleForSerialization(Strategy::pare nt(*m_lastClosed), shouldAnnotate());
255 }
256
257 template<typename Strategy>
258 Node* StyledMarkupTraverser<Strategy>::traverseNodesForSerialization(Node* start Node, Node* pastEnd)
210 { 259 {
211 WillBeHeapVector<RawPtrWillBeMember<ContainerNode>> ancestorsToClose; 260 WillBeHeapVector<RawPtrWillBeMember<ContainerNode>> ancestorsToClose;
212 Node* next; 261 Node* next;
213 Node* lastClosed = nullptr; 262 Node* lastClosed = nullptr;
214 for (Node* n = startNode; n != pastEnd; n = next) { 263 for (Node* n = startNode; n != pastEnd; n = next) {
215 // According to <rdar://problem/5730668>, it is possible for n to blow 264 // According to <rdar://problem/5730668>, it is possible for n to blow
216 // past pastEnd and become null here. This shouldn't be possible. 265 // past pastEnd and become null here. This shouldn't be possible.
217 // This null check will prevent crashes (but create too much markup) 266 // This null check will prevent crashes (but create too much markup)
218 // and the ASSERT will hopefully lead us to understanding the problem. 267 // and the ASSERT will hopefully lead us to understanding the problem.
219 ASSERT(n); 268 ASSERT(n);
220 if (!n) 269 if (!n)
221 break; 270 break;
222 271
223 next = Strategy::next(*n); 272 next = Strategy::next(*n);
224 273
225 if (isBlock(n) && canHaveChildrenForEditing(n) && next == pastEnd) { 274 if (isBlock(n) && canHaveChildrenForEditing(n) && next == pastEnd) {
226 // Don't write out empty block containers that aren't fully selected . 275 // Don't write out empty block containers that aren't fully selected .
227 continue; 276 continue;
228 } 277 }
229 278
230 if (!n->layoutObject() && !enclosingElementWithTag(firstPositionInOrBefo reNode(n), selectTag)) { 279 if (!n->layoutObject() && !enclosingElementWithTag(firstPositionInOrBefo reNode(n), selectTag)) {
231 next = Strategy::nextSkippingChildren(*n); 280 next = Strategy::nextSkippingChildren(*n);
232 // Don't skip over pastEnd. 281 // Don't skip over pastEnd.
233 if (pastEnd && Strategy::isDescendantOf(*pastEnd, *n)) 282 if (pastEnd && Strategy::isDescendantOf(*pastEnd, *n))
234 next = pastEnd; 283 next = pastEnd;
235 } else { 284 } else {
236 // Add the node to the markup if we're not skipping the descendants 285 // Add the node to the markup if we're not skipping the descendants
237 if (markupAccumulator) 286 appendStartMarkup(*n);
238 appendStartMarkup(*markupAccumulator, *n);
239 287
240 // If node has no children, close the tag now. 288 // If node has no children, close the tag now.
241 if (Strategy::hasChildren(*n)) { 289 if (Strategy::hasChildren(*n)) {
242 ancestorsToClose.append(toContainerNode(n)); 290 ancestorsToClose.append(toContainerNode(n));
243 continue; 291 continue;
244 } 292 }
245 if (markupAccumulator && n->isElementNode()) 293 appendEndMarkup(*n);
246 markupAccumulator->appendEndTag(toElement(*n));
247 lastClosed = n; 294 lastClosed = n;
248 } 295 }
249 296
250 // If we didn't insert open tag and there's no more siblings or we're at the end of the traversal, take care of ancestors. 297 // If we didn't insert open tag and there's no more siblings or we're at the end of the traversal, take care of ancestors.
251 // FIXME: What happens if we just inserted open tag and reached the end? 298 // FIXME: What happens if we just inserted open tag and reached the end?
252 if (Strategy::nextSibling(*n) && next != pastEnd) 299 if (Strategy::nextSibling(*n) && next != pastEnd)
253 continue; 300 continue;
254 301
255 // Close up the ancestors. 302 // Close up the ancestors.
256 while (!ancestorsToClose.isEmpty()) { 303 while (!ancestorsToClose.isEmpty()) {
257 ContainerNode* ancestor = ancestorsToClose.last(); 304 ContainerNode* ancestor = ancestorsToClose.last();
258 ASSERT(ancestor); 305 ASSERT(ancestor);
259 if (next != pastEnd && Strategy::isDescendantOf(*next, *ancestor)) 306 if (next != pastEnd && Strategy::isDescendantOf(*next, *ancestor))
260 break; 307 break;
261 // Not at the end of the range, close ancestors up to sibling of nex t node. 308 // Not at the end of the range, close ancestors up to sibling of nex t node.
262 if (markupAccumulator && ancestor->isElementNode()) 309 appendEndMarkup(*ancestor);
263 markupAccumulator->appendEndTag(toElement(*ancestor));
264 lastClosed = ancestor; 310 lastClosed = ancestor;
265 ancestorsToClose.removeLast(); 311 ancestorsToClose.removeLast();
266 } 312 }
267 313
268 // Surround the currently accumulated markup with markup for ancestors w e never opened as we leave the subtree(s) rooted at those ancestors. 314 // Surround the currently accumulated markup with markup for ancestors w e never opened as we leave the subtree(s) rooted at those ancestors.
269 ContainerNode* nextParent = next ? Strategy::parent(*next) : nullptr; 315 ContainerNode* nextParent = next ? Strategy::parent(*next) : nullptr;
270 if (next == pastEnd || n == nextParent) 316 if (next == pastEnd || n == nextParent)
271 continue; 317 continue;
272 318
273 ASSERT(n); 319 ASSERT(n);
274 Node* lastAncestorClosedOrSelf = Strategy::isDescendantOf(*n, *lastClose d) ? lastClosed : n; 320 Node* lastAncestorClosedOrSelf = Strategy::isDescendantOf(*n, *lastClose d) ? lastClosed : n;
275 for (ContainerNode* parent = Strategy::parent(*lastAncestorClosedOrSelf) ; parent && parent != nextParent; parent = Strategy::parent(*parent)) { 321 for (ContainerNode* parent = Strategy::parent(*lastAncestorClosedOrSelf) ; parent && parent != nextParent; parent = Strategy::parent(*parent)) {
276 // All ancestors that aren't in the ancestorsToClose list should eit her be a) unrendered: 322 // All ancestors that aren't in the ancestorsToClose list should eit her be a) unrendered:
277 if (!parent->layoutObject()) 323 if (!parent->layoutObject())
278 continue; 324 continue;
279 // or b) ancestors that we never encountered during a pre-order trav ersal starting at startNode: 325 // or b) ancestors that we never encountered during a pre-order trav ersal starting at startNode:
280 ASSERT(startNode); 326 ASSERT(startNode);
281 ASSERT(Strategy::isDescendantOf(*startNode, *parent)); 327 ASSERT(Strategy::isDescendantOf(*startNode, *parent));
282 if (markupAccumulator) { 328 RefPtrWillBeRawPtr<EditingStyle> style = createInlineStyleIfNeeded(* parent);
283 RefPtrWillBeRawPtr<EditingStyle> style = createInlineStyleIfNeed ed(*parent); 329 wrapWithNode(*parent, style);
284 wrapWithNode(*markupAccumulator, *parent, style);
285 }
286 lastClosed = parent; 330 lastClosed = parent;
287 } 331 }
288 } 332 }
289 333
290 return lastClosed; 334 return lastClosed;
291 } 335 }
292 336
293 template<typename Strategy> 337 template<typename Strategy>
294 bool StyledMarkupSerializer<Strategy>::needsInlineStyle(const Element& element) 338 bool StyledMarkupTraverser<Strategy>::needsInlineStyle(const Element& element)
295 { 339 {
296 if (!element.isHTMLElement()) 340 if (!element.isHTMLElement())
297 return false; 341 return false;
298 if (shouldAnnotate()) 342 if (shouldAnnotate())
299 return true; 343 return true;
300 return convertBlocksToInlines() && isBlock(&element); 344 return convertBlocksToInlines() && isBlock(&element);
301 } 345 }
302 346
303 template<typename Strategy> 347 template<typename Strategy>
304 void StyledMarkupSerializer<Strategy>::wrapWithNode(StyledMarkupAccumulator& acc umulator, ContainerNode& node, PassRefPtrWillBeRawPtr<EditingStyle> style) 348 void StyledMarkupTraverser<Strategy>::wrapWithNode(ContainerNode& node, PassRefP trWillBeRawPtr<EditingStyle> style)
305 { 349 {
350 if (!m_accumulator)
351 return;
306 StringBuilder markup; 352 StringBuilder markup;
307 if (node.isDocumentNode()) { 353 if (node.isDocumentNode()) {
308 MarkupFormatter::appendXMLDeclaration(markup, toDocument(node)); 354 MarkupFormatter::appendXMLDeclaration(markup, toDocument(node));
309 accumulator.pushMarkup(markup.toString()); 355 m_accumulator->pushMarkup(markup.toString());
310 return; 356 return;
311 } 357 }
312 if (!node.isElementNode()) 358 if (!node.isElementNode())
313 return; 359 return;
314 Element& element = toElement(node); 360 Element& element = toElement(node);
315 if (shouldApplyWrappingStyle(element) || needsInlineStyle(element)) 361 if (shouldApplyWrappingStyle(element) || needsInlineStyle(element))
316 accumulator.appendElementWithInlineStyle(markup, element, style); 362 m_accumulator->appendElementWithInlineStyle(markup, element, style);
317 else 363 else
318 accumulator.appendElement(markup, element); 364 m_accumulator->appendElement(markup, element);
319 accumulator.pushMarkup(markup.toString()); 365 m_accumulator->pushMarkup(markup.toString());
320 accumulator.appendEndTag(toElement(node)); 366 m_accumulator->appendEndTag(toElement(node));
321 } 367 }
322 368
323 template<typename Strategy> 369 template<typename Strategy>
324 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupSerializer<Strategy>::createInlineS tyleIfNeeded(Node& node) 370 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupTraverser<Strategy>::createInlineSt yleIfNeeded(Node& node)
325 { 371 {
372 if (!m_accumulator)
373 return nullptr;
326 if (!node.isElementNode()) 374 if (!node.isElementNode())
327 return nullptr; 375 return nullptr;
328 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = createInlineStyle(toElement(n ode)); 376 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = createInlineStyle(toElement(n ode));
329 if (convertBlocksToInlines() && isBlock(&node)) 377 if (convertBlocksToInlines() && isBlock(&node))
330 inlineStyle->forceInline(); 378 inlineStyle->forceInline();
331 return inlineStyle; 379 return inlineStyle;
332 } 380 }
333 381
334 template<typename Strategy> 382 template<typename Strategy>
335 void StyledMarkupSerializer<Strategy>::appendStartMarkup(StyledMarkupAccumulator & accumulator, Node& node) 383 void StyledMarkupTraverser<Strategy>::appendStartMarkup(Node& node)
336 { 384 {
385 if (!m_accumulator)
386 return;
337 switch (node.nodeType()) { 387 switch (node.nodeType()) {
338 case Node::TEXT_NODE: { 388 case Node::TEXT_NODE: {
339 Text& text = toText(node); 389 Text& text = toText(node);
340 if (text.parentElement() && text.parentElement()->tagQName() == textarea Tag) { 390 if (text.parentElement() && isHTMLTextAreaElement(text.parentElement())) {
341 accumulator.appendText(text); 391 m_accumulator->appendText(text);
342 break; 392 break;
343 } 393 }
344 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr; 394 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr;
345 if (shouldApplyWrappingStyle(text)) { 395 if (shouldApplyWrappingStyle(text)) {
346 inlineStyle = m_wrappingStyle->copy(); 396 inlineStyle = m_wrappingStyle->copy();
347 // FIXME: <rdar://problem/5371536> Style rules that match pasted con tent can change it's appearance 397 // FIXME: <rdar://problem/5371536> Style rules that match pasted con tent can change it's appearance
348 // Make sure spans are inline style in paste side e.g. span { displa y: block }. 398 // Make sure spans are inline style in paste side e.g. span { displa y: block }.
349 inlineStyle->forceInline(); 399 inlineStyle->forceInline();
350 // FIXME: Should this be included in forceInline? 400 // FIXME: Should this be included in forceInline?
351 inlineStyle->style()->setProperty(CSSPropertyFloat, CSSValueNone); 401 inlineStyle->style()->setProperty(CSSPropertyFloat, CSSValueNone);
352 } 402 }
353 accumulator.appendTextWithInlineStyle(text, inlineStyle); 403 m_accumulator->appendTextWithInlineStyle(text, inlineStyle);
354 break; 404 break;
355 } 405 }
356 case Node::ELEMENT_NODE: { 406 case Node::ELEMENT_NODE: {
357 Element& element = toElement(node); 407 Element& element = toElement(node);
358 if ((element.isHTMLElement() && shouldAnnotate()) || shouldApplyWrapping Style(element)) { 408 if ((element.isHTMLElement() && shouldAnnotate()) || shouldApplyWrapping Style(element)) {
359 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = createInlineStyle(ele ment); 409 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = createInlineStyle(ele ment);
360 accumulator.appendElementWithInlineStyle(element, inlineStyle); 410 m_accumulator->appendElementWithInlineStyle(element, inlineStyle);
361 break; 411 break;
362 } 412 }
363 accumulator.appendElement(element); 413 m_accumulator->appendElement(element);
364 break; 414 break;
365 } 415 }
366 default: 416 default:
367 accumulator.appendStartMarkup(node); 417 m_accumulator->appendStartMarkup(node);
368 break; 418 break;
369 } 419 }
370 } 420 }
371 421
372 template<typename Strategy> 422 template<typename Strategy>
373 bool StyledMarkupSerializer<Strategy>::shouldApplyWrappingStyle(const Node& node ) const 423 void StyledMarkupTraverser<Strategy>::appendEndMarkup(Node& node)
424 {
425 if (!m_accumulator || !node.isElementNode())
426 return;
427 m_accumulator->appendEndTag(toElement(node));
428 }
429
430 template<typename Strategy>
431 bool StyledMarkupTraverser<Strategy>::shouldApplyWrappingStyle(const Node& node) const
374 { 432 {
375 return m_lastClosed && Strategy::parent(*m_lastClosed) == Strategy::parent(n ode) 433 return m_lastClosed && Strategy::parent(*m_lastClosed) == Strategy::parent(n ode)
376 && m_wrappingStyle && m_wrappingStyle->style(); 434 && m_wrappingStyle && m_wrappingStyle->style();
377 } 435 }
378 436
379 template<typename Strategy> 437 template<typename Strategy>
380 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupSerializer<Strategy>::createInlineS tyle(Element& element) 438 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupTraverser<Strategy>::createInlineSt yle(Element& element)
381 { 439 {
382 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr; 440 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr;
383 441
384 if (shouldApplyWrappingStyle(element)) { 442 if (shouldApplyWrappingStyle(element)) {
385 inlineStyle = m_wrappingStyle->copy(); 443 inlineStyle = m_wrappingStyle->copy();
386 inlineStyle->removePropertiesInElementDefaultStyle(&element); 444 inlineStyle->removePropertiesInElementDefaultStyle(&element);
387 inlineStyle->removeStyleConflictingWithStyleOfElement(&element); 445 inlineStyle->removeStyleConflictingWithStyleOfElement(&element);
388 } else { 446 } else {
389 inlineStyle = EditingStyle::create(); 447 inlineStyle = EditingStyle::create();
390 } 448 }
391 449
392 if (element.isStyledElement() && element.inlineStyle()) 450 if (element.isStyledElement() && element.inlineStyle())
393 inlineStyle->overrideWithStyle(element.inlineStyle()); 451 inlineStyle->overrideWithStyle(element.inlineStyle());
394 452
395 if (element.isHTMLElement() && shouldAnnotate()) 453 if (element.isHTMLElement() && shouldAnnotate())
396 inlineStyle->mergeStyleFromRulesForSerialization(&toHTMLElement(element) ); 454 inlineStyle->mergeStyleFromRulesForSerialization(&toHTMLElement(element) );
397 455
398 return inlineStyle; 456 return inlineStyle;
399 } 457 }
400 458
401 template class StyledMarkupSerializer<EditingStrategy>; 459 template class StyledMarkupSerializer<EditingStrategy>;
402 460
403 } // namespace blink 461 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/editing/StyledMarkupSerializer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698