2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2012 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 "core/editing/MarkupAccumulator.h"
30 #include "HTMLNames.h"
31 #include "XLinkNames.h"
32 #include "XMLNSNames.h"
34 #include "core/dom/CDATASection.h"
35 #include "core/dom/Comment.h"
36 #include "core/dom/DocumentFragment.h"
37 #include "core/dom/DocumentType.h"
38 #include "core/dom/ProcessingInstruction.h"
39 #include "core/editing/Editor.h"
40 #include "core/html/HTMLElement.h"
41 #include "core/html/HTMLTemplateElement.h"
42 #include "weborigin/KURL.h"
43 #include "wtf/unicode/CharacterNames.h"
47 using namespace HTMLNames;
49 void MarkupAccumulator::appendCharactersReplacingEntities(StringBuilder& result, const String& source, unsigned offset, unsigned length, EntityMask entityMask)
51 DEFINE_STATIC_LOCAL(const String, ampReference, ("&"));
52 DEFINE_STATIC_LOCAL(const String, ltReference, ("<"));
53 DEFINE_STATIC_LOCAL(const String, gtReference, (">"));
54 DEFINE_STATIC_LOCAL(const String, quotReference, ("""));
55 DEFINE_STATIC_LOCAL(const String, nbspReference, (" "));
57 static const EntityDescription entityMaps[] = {
58 { '&', ampReference, EntityAmp },
59 { '<', ltReference, EntityLt },
60 { '>', gtReference, EntityGt },
61 { '"', quotReference, EntityQuot },
62 { noBreakSpace, nbspReference, EntityNbsp },
65 if (!(offset + length))
68 ASSERT(offset + length <= source.length());
70 if (source.is8Bit()) {
71 const LChar* text = source.characters8() + offset;
73 size_t positionAfterLastEntity = 0;
74 for (size_t i = 0; i < length; ++i) {
75 for (size_t entityIndex = 0; entityIndex < WTF_ARRAY_LENGTH(entityMaps); ++entityIndex) {
76 if (text[i] == entityMaps[entityIndex].entity && entityMaps[entityIndex].mask & entityMask) {
77 result.append(text + positionAfterLastEntity, i - positionAfterLastEntity);
78 result.append(entityMaps[entityIndex].reference);
79 positionAfterLastEntity = i + 1;
84 result.append(text + positionAfterLastEntity, length - positionAfterLastEntity);
86 const UChar* text = source.characters16() + offset;
88 size_t positionAfterLastEntity = 0;
89 for (size_t i = 0; i < length; ++i) {
90 for (size_t entityIndex = 0; entityIndex < WTF_ARRAY_LENGTH(entityMaps); ++entityIndex) {
91 if (text[i] == entityMaps[entityIndex].entity && entityMaps[entityIndex].mask & entityMask) {
92 result.append(text + positionAfterLastEntity, i - positionAfterLastEntity);
93 result.append(entityMaps[entityIndex].reference);
94 positionAfterLastEntity = i + 1;
99 result.append(text + positionAfterLastEntity, length - positionAfterLastEntity);
103 MarkupAccumulator::MarkupAccumulator(Vector<Node*>* nodes, EAbsoluteURLs resolveUrlsMethod, const Range* range)
106 , m_resolveURLsMethod(resolveUrlsMethod)
110 MarkupAccumulator::~MarkupAccumulator()
114 String MarkupAccumulator::serializeNodes(Node* targetNode, EChildrenOnly childrenOnly)
116 return serializeNodes(targetNode, childrenOnly, 0);
119 String MarkupAccumulator::serializeNodes(Node* targetNode, EChildrenOnly childrenOnly, Vector<QualifiedName>* tagNamesToSkip)
121 serializeNodesWithNamespaces(targetNode, childrenOnly, 0, tagNamesToSkip);
122 return m_markup.toString();
125 void MarkupAccumulator::serializeNodesWithNamespaces(Node* targetNode, EChildrenOnly childrenOnly, const Namespaces* namespaces, Vector<QualifiedName>* tagNamesToSkip)
127 if (tagNamesToSkip) {
128 for (size_t i = 0; i < tagNamesToSkip->size(); ++i) {
129 if (targetNode->hasTagName(tagNamesToSkip->at(i)))
134 Namespaces namespaceHash;
136 namespaceHash = *namespaces;
139 appendStartTag(targetNode, &namespaceHash);
141 if (!(targetNode->document().isHTMLDocument() && elementCannotHaveEndTag(targetNode))) {
142 Node* current = targetNode->hasTagName(templateTag) ? toHTMLTemplateElement(targetNode)->content()->firstChild() : targetNode->firstChild();
143 for ( ; current; current = current->nextSibling())
144 serializeNodesWithNamespaces(current, IncludeNode, &namespaceHash, tagNamesToSkip);
148 appendEndTag(targetNode);
151 String MarkupAccumulator::resolveURLIfNeeded(const Element* element, const String& urlString) const
153 switch (m_resolveURLsMethod) {
155 return element->document().completeURL(urlString).string();
157 case ResolveNonLocalURLs:
158 if (!element->document().url().isLocalFile())
159 return element->document().completeURL(urlString).string();
162 case DoNotResolveURLs:
168 void MarkupAccumulator::appendString(const String& string)
170 m_markup.append(string);
173 void MarkupAccumulator::appendStartTag(Node* node, Namespaces* namespaces)
175 appendStartMarkup(m_markup, node, namespaces);
177 m_nodes->append(node);
180 void MarkupAccumulator::appendEndTag(Node* node)
182 appendEndMarkup(m_markup, node);
185 size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
188 for (size_t i = 0; i < strings.size(); ++i)
189 length += strings[i].length();
193 void MarkupAccumulator::concatenateMarkup(StringBuilder& result)
195 result.append(m_markup);
198 void MarkupAccumulator::appendAttributeValue(StringBuilder& result, const String& attribute, bool documentIsHTML)
200 appendCharactersReplacingEntities(result, attribute, 0, attribute.length(),
201 documentIsHTML ? EntityMaskInHTMLAttributeValue : EntityMaskInAttributeValue);
204 void MarkupAccumulator::appendCustomAttributes(StringBuilder&, Element*, Namespaces*)
208 void MarkupAccumulator::appendQuotedURLAttributeValue(StringBuilder& result, const Element* element, const Attribute& attribute)
210 ASSERT(element->isURLAttribute(attribute));
211 const String resolvedURLString = resolveURLIfNeeded(element, attribute.value());
212 UChar quoteChar = '"';
213 String strippedURLString = resolvedURLString.stripWhiteSpace();
214 if (protocolIsJavaScript(strippedURLString)) {
215 // minimal escaping for javascript urls
216 if (strippedURLString.contains('"')) {
217 if (strippedURLString.contains('\''))
218 strippedURLString.replaceWithLiteral('"', """);
222 result.append(quoteChar);
223 result.append(strippedURLString);
224 result.append(quoteChar);
228 // FIXME: This does not fully match other browsers. Firefox percent-escapes non-ASCII characters for innerHTML.
229 result.append(quoteChar);
230 appendAttributeValue(result, resolvedURLString, false);
231 result.append(quoteChar);
234 void MarkupAccumulator::appendNodeValue(StringBuilder& result, const Node* node, const Range* range, EntityMask entityMask)
236 const String str = node->nodeValue();
237 unsigned length = str.length();
241 if (node == range->endContainer())
242 length = range->endOffset();
243 if (node == range->startContainer()) {
244 start = range->startOffset();
249 appendCharactersReplacingEntities(result, str, start, length, entityMask);
252 bool MarkupAccumulator::shouldAddNamespaceElement(const Element* element)
254 // Don't add namespace attribute if it is already defined for this elem.
255 const AtomicString& prefix = element->prefix();
256 if (prefix.isEmpty())
257 return !element->hasAttribute(xmlnsAtom);
259 DEFINE_STATIC_LOCAL(String, xmlnsWithColon, ("xmlns:"));
260 return !element->hasAttribute(xmlnsWithColon + prefix);
263 bool MarkupAccumulator::shouldAddNamespaceAttribute(const Attribute& attribute, Namespaces& namespaces)
265 // Don't add namespace attributes twice
266 if (attribute.name() == XMLNSNames::xmlnsAttr) {
267 namespaces.set(emptyAtom.impl(), attribute.value().impl());
271 QualifiedName xmlnsPrefixAttr(xmlnsAtom, attribute.localName(), XMLNSNames::xmlnsNamespaceURI);
272 if (attribute.name() == xmlnsPrefixAttr) {
273 namespaces.set(attribute.localName().impl(), attribute.value().impl());
280 void MarkupAccumulator::appendNamespace(StringBuilder& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces& namespaces)
282 if (namespaceURI.isEmpty())
285 // Use emptyAtoms's impl() for both null and empty strings since the HashMap can't handle 0 as a key
286 StringImpl* pre = prefix.isEmpty() ? emptyAtom.impl() : prefix.impl();
287 StringImpl* foundNS = namespaces.get(pre);
288 if (foundNS != namespaceURI.impl()) {
289 namespaces.set(pre, namespaceURI.impl());
291 result.append(xmlnsAtom.string());
292 if (!prefix.isEmpty()) {
294 result.append(prefix);
299 appendAttributeValue(result, namespaceURI, false);
304 EntityMask MarkupAccumulator::entityMaskForText(Text* text) const
306 const QualifiedName* parentName = 0;
307 if (text->parentElement())
308 parentName = &(text->parentElement())->tagQName();
310 if (parentName && (*parentName == scriptTag || *parentName == styleTag || *parentName == xmpTag))
311 return EntityMaskInCDATA;
313 return text->document().isHTMLDocument() ? EntityMaskInHTMLPCDATA : EntityMaskInPCDATA;
316 void MarkupAccumulator::appendText(StringBuilder& result, Text* text)
318 appendNodeValue(result, text, m_range, entityMaskForText(text));
321 void MarkupAccumulator::appendComment(StringBuilder& result, const String& comment)
323 // FIXME: Comment content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "-->".
324 result.appendLiteral("<!--");
325 result.append(comment);
326 result.appendLiteral("-->");
329 void MarkupAccumulator::appendXMLDeclaration(StringBuilder& result, const Document* document)
331 if (!document->hasXMLDeclaration())
334 result.appendLiteral("<?xml version=\"");
335 result.append(document->xmlVersion());
336 const String& encoding = document->xmlEncoding();
337 if (!encoding.isEmpty()) {
338 result.appendLiteral("\" encoding=\"");
339 result.append(encoding);
341 if (document->xmlStandaloneStatus() != Document::StandaloneUnspecified) {
342 result.appendLiteral("\" standalone=\"");
343 if (document->xmlStandalone())
344 result.appendLiteral("yes");
346 result.appendLiteral("no");
349 result.appendLiteral("\"?>");
352 void MarkupAccumulator::appendDocumentType(StringBuilder& result, const DocumentType* n)
354 if (n->name().isEmpty())
357 result.appendLiteral("<!DOCTYPE ");
358 result.append(n->name());
359 if (!n->publicId().isEmpty()) {
360 result.appendLiteral(" PUBLIC \"");
361 result.append(n->publicId());
363 if (!n->systemId().isEmpty()) {
366 result.append(n->systemId());
369 } else if (!n->systemId().isEmpty()) {
370 result.appendLiteral(" SYSTEM \"");
371 result.append(n->systemId());
374 if (!n->internalSubset().isEmpty()) {
377 result.append(n->internalSubset());
383 void MarkupAccumulator::appendProcessingInstruction(StringBuilder& result, const String& target, const String& data)
385 // FIXME: PI data is not escaped, but XMLSerializer (and possibly other callers) this should raise an exception if it includes "?>".
388 result.append(target);
395 void MarkupAccumulator::appendElement(StringBuilder& result, Element* element, Namespaces* namespaces)
397 appendOpenTag(result, element, namespaces);
399 if (element->hasAttributes()) {
400 unsigned length = element->attributeCount();
401 for (unsigned int i = 0; i < length; i++)
402 appendAttribute(result, element, *element->attributeItem(i), namespaces);
405 // Give an opportunity to subclasses to add their own attributes.
406 appendCustomAttributes(result, element, namespaces);
408 appendCloseTag(result, element);
411 void MarkupAccumulator::appendOpenTag(StringBuilder& result, Element* element, Namespaces* namespaces)
414 result.append(element->nodeNamePreservingCase());
415 if (!element->document().isHTMLDocument() && namespaces && shouldAddNamespaceElement(element))
416 appendNamespace(result, element->prefix(), element->namespaceURI(), *namespaces);
419 void MarkupAccumulator::appendCloseTag(StringBuilder& result, Element* element)
421 if (shouldSelfClose(element)) {
422 if (element->isHTMLElement())
423 result.append(' '); // XHTML 1.0 <-> HTML compatibility.
429 static inline bool attributeIsInSerializedNamespace(const Attribute& attribute)
431 return attribute.namespaceURI() == XMLNames::xmlNamespaceURI
432 || attribute.namespaceURI() == XLinkNames::xlinkNamespaceURI
433 || attribute.namespaceURI() == XMLNSNames::xmlnsNamespaceURI;
436 void MarkupAccumulator::appendAttribute(StringBuilder& result, Element* element, const Attribute& attribute, Namespaces* namespaces)
438 bool documentIsHTML = element->document().isHTMLDocument();
442 if (documentIsHTML && !attributeIsInSerializedNamespace(attribute))
443 result.append(attribute.name().localName());
445 QualifiedName prefixedName = attribute.name();
446 if (attribute.namespaceURI() == XLinkNames::xlinkNamespaceURI) {
447 if (!attribute.prefix())
448 prefixedName.setPrefix(xlinkAtom);
449 } else if (attribute.namespaceURI() == XMLNames::xmlNamespaceURI) {
450 if (!attribute.prefix())
451 prefixedName.setPrefix(xmlAtom);
452 } else if (attribute.namespaceURI() == XMLNSNames::xmlnsNamespaceURI) {
453 if (attribute.name() != XMLNSNames::xmlnsAttr && !attribute.prefix())
454 prefixedName.setPrefix(xmlnsAtom);
456 result.append(prefixedName.toString());
461 if (element->isURLAttribute(attribute))
462 appendQuotedURLAttributeValue(result, element, attribute);
465 appendAttributeValue(result, attribute.value(), documentIsHTML);
469 if (!documentIsHTML && namespaces && shouldAddNamespaceAttribute(attribute, *namespaces))
470 appendNamespace(result, attribute.prefix(), attribute.namespaceURI(), *namespaces);
473 void MarkupAccumulator::appendCDATASection(StringBuilder& result, const String& section)
475 // FIXME: CDATA content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "]]>".
476 result.appendLiteral("<![CDATA[");
477 result.append(section);
478 result.appendLiteral("]]>");
481 void MarkupAccumulator::appendStartMarkup(StringBuilder& result, const Node* node, Namespaces* namespaces)
483 switch (node->nodeType()) {
484 case Node::TEXT_NODE:
485 appendText(result, toText(const_cast<Node*>(node)));
487 case Node::COMMENT_NODE:
488 appendComment(result, toComment(node)->data());
490 case Node::DOCUMENT_NODE:
491 appendXMLDeclaration(result, toDocument(node));
493 case Node::DOCUMENT_FRAGMENT_NODE:
495 case Node::DOCUMENT_TYPE_NODE:
496 appendDocumentType(result, toDocumentType(node));
498 case Node::PROCESSING_INSTRUCTION_NODE:
499 appendProcessingInstruction(result, toProcessingInstruction(node)->target(), toProcessingInstruction(node)->data());
501 case Node::ELEMENT_NODE:
502 appendElement(result, toElement(const_cast<Node*>(node)), namespaces);
504 case Node::CDATA_SECTION_NODE:
505 appendCDATASection(result, toCDATASection(node)->data());
507 case Node::ATTRIBUTE_NODE:
508 case Node::ENTITY_NODE:
509 case Node::NOTATION_NODE:
510 case Node::XPATH_NAMESPACE_NODE:
511 ASSERT_NOT_REACHED();
516 // Rules of self-closure
517 // 1. No elements in HTML documents use the self-closing syntax.
518 // 2. Elements w/ children never self-close because they use a separate end tag.
519 // 3. HTML elements which do not have a "forbidden" end tag will close with a separate end tag.
520 // 4. Other elements self-close.
521 bool MarkupAccumulator::shouldSelfClose(const Node* node)
523 if (node->document().isHTMLDocument())
525 if (node->hasChildNodes())
527 if (node->isHTMLElement() && !elementCannotHaveEndTag(node))
532 bool MarkupAccumulator::elementCannotHaveEndTag(const Node* node)
534 if (!node->isHTMLElement())
537 // FIXME: ieForbidsInsertHTML may not be the right function to call here
538 // ieForbidsInsertHTML is used to disallow setting innerHTML/outerHTML
539 // or createContextualFragment. It does not necessarily align with
540 // which elements should be serialized w/o end tags.
541 return toHTMLElement(node)->ieForbidsInsertHTML();
544 void MarkupAccumulator::appendEndMarkup(StringBuilder& result, const Node* node)
546 if (!node->isElementNode() || shouldSelfClose(node) || (!node->hasChildNodes() && elementCannotHaveEndTag(node)))
551 result.append(toElement(node)->nodeNamePreservingCase());