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

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

Issue 1177323005: Move StyledMarkupAccumulator::shouldApplyWrappingStyle to its serializer (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: yosin's review 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
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 StyledMarkupAccumulator::StyledMarkupAccumulator(EAbsoluteURLs shouldResolveURLs , const TextOffset& start, const TextOffset& end, const PassRefPtrWillBeRawPtr<D ocument> document, EAnnotateForInterchange shouldAnnotate, Node* highestNodeToBe Serialized) 54 StyledMarkupAccumulator::StyledMarkupAccumulator(EAbsoluteURLs shouldResolveURLs , const TextOffset& start, const TextOffset& end, const PassRefPtrWillBeRawPtr<D ocument> document, EAnnotateForInterchange shouldAnnotate, Node* highestNodeToBe Serialized)
55 : m_formatter(shouldResolveURLs) 55 : m_formatter(shouldResolveURLs)
56 , m_start(start) 56 , m_start(start)
57 , m_end(end) 57 , m_end(end)
58 , m_document(document) 58 , m_document(document)
59 , m_shouldAnnotate(shouldAnnotate) 59 , m_shouldAnnotate(shouldAnnotate)
60 , m_highestNodeToBeSerialized(highestNodeToBeSerialized) 60 , m_highestNodeToBeSerialized(highestNodeToBeSerialized)
61 { 61 {
62 } 62 }
63 63
64 void StyledMarkupAccumulator::appendStartTag(Node& node)
65 {
66 appendStartMarkup(node);
67 }
68
69 void StyledMarkupAccumulator::appendEndTag(const Element& element) 64 void StyledMarkupAccumulator::appendEndTag(const Element& element)
70 { 65 {
71 appendEndMarkup(m_result, element); 66 appendEndMarkup(m_result, element);
72 } 67 }
73 68
74 void StyledMarkupAccumulator::appendStartMarkup(Node& node) 69 void StyledMarkupAccumulator::appendStartMarkup(Node& node)
75 { 70 {
76 switch (node.nodeType()) { 71 m_formatter.appendStartMarkup(m_result, node, nullptr);
77 case Node::TEXT_NODE:
78 appendText(toText(node));
79 break;
80 case Node::ELEMENT_NODE: {
81 Element& element = toElement(node);
82 RefPtrWillBeRawPtr<EditingStyle> style = createInlineStyle(element);
83 appendElement(element, style);
84 break;
85 }
86 default:
87 m_formatter.appendStartMarkup(m_result, node, nullptr);
88 break;
89 }
90 } 72 }
91 73
92 void StyledMarkupAccumulator::appendEndMarkup(StringBuilder& result, const Eleme nt& element) 74 void StyledMarkupAccumulator::appendEndMarkup(StringBuilder& result, const Eleme nt& element)
93 { 75 {
94 m_formatter.appendEndMarkup(result, element); 76 m_formatter.appendEndMarkup(result, element);
95 } 77 }
96 78
97 void StyledMarkupAccumulator::appendText(Text& text) 79 void StyledMarkupAccumulator::appendText(Text& text)
98 { 80 {
99 appendText(m_result, text); 81 const String& str = text.data();
82 unsigned length = str.length();
83 unsigned start = 0;
84 if (m_end.isNotNull()) {
85 if (text == m_end.text())
86 length = m_end.offset();
87 }
88 if (m_start.isNotNull()) {
89 if (text == m_start.text()) {
90 start = m_start.offset();
91 length -= start;
92 }
93 }
94 MarkupFormatter::appendCharactersReplacingEntities(m_result, str, start, len gth, m_formatter.entityMaskForText(text));
100 } 95 }
101 96
102 void StyledMarkupAccumulator::appendText(StringBuilder& out, Text& text) 97 void StyledMarkupAccumulator::appendTextWithInlineStyle(Text& text, PassRefPtrWi llBeRawPtr<EditingStyle> inlineStyle)
103 { 98 {
104 const bool parentIsTextarea = text.parentElement() && text.parentElement()-> tagQName() == textareaTag; 99 if (inlineStyle) {
105 const bool wrappingSpan = shouldApplyWrappingStyle(text) && !parentIsTextare a;
106 if (wrappingSpan) {
107 RefPtrWillBeRawPtr<EditingStyle> wrappingStyle = m_wrappingStyle->copy() ;
108 // FIXME: <rdar://problem/5371536> Style rules that match pasted content can change it's appearance
109 // Make sure spans are inline style in paste side e.g. span { display: b lock }.
110 wrappingStyle->forceInline();
111 // FIXME: Should this be included in forceInline?
112 wrappingStyle->style()->setProperty(CSSPropertyFloat, CSSValueNone);
113
114 // wrappingStyleForSerialization should have removed -webkit-text-decora tions-in-effect 100 // wrappingStyleForSerialization should have removed -webkit-text-decora tions-in-effect
115 ASSERT(propertyMissingOrEqualToNone(wrappingStyle->style(), CSSPropertyW ebkitTextDecorationsInEffect)); 101 ASSERT(propertyMissingOrEqualToNone(inlineStyle->style(), CSSPropertyWeb kitTextDecorationsInEffect));
116 ASSERT(m_document); 102 ASSERT(m_document);
117 103
118 out.appendLiteral("<span style=\""); 104 m_result.appendLiteral("<span style=\"");
119 MarkupFormatter::appendAttributeValue(out, wrappingStyle->style()->asTex t(), m_document->isHTMLDocument()); 105 MarkupFormatter::appendAttributeValue(m_result, inlineStyle->style()->as Text(), m_document->isHTMLDocument());
120 out.appendLiteral("\">"); 106 m_result.appendLiteral("\">");
121 } 107 }
122 108
123 if (!shouldAnnotate() || parentIsTextarea) { 109 if (!shouldAnnotate()) {
124 const String& str = text.data(); 110 appendText(text);
125 unsigned length = str.length();
126 unsigned start = 0;
127 if (m_end.isNotNull()) {
128 if (text == m_end.text())
129 length = m_end.offset();
130 }
131 if (m_start.isNotNull()) {
132 if (text == m_start.text()) {
133 start = m_start.offset();
134 length -= start;
135 }
136 }
137 MarkupFormatter::appendCharactersReplacingEntities(out, str, start, leng th, m_formatter.entityMaskForText(text));
138 } else { 111 } else {
139 const bool useRenderedText = !enclosingElementWithTag(firstPositionInNod e(&text), selectTag); 112 const bool useRenderedText = !enclosingElementWithTag(firstPositionInNod e(&text), selectTag);
140 String content = useRenderedText ? renderedText(text) : stringValueForRa nge(text); 113 String content = useRenderedText ? renderedText(text) : stringValueForRa nge(text);
141 StringBuilder buffer; 114 StringBuilder buffer;
142 MarkupFormatter::appendCharactersReplacingEntities(buffer, content, 0, c ontent.length(), EntityMaskInPCDATA); 115 MarkupFormatter::appendCharactersReplacingEntities(buffer, content, 0, c ontent.length(), EntityMaskInPCDATA);
143 out.append(convertHTMLTextToInterchangeFormat(buffer.toString(), text)); 116 m_result.append(convertHTMLTextToInterchangeFormat(buffer.toString(), te xt));
144 } 117 }
145 118
146 if (wrappingSpan) 119 if (inlineStyle)
147 out.append("</span>"); 120 m_result.append("</span>");
148 }
149
150 void StyledMarkupAccumulator::appendElement(const Element& element, PassRefPtrWi llBeRawPtr<EditingStyle> style)
151 {
152 if ((element.isHTMLElement() && shouldAnnotate()) || shouldApplyWrappingStyl e(element)) {
153 appendElementWithInlineStyle(m_result, element, style);
154 return;
155 }
156 appendElement(m_result, element);
157 } 121 }
158 122
159 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupAccumulator::createInlineStyle(Elem ent& element) 123 RefPtrWillBeRawPtr<EditingStyle> StyledMarkupAccumulator::createInlineStyle(Elem ent& element)
160 { 124 {
161 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr; 125 RefPtrWillBeRawPtr<EditingStyle> inlineStyle = nullptr;
162 126
163 if (shouldApplyWrappingStyle(element)) { 127 if (shouldApplyWrappingStyle(element)) {
164 inlineStyle = m_wrappingStyle->copy(); 128 inlineStyle = m_wrappingStyle->copy();
165 inlineStyle->removePropertiesInElementDefaultStyle(&element); 129 inlineStyle->removePropertiesInElementDefaultStyle(&element);
166 inlineStyle->removeStyleConflictingWithStyleOfElement(&element); 130 inlineStyle->removeStyleConflictingWithStyleOfElement(&element);
167 } else { 131 } else {
168 inlineStyle = EditingStyle::create(); 132 inlineStyle = EditingStyle::create();
169 } 133 }
170 134
171 if (element.isStyledElement() && element.inlineStyle()) 135 if (element.isStyledElement() && element.inlineStyle())
172 inlineStyle->overrideWithStyle(element.inlineStyle()); 136 inlineStyle->overrideWithStyle(element.inlineStyle());
173 137
174 if (element.isHTMLElement() && shouldAnnotate()) 138 if (element.isHTMLElement() && shouldAnnotate())
175 inlineStyle->mergeStyleFromRulesForSerialization(&toHTMLElement(element) ); 139 inlineStyle->mergeStyleFromRulesForSerialization(&toHTMLElement(element) );
176 140
177 return inlineStyle; 141 return inlineStyle;
178 } 142 }
179 143
144 void StyledMarkupAccumulator::appendElementWithInlineStyle(const Element& elemen t, PassRefPtrWillBeRawPtr<EditingStyle> style)
145 {
146 appendElementWithInlineStyle(m_result, element, style);
147 }
148
180 void StyledMarkupAccumulator::appendElementWithInlineStyle(StringBuilder& out, c onst Element& element, PassRefPtrWillBeRawPtr<EditingStyle> style) 149 void StyledMarkupAccumulator::appendElementWithInlineStyle(StringBuilder& out, c onst Element& element, PassRefPtrWillBeRawPtr<EditingStyle> style)
181 { 150 {
182 const bool documentIsHTML = element.document().isHTMLDocument(); 151 const bool documentIsHTML = element.document().isHTMLDocument();
183 m_formatter.appendOpenTag(out, element, nullptr); 152 m_formatter.appendOpenTag(out, element, nullptr);
184 AttributeCollection attributes = element.attributes(); 153 AttributeCollection attributes = element.attributes();
185 for (const auto& attribute : attributes) { 154 for (const auto& attribute : attributes) {
186 // We'll handle the style attribute separately, below. 155 // We'll handle the style attribute separately, below.
187 if (attribute.name() == styleAttr) 156 if (attribute.name() == styleAttr)
188 continue; 157 continue;
189 m_formatter.appendAttribute(out, element, attribute, nullptr); 158 m_formatter.appendAttribute(out, element, attribute, nullptr);
190 } 159 }
191 if (style && !style->isEmpty()) { 160 if (style && !style->isEmpty()) {
192 out.appendLiteral(" style=\""); 161 out.appendLiteral(" style=\"");
193 MarkupFormatter::appendAttributeValue(out, style->style()->asText(), doc umentIsHTML); 162 MarkupFormatter::appendAttributeValue(out, style->style()->asText(), doc umentIsHTML);
194 out.append('\"'); 163 out.append('\"');
195 } 164 }
196 m_formatter.appendCloseTag(out, element); 165 m_formatter.appendCloseTag(out, element);
197 } 166 }
198 167
168 void StyledMarkupAccumulator::appendElement(const Element& element)
169 {
170 appendElement(m_result, element);
171 }
172
199 void StyledMarkupAccumulator::appendElement(StringBuilder& out, const Element& e lement) 173 void StyledMarkupAccumulator::appendElement(StringBuilder& out, const Element& e lement)
200 { 174 {
201 m_formatter.appendOpenTag(out, element, nullptr); 175 m_formatter.appendOpenTag(out, element, nullptr);
202 AttributeCollection attributes = element.attributes(); 176 AttributeCollection attributes = element.attributes();
203 for (const auto& attribute : attributes) 177 for (const auto& attribute : attributes)
204 m_formatter.appendAttribute(out, element, attribute, nullptr); 178 m_formatter.appendAttribute(out, element, attribute, nullptr);
205 m_formatter.appendCloseTag(out, element); 179 m_formatter.appendCloseTag(out, element);
206 } 180 }
207 181
208 void StyledMarkupAccumulator::wrapWithStyleNode(StylePropertySet* style) 182 void StyledMarkupAccumulator::wrapWithStyleNode(StylePropertySet* style)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 return node.data(); 225 return node.data();
252 226
253 String str = node.data(); 227 String str = node.data();
254 if (m_start.text() == node) 228 if (m_start.text() == node)
255 str.truncate(m_end.offset()); 229 str.truncate(m_end.offset());
256 if (m_end.text() == node) 230 if (m_end.text() == node)
257 str.remove(0, m_start.offset()); 231 str.remove(0, m_start.offset());
258 return str; 232 return str;
259 } 233 }
260 234
261 bool StyledMarkupAccumulator::shouldApplyWrappingStyle(const Node& node) const 235 bool StyledMarkupAccumulator::shouldApplyWrappingStyle(const Node& node) const
hajimehoshi 2015/06/16 10:07:51 TODO: Remove this
hajimehoshi 2015/06/16 10:56:47 Done.
262 { 236 {
263 // TODO(hajimehoshi): Use Strategy 237 // TODO(hajimehoshi): Use Strategy
264 return m_highestNodeToBeSerialized && m_highestNodeToBeSerialized->parentNod e() == node.parentNode() 238 return m_highestNodeToBeSerialized && m_highestNodeToBeSerialized->parentNod e() == node.parentNode()
265 && m_wrappingStyle && m_wrappingStyle->style(); 239 && m_wrappingStyle && m_wrappingStyle->style();
266 } 240 }
267 241
268 bool StyledMarkupAccumulator::shouldAnnotate() const 242 bool StyledMarkupAccumulator::shouldAnnotate() const
269 { 243 {
270 return m_shouldAnnotate == AnnotateForInterchange; 244 return m_shouldAnnotate == AnnotateForInterchange;
271 } 245 }
272 246
273 void StyledMarkupAccumulator::pushMarkup(const String& str) 247 void StyledMarkupAccumulator::pushMarkup(const String& str)
274 { 248 {
275 m_reversedPrecedingMarkup.append(str); 249 m_reversedPrecedingMarkup.append(str);
276 } 250 }
277 251
278 void StyledMarkupAccumulator::appendInterchangeNewline() 252 void StyledMarkupAccumulator::appendInterchangeNewline()
279 { 253 {
280 DEFINE_STATIC_LOCAL(const String, interchangeNewlineString, ("<br class=\"" AppleInterchangeNewline "\">")); 254 DEFINE_STATIC_LOCAL(const String, interchangeNewlineString, ("<br class=\"" AppleInterchangeNewline "\">"));
281 m_result.append(interchangeNewlineString); 255 m_result.append(interchangeNewlineString);
282 } 256 }
283 257
284 } // namespace blink 258 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/editing/StyledMarkupAccumulator.h ('k') | Source/core/editing/StyledMarkupSerializer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698