OLD | NEW |
| (Empty) |
1 /* | |
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. | |
4 * Copyright (C) 2011 Igalia S.L. | |
5 * Copyright (C) 2011 Motorola Mobility. All rights reserved. | |
6 * | |
7 * Redistribution and use in source and binary forms, with or without | |
8 * modification, are permitted provided that the following conditions | |
9 * are met: | |
10 * 1. Redistributions of source code must retain the above copyright | |
11 * notice, this list of conditions and the following disclaimer. | |
12 * 2. Redistributions in binary form must reproduce the above copyright | |
13 * notice, this list of conditions and the following disclaimer in the | |
14 * documentation and/or other materials provided with the distribution. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 */ | |
28 | |
29 #include "config.h" | |
30 #include "core/editing/StyledMarkupAccumulator.h" | |
31 | |
32 #include "core/css/StylePropertySet.h" | |
33 #include "core/dom/Text.h" | |
34 #include "core/editing/EditingUtilities.h" | |
35 #include "core/editing/iterators/TextIterator.h" | |
36 #include "wtf/text/StringBuilder.h" | |
37 | |
38 namespace blink { | |
39 | |
40 namespace { | |
41 | |
42 size_t totalLength(const Vector<String>& strings) | |
43 { | |
44 size_t length = 0; | |
45 for (const auto& string : strings) | |
46 length += string.length(); | |
47 return length; | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 using namespace HTMLNames; | |
53 | |
54 StyledMarkupAccumulator::StyledMarkupAccumulator(EAbsoluteURLs shouldResolveURLs
, const TextOffset& start, const TextOffset& end, const PassRefPtrWillBeRawPtr<D
ocument> document, EAnnotateForInterchange shouldAnnotate, ConvertBlocksToInline
s convertBlocksToInlines) | |
55 : m_formatter(shouldResolveURLs) | |
56 , m_start(start) | |
57 , m_end(end) | |
58 , m_document(document) | |
59 , m_shouldAnnotate(shouldAnnotate) | |
60 , m_convertBlocksToInlines(convertBlocksToInlines) | |
61 { | |
62 } | |
63 | |
64 void StyledMarkupAccumulator::appendEndTag(const Element& element) | |
65 { | |
66 appendEndMarkup(m_result, element); | |
67 } | |
68 | |
69 void StyledMarkupAccumulator::appendStartMarkup(Node& node) | |
70 { | |
71 m_formatter.appendStartMarkup(m_result, node, nullptr); | |
72 } | |
73 | |
74 void StyledMarkupAccumulator::appendEndMarkup(StringBuilder& result, const Eleme
nt& element) | |
75 { | |
76 m_formatter.appendEndMarkup(result, element); | |
77 } | |
78 | |
79 void StyledMarkupAccumulator::appendText(Text& text) | |
80 { | |
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)); | |
95 } | |
96 | |
97 void StyledMarkupAccumulator::appendTextWithInlineStyle(Text& text, PassRefPtrWi
llBeRawPtr<EditingStyle> inlineStyle) | |
98 { | |
99 if (inlineStyle) { | |
100 // wrappingStyleForSerialization should have removed -webkit-text-decora
tions-in-effect | |
101 ASSERT(propertyMissingOrEqualToNone(inlineStyle->style(), CSSPropertyWeb
kitTextDecorationsInEffect)); | |
102 ASSERT(m_document); | |
103 | |
104 m_result.appendLiteral("<span style=\""); | |
105 MarkupFormatter::appendAttributeValue(m_result, inlineStyle->style()->as
Text(), m_document->isHTMLDocument()); | |
106 m_result.appendLiteral("\">"); | |
107 } | |
108 if (!shouldAnnotate()) { | |
109 appendText(text); | |
110 } else { | |
111 const bool useRenderedText = !enclosingElementWithTag(firstPositionInNod
e(&text), selectTag); | |
112 String content = useRenderedText ? renderedText(text) : stringValueForRa
nge(text); | |
113 StringBuilder buffer; | |
114 MarkupFormatter::appendCharactersReplacingEntities(buffer, content, 0, c
ontent.length(), EntityMaskInPCDATA); | |
115 m_result.append(convertHTMLTextToInterchangeFormat(buffer.toString(), te
xt)); | |
116 } | |
117 if (inlineStyle) | |
118 m_result.append("</span>"); | |
119 } | |
120 | |
121 void StyledMarkupAccumulator::appendElementWithInlineStyle(const Element& elemen
t, PassRefPtrWillBeRawPtr<EditingStyle> style) | |
122 { | |
123 appendElementWithInlineStyle(m_result, element, style); | |
124 } | |
125 | |
126 void StyledMarkupAccumulator::appendElementWithInlineStyle(StringBuilder& out, c
onst Element& element, PassRefPtrWillBeRawPtr<EditingStyle> style) | |
127 { | |
128 const bool documentIsHTML = element.document().isHTMLDocument(); | |
129 m_formatter.appendOpenTag(out, element, nullptr); | |
130 AttributeCollection attributes = element.attributes(); | |
131 for (const auto& attribute : attributes) { | |
132 // We'll handle the style attribute separately, below. | |
133 if (attribute.name() == styleAttr) | |
134 continue; | |
135 m_formatter.appendAttribute(out, element, attribute, nullptr); | |
136 } | |
137 if (style && !style->isEmpty()) { | |
138 out.appendLiteral(" style=\""); | |
139 MarkupFormatter::appendAttributeValue(out, style->style()->asText(), doc
umentIsHTML); | |
140 out.append('\"'); | |
141 } | |
142 m_formatter.appendCloseTag(out, element); | |
143 } | |
144 | |
145 void StyledMarkupAccumulator::appendElement(const Element& element) | |
146 { | |
147 appendElement(m_result, element); | |
148 } | |
149 | |
150 void StyledMarkupAccumulator::appendElement(StringBuilder& out, const Element& e
lement) | |
151 { | |
152 m_formatter.appendOpenTag(out, element, nullptr); | |
153 AttributeCollection attributes = element.attributes(); | |
154 for (const auto& attribute : attributes) | |
155 m_formatter.appendAttribute(out, element, attribute, nullptr); | |
156 m_formatter.appendCloseTag(out, element); | |
157 } | |
158 | |
159 void StyledMarkupAccumulator::wrapWithStyleNode(StylePropertySet* style) | |
160 { | |
161 // wrappingStyleForSerialization should have removed -webkit-text-decoration
s-in-effect | |
162 ASSERT(propertyMissingOrEqualToNone(style, CSSPropertyWebkitTextDecorationsI
nEffect)); | |
163 ASSERT(m_document); | |
164 | |
165 StringBuilder openTag; | |
166 openTag.appendLiteral("<div style=\""); | |
167 MarkupFormatter::appendAttributeValue(openTag, style->asText(), m_document->
isHTMLDocument()); | |
168 openTag.appendLiteral("\">"); | |
169 m_reversedPrecedingMarkup.append(openTag.toString()); | |
170 | |
171 m_result.append("</div>"); | |
172 } | |
173 | |
174 String StyledMarkupAccumulator::takeResults() | |
175 { | |
176 StringBuilder result; | |
177 result.reserveCapacity(totalLength(m_reversedPrecedingMarkup) + m_result.len
gth()); | |
178 | |
179 for (size_t i = m_reversedPrecedingMarkup.size(); i > 0; --i) | |
180 result.append(m_reversedPrecedingMarkup[i - 1]); | |
181 | |
182 result.append(m_result); | |
183 | |
184 // We remove '\0' characters because they are not visibly rendered to the us
er. | |
185 return result.toString().replace(0, ""); | |
186 } | |
187 | |
188 String StyledMarkupAccumulator::renderedText(Text& textNode) | |
189 { | |
190 int startOffset = 0; | |
191 int endOffset = textNode.length(); | |
192 if (m_start.text() == textNode) | |
193 startOffset = m_start.offset(); | |
194 if (m_end.text() == textNode) | |
195 endOffset = m_end.offset(); | |
196 return plainText(EphemeralRange(Position(&textNode, startOffset), Position(&
textNode, endOffset))); | |
197 } | |
198 | |
199 String StyledMarkupAccumulator::stringValueForRange(const Text& node) | |
200 { | |
201 if (m_start.isNull()) | |
202 return node.data(); | |
203 | |
204 String str = node.data(); | |
205 if (m_start.text() == node) | |
206 str.truncate(m_end.offset()); | |
207 if (m_end.text() == node) | |
208 str.remove(0, m_start.offset()); | |
209 return str; | |
210 } | |
211 | |
212 bool StyledMarkupAccumulator::shouldAnnotate() const | |
213 { | |
214 return m_shouldAnnotate == AnnotateForInterchange; | |
215 } | |
216 | |
217 void StyledMarkupAccumulator::pushMarkup(const String& str) | |
218 { | |
219 m_reversedPrecedingMarkup.append(str); | |
220 } | |
221 | |
222 void StyledMarkupAccumulator::appendInterchangeNewline() | |
223 { | |
224 DEFINE_STATIC_LOCAL(const String, interchangeNewlineString, ("<br class=\""
AppleInterchangeNewline "\">")); | |
225 m_result.append(interchangeNewlineString); | |
226 } | |
227 | |
228 } // namespace blink | |
OLD | NEW |