2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "MarkupAccumulator.h"
30 #include "CDATASection.h"
31 #include "CharacterNames.h"
33 #include "DocumentFragment.h"
34 #include "DocumentType.h"
36 #include "HTMLElement.h"
37 #include "HTMLNames.h"
39 #include "ProcessingInstruction.h"
40 #include "XMLNSNames.h"
44 using namespace HTMLNames;
46 void appendCharactersReplacingEntities(Vector<UChar>& out, const UChar* content, size_t length, EntityMask entityMask)
48 DEFINE_STATIC_LOCAL(const String, ampReference, ("&"));
49 DEFINE_STATIC_LOCAL(const String, ltReference, ("<"));
50 DEFINE_STATIC_LOCAL(const String, gtReference, (">"));
51 DEFINE_STATIC_LOCAL(const String, quotReference, ("""));
52 DEFINE_STATIC_LOCAL(const String, nbspReference, (" "));
54 static const EntityDescription entityMaps[] = {
55 { '&', ampReference, EntityAmp },
56 { '<', ltReference, EntityLt },
57 { '>', gtReference, EntityGt },
58 { '"', quotReference, EntityQuot },
59 { noBreakSpace, nbspReference, EntityNbsp },
62 size_t positionAfterLastEntity = 0;
63 for (size_t i = 0; i < length; ++i) {
64 for (size_t m = 0; m < WTF_ARRAY_LENGTH(entityMaps); ++m) {
65 if (content[i] == entityMaps[m].entity && entityMaps[m].mask & entityMask) {
66 out.append(content + positionAfterLastEntity, i - positionAfterLastEntity);
67 append(out, entityMaps[m].reference);
68 positionAfterLastEntity = i + 1;
73 out.append(content + positionAfterLastEntity, length - positionAfterLastEntity);
76 MarkupAccumulator::MarkupAccumulator(Vector<Node*>* nodes, EAbsoluteURLs shouldResolveURLs, const Range* range)
79 , m_shouldResolveURLs(shouldResolveURLs)
83 MarkupAccumulator::~MarkupAccumulator()
87 String MarkupAccumulator::serializeNodes(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly)
90 serializeNodesWithNamespaces(node, nodeToSkip, childrenOnly, 0);
91 out.reserveInitialCapacity(length());
92 concatenateMarkup(out);
93 return String::adopt(out);
96 void MarkupAccumulator::serializeNodesWithNamespaces(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly, const Namespaces* namespaces)
98 if (node == nodeToSkip)
101 Namespaces namespaceHash;
103 namespaceHash = *namespaces;
106 appendStartTag(node, &namespaceHash);
108 if (!(node->document()->isHTMLDocument() && elementCannotHaveEndTag(node))) {
109 for (Node* current = node->firstChild(); current; current = current->nextSibling())
110 serializeNodesWithNamespaces(current, nodeToSkip, IncludeNode, &namespaceHash);
117 void MarkupAccumulator::appendString(const String& string)
119 m_succeedingMarkup.append(string);
122 void MarkupAccumulator::appendStartTag(Node* node, Namespaces* namespaces)
124 Vector<UChar> markup;
125 appendStartMarkup(markup, node, namespaces);
126 appendString(String::adopt(markup));
128 m_nodes->append(node);
131 void MarkupAccumulator::appendEndTag(Node* node)
133 Vector<UChar> markup;
134 appendEndMarkup(markup, node);
135 appendString(String::adopt(markup));
138 size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
141 for (size_t i = 0; i < strings.size(); ++i)
142 length += strings[i].length();
146 // FIXME: This is a very inefficient way of accumulating the markup.
147 // We're converting results of appendStartMarkup and appendEndMarkup from Vector<UChar> to String
148 // and then back to Vector<UChar> and again to String here.
149 void MarkupAccumulator::concatenateMarkup(Vector<UChar>& out)
151 for (size_t i = 0; i < m_succeedingMarkup.size(); ++i)
152 append(out, m_succeedingMarkup[i]);
155 void MarkupAccumulator::appendAttributeValue(Vector<UChar>& result, const String& attribute, bool documentIsHTML)
157 appendCharactersReplacingEntities(result, attribute.characters(), attribute.length(),
158 documentIsHTML ? EntityMaskInHTMLAttributeValue : EntityMaskInAttributeValue);
161 void MarkupAccumulator::appendQuotedURLAttributeValue(Vector<UChar>& result, const String& urlString)
163 UChar quoteChar = '\"';
164 String strippedURLString = urlString.stripWhiteSpace();
165 if (protocolIsJavaScript(strippedURLString)) {
166 // minimal escaping for javascript urls
167 if (strippedURLString.contains('"')) {
168 if (strippedURLString.contains('\''))
169 strippedURLString.replace('\"', """);
173 result.append(quoteChar);
174 append(result, strippedURLString);
175 result.append(quoteChar);
179 // FIXME: This does not fully match other browsers. Firefox percent-escapes non-ASCII characters for innerHTML.
180 result.append(quoteChar);
181 appendAttributeValue(result, urlString, false);
182 result.append(quoteChar);
185 void MarkupAccumulator::appendNodeValue(Vector<UChar>& out, const Node* node, const Range* range, EntityMask entityMask)
187 String str = node->nodeValue();
188 const UChar* characters = str.characters();
189 size_t length = str.length();
193 if (node == range->endContainer(ec))
194 length = range->endOffset(ec);
195 if (node == range->startContainer(ec)) {
196 size_t start = range->startOffset(ec);
202 appendCharactersReplacingEntities(out, characters, length, entityMask);
205 bool MarkupAccumulator::shouldAddNamespaceElement(const Element* element)
207 // Don't add namespace attribute if it is already defined for this elem.
208 const AtomicString& prefix = element->prefix();
209 AtomicString attr = !prefix.isEmpty() ? "xmlns:" + prefix : "xmlns";
210 return !element->hasAttribute(attr);
213 bool MarkupAccumulator::shouldAddNamespaceAttribute(const Attribute& attribute, Namespaces& namespaces)
215 namespaces.checkConsistency();
217 // Don't add namespace attributes twice
218 if (attribute.name() == XMLNSNames::xmlnsAttr) {
219 namespaces.set(emptyAtom.impl(), attribute.value().impl());
223 QualifiedName xmlnsPrefixAttr(xmlnsAtom, attribute.localName(), XMLNSNames::xmlnsNamespaceURI);
224 if (attribute.name() == xmlnsPrefixAttr) {
225 namespaces.set(attribute.localName().impl(), attribute.value().impl());
232 void MarkupAccumulator::appendNamespace(Vector<UChar>& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces& namespaces)
234 namespaces.checkConsistency();
235 if (namespaceURI.isEmpty())
238 // Use emptyAtoms's impl() for both null and empty strings since the HashMap can't handle 0 as a key
239 AtomicStringImpl* pre = prefix.isEmpty() ? emptyAtom.impl() : prefix.impl();
240 AtomicStringImpl* foundNS = namespaces.get(pre);
241 if (foundNS != namespaceURI.impl()) {
242 namespaces.set(pre, namespaceURI.impl());
244 append(result, xmlnsAtom.string());
245 if (!prefix.isEmpty()) {
247 append(result, prefix);
252 appendAttributeValue(result, namespaceURI, false);
257 EntityMask MarkupAccumulator::entityMaskForText(Text* text) const
259 const QualifiedName* parentName = 0;
260 if (text->parentElement())
261 parentName = &static_cast<Element*>(text->parentElement())->tagQName();
263 if (parentName && (*parentName == scriptTag || *parentName == styleTag || *parentName == xmpTag))
264 return EntityMaskInCDATA;
266 return text->document()->isHTMLDocument() ? EntityMaskInHTMLPCDATA : EntityMaskInPCDATA;
269 void MarkupAccumulator::appendText(Vector<UChar>& out, Text* text)
271 appendNodeValue(out, text, m_range, entityMaskForText(text));
274 void MarkupAccumulator::appendComment(Vector<UChar>& out, const String& comment)
276 // FIXME: Comment content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "-->".
278 append(out, comment);
282 void MarkupAccumulator::appendDocumentType(Vector<UChar>& result, const DocumentType* n)
284 if (n->name().isEmpty())
287 append(result, "<!DOCTYPE ");
288 append(result, n->name());
289 if (!n->publicId().isEmpty()) {
290 append(result, " PUBLIC \"");
291 append(result, n->publicId());
292 append(result, "\"");
293 if (!n->systemId().isEmpty()) {
294 append(result, " \"");
295 append(result, n->systemId());
296 append(result, "\"");
298 } else if (!n->systemId().isEmpty()) {
299 append(result, " SYSTEM \"");
300 append(result, n->systemId());
301 append(result, "\"");
303 if (!n->internalSubset().isEmpty()) {
304 append(result, " [");
305 append(result, n->internalSubset());
311 void MarkupAccumulator::appendProcessingInstruction(Vector<UChar>& out, const String& target, const String& data)
313 // FIXME: PI data is not escaped, but XMLSerializer (and possibly other callers) this should raise an exception if it includes "?>".
321 void MarkupAccumulator::appendElement(Vector<UChar>& out, Element* element, Namespaces* namespaces)
323 appendOpenTag(out, element, namespaces);
325 NamedNodeMap* attributes = element->attributes();
326 unsigned length = attributes->length();
327 for (unsigned int i = 0; i < length; i++)
328 appendAttribute(out, element, *attributes->attributeItem(i), namespaces);
330 appendCloseTag(out, element);
333 void MarkupAccumulator::appendOpenTag(Vector<UChar>& out, Element* element, Namespaces* namespaces)
336 append(out, element->nodeNamePreservingCase());
337 if (!element->document()->isHTMLDocument() && namespaces && shouldAddNamespaceElement(element))
338 appendNamespace(out, element->prefix(), element->namespaceURI(), *namespaces);
341 void MarkupAccumulator::appendCloseTag(Vector<UChar>& out, Element* element)
343 if (shouldSelfClose(element)) {
344 if (element->isHTMLElement())
345 out.append(' '); // XHTML 1.0 <-> HTML compatibility.
351 void MarkupAccumulator::appendAttribute(Vector<UChar>& out, Element* element, const Attribute& attribute, Namespaces* namespaces)
353 bool documentIsHTML = element->document()->isHTMLDocument();
358 append(out, attribute.name().localName());
360 append(out, attribute.name().toString());
364 if (element->isURLAttribute(const_cast<Attribute*>(&attribute))) {
365 // We don't want to complete file:/// URLs because it may contain sensitive information
366 // about the user's system.
367 if (shouldResolveURLs() && !element->document()->url().isLocalFile())
368 appendQuotedURLAttributeValue(out, element->document()->completeURL(attribute.value()).string());
370 appendQuotedURLAttributeValue(out, attribute.value());
373 appendAttributeValue(out, attribute.value(), documentIsHTML);
377 if (!documentIsHTML && namespaces && shouldAddNamespaceAttribute(attribute, *namespaces))
378 appendNamespace(out, attribute.prefix(), attribute.namespaceURI(), *namespaces);
381 void MarkupAccumulator::appendCDATASection(Vector<UChar>& out, const String& section)
383 // FIXME: CDATA content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "]]>".
384 append(out, "<![CDATA[");
385 append(out, section);
389 void MarkupAccumulator::appendStartMarkup(Vector<UChar>& result, const Node* node, Namespaces* namespaces)
392 namespaces->checkConsistency();
394 switch (node->nodeType()) {
395 case Node::TEXT_NODE:
396 appendText(result, static_cast<Text*>(const_cast<Node*>(node)));
398 case Node::COMMENT_NODE:
399 appendComment(result, static_cast<const Comment*>(node)->data());
401 case Node::DOCUMENT_NODE:
402 case Node::DOCUMENT_FRAGMENT_NODE:
404 case Node::DOCUMENT_TYPE_NODE:
405 appendDocumentType(result, static_cast<const DocumentType*>(node));
407 case Node::PROCESSING_INSTRUCTION_NODE:
408 appendProcessingInstruction(result, static_cast<const ProcessingInstruction*>(node)->target(), static_cast<const ProcessingInstruction*>(node)->data());
410 case Node::ELEMENT_NODE:
411 appendElement(result, static_cast<Element*>(const_cast<Node*>(node)), namespaces);
413 case Node::CDATA_SECTION_NODE:
414 appendCDATASection(result, static_cast<const CDATASection*>(node)->data());
416 case Node::ATTRIBUTE_NODE:
417 case Node::ENTITY_NODE:
418 case Node::ENTITY_REFERENCE_NODE:
419 case Node::NOTATION_NODE:
420 case Node::XPATH_NAMESPACE_NODE:
421 ASSERT_NOT_REACHED();
426 // Rules of self-closure
427 // 1. No elements in HTML documents use the self-closing syntax.
428 // 2. Elements w/ children never self-close because they use a separate end tag.
429 // 3. HTML elements which do not have a "forbidden" end tag will close with a separate end tag.
430 // 4. Other elements self-close.
431 bool MarkupAccumulator::shouldSelfClose(const Node* node)
433 if (node->document()->isHTMLDocument())
435 if (node->hasChildNodes())
437 if (node->isHTMLElement() && !elementCannotHaveEndTag(node))
442 bool MarkupAccumulator::elementCannotHaveEndTag(const Node* node)
444 if (!node->isHTMLElement())
447 // FIXME: ieForbidsInsertHTML may not be the right function to call here
448 // ieForbidsInsertHTML is used to disallow setting innerHTML/outerHTML
449 // or createContextualFragment. It does not necessarily align with
450 // which elements should be serialized w/o end tags.
451 return static_cast<const HTMLElement*>(node)->ieForbidsInsertHTML();
454 void MarkupAccumulator::appendEndMarkup(Vector<UChar>& result, const Node* node)
456 if (!node->isElementNode() || shouldSelfClose(node) || (!node->hasChildNodes() && elementCannotHaveEndTag(node)))
461 append(result, static_cast<const Element*>(node)->nodeNamePreservingCase());