| Index: Source/core/editing/MarkupAccumulator.cpp
|
| diff --git a/Source/core/editing/MarkupAccumulator.cpp b/Source/core/editing/MarkupAccumulator.cpp
|
| index dc08c42de75994243bea07922ae34bdc044a2fd5..54a83453dfb28300a923bdca0f88fe060d406a98 100644
|
| --- a/Source/core/editing/MarkupAccumulator.cpp
|
| +++ b/Source/core/editing/MarkupAccumulator.cpp
|
| @@ -47,67 +47,8 @@ namespace blink {
|
|
|
| using namespace HTMLNames;
|
|
|
| -struct EntityDescription {
|
| - UChar entity;
|
| - const CString& reference;
|
| - EntityMask mask;
|
| -};
|
| -
|
| -template <typename CharType>
|
| -static inline void appendCharactersReplacingEntitiesInternal(StringBuilder& result, CharType* text, unsigned length, const EntityDescription entityMaps[], unsigned entityMapsCount, EntityMask entityMask)
|
| -{
|
| - unsigned positionAfterLastEntity = 0;
|
| - for (unsigned i = 0; i < length; ++i) {
|
| - for (unsigned entityIndex = 0; entityIndex < entityMapsCount; ++entityIndex) {
|
| - if (text[i] == entityMaps[entityIndex].entity && entityMaps[entityIndex].mask & entityMask) {
|
| - result.append(text + positionAfterLastEntity, i - positionAfterLastEntity);
|
| - const CString& replacement = entityMaps[entityIndex].reference;
|
| - result.append(replacement.data(), replacement.length());
|
| - positionAfterLastEntity = i + 1;
|
| - break;
|
| - }
|
| - }
|
| - }
|
| - result.append(text + positionAfterLastEntity, length - positionAfterLastEntity);
|
| -}
|
| -
|
| -void MarkupAccumulator::appendCharactersReplacingEntities(StringBuilder& result, const String& source, unsigned offset, unsigned length, EntityMask entityMask)
|
| -{
|
| - DEFINE_STATIC_LOCAL(const CString, ampReference, ("&"));
|
| - DEFINE_STATIC_LOCAL(const CString, ltReference, ("<"));
|
| - DEFINE_STATIC_LOCAL(const CString, gtReference, (">"));
|
| - DEFINE_STATIC_LOCAL(const CString, quotReference, ("""));
|
| - DEFINE_STATIC_LOCAL(const CString, nbspReference, (" "));
|
| -
|
| - static const EntityDescription entityMaps[] = {
|
| - { '&', ampReference, EntityAmp },
|
| - { '<', ltReference, EntityLt },
|
| - { '>', gtReference, EntityGt },
|
| - { '"', quotReference, EntityQuot },
|
| - { noBreakSpaceCharacter, nbspReference, EntityNbsp },
|
| - };
|
| -
|
| - if (!(offset + length))
|
| - return;
|
| -
|
| - ASSERT(offset + length <= source.length());
|
| - if (source.is8Bit())
|
| - appendCharactersReplacingEntitiesInternal(result, source.characters8() + offset, length, entityMaps, WTF_ARRAY_LENGTH(entityMaps), entityMask);
|
| - else
|
| - appendCharactersReplacingEntitiesInternal(result, source.characters16() + offset, length, entityMaps, WTF_ARRAY_LENGTH(entityMaps), entityMask);
|
| -}
|
| -
|
| -size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
|
| -{
|
| - size_t length = 0;
|
| - for (const auto& string : strings)
|
| - length += string.length();
|
| - return length;
|
| -}
|
| -
|
| MarkupAccumulator::MarkupAccumulator(EAbsoluteURLs resolveUrlsMethod, SerializationType serializationType)
|
| - : m_resolveURLsMethod(resolveUrlsMethod)
|
| - , m_serializationType(serializationType)
|
| + : m_formatter(resolveUrlsMethod, serializationType)
|
| {
|
| }
|
|
|
| @@ -115,23 +56,6 @@ MarkupAccumulator::~MarkupAccumulator()
|
| {
|
| }
|
|
|
| -String MarkupAccumulator::resolveURLIfNeeded(const Element& element, const String& urlString) const
|
| -{
|
| - switch (m_resolveURLsMethod) {
|
| - case ResolveAllURLs:
|
| - return element.document().completeURL(urlString).string();
|
| -
|
| - case ResolveNonLocalURLs:
|
| - if (!element.document().url().isLocalFile())
|
| - return element.document().completeURL(urlString).string();
|
| - break;
|
| -
|
| - case DoNotResolveURLs:
|
| - break;
|
| - }
|
| - return urlString;
|
| -}
|
| -
|
| void MarkupAccumulator::appendString(const String& string)
|
| {
|
| m_markup.append(string);
|
| @@ -154,7 +78,7 @@ void MarkupAccumulator::appendStartMarkup(StringBuilder& result, Node& node, Nam
|
| appendText(result, toText(node));
|
| break;
|
| case Node::COMMENT_NODE:
|
| - appendComment(result, toComment(node).data());
|
| + MarkupFormatter::appendComment(result, toComment(node).data());
|
| break;
|
| case Node::DOCUMENT_NODE:
|
| appendXMLDeclaration(result, toDocument(node));
|
| @@ -193,12 +117,7 @@ static bool elementCannotHaveEndTag(const Node& node)
|
|
|
| void MarkupAccumulator::appendEndMarkup(StringBuilder& result, const Element& element)
|
| {
|
| - if (shouldSelfClose(element) || (!element.hasChildren() && elementCannotHaveEndTag(element)))
|
| - return;
|
| -
|
| - result.appendLiteral("</");
|
| - result.append(element.tagQName().toString());
|
| - result.append('>');
|
| + m_formatter.appendEndMarkup(result, element);
|
| }
|
|
|
| void MarkupAccumulator::concatenateMarkup(StringBuilder& result) const
|
| @@ -208,134 +127,36 @@ void MarkupAccumulator::concatenateMarkup(StringBuilder& result) const
|
|
|
| void MarkupAccumulator::appendAttributeValue(StringBuilder& result, const String& attribute, bool documentIsHTML)
|
| {
|
| - appendCharactersReplacingEntities(result, attribute, 0, attribute.length(),
|
| - documentIsHTML ? EntityMaskInHTMLAttributeValue : EntityMaskInAttributeValue);
|
| + m_formatter.appendAttributeValue(result, attribute, documentIsHTML);
|
| }
|
|
|
| void MarkupAccumulator::appendCustomAttributes(StringBuilder&, const Element&, Namespaces*)
|
| {
|
| }
|
|
|
| -void MarkupAccumulator::appendQuotedURLAttributeValue(StringBuilder& result, const Element& element, const Attribute& attribute)
|
| -{
|
| - ASSERT(element.isURLAttribute(attribute));
|
| - const String resolvedURLString = resolveURLIfNeeded(element, attribute.value());
|
| - UChar quoteChar = '"';
|
| - String strippedURLString = resolvedURLString.stripWhiteSpace();
|
| - if (protocolIsJavaScript(strippedURLString)) {
|
| - // minimal escaping for javascript urls
|
| - if (strippedURLString.contains('&'))
|
| - strippedURLString.replaceWithLiteral('&', "&");
|
| -
|
| - if (strippedURLString.contains('"')) {
|
| - if (strippedURLString.contains('\''))
|
| - strippedURLString.replaceWithLiteral('"', """);
|
| - else
|
| - quoteChar = '\'';
|
| - }
|
| - result.append(quoteChar);
|
| - result.append(strippedURLString);
|
| - result.append(quoteChar);
|
| - return;
|
| - }
|
| -
|
| - // FIXME: This does not fully match other browsers. Firefox percent-escapes non-ASCII characters for innerHTML.
|
| - result.append(quoteChar);
|
| - appendAttributeValue(result, resolvedURLString, false);
|
| - result.append(quoteChar);
|
| -}
|
| -
|
| void MarkupAccumulator::appendNamespace(StringBuilder& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces& namespaces)
|
| {
|
| - if (namespaceURI.isEmpty())
|
| - return;
|
| -
|
| - const AtomicString& lookupKey = (!prefix) ? emptyAtom : prefix;
|
| - AtomicString foundURI = namespaces.get(lookupKey);
|
| - if (foundURI != namespaceURI) {
|
| - namespaces.set(lookupKey, namespaceURI);
|
| - result.append(' ');
|
| - result.append(xmlnsAtom.string());
|
| - if (!prefix.isEmpty()) {
|
| - result.append(':');
|
| - result.append(prefix);
|
| - }
|
| -
|
| - result.appendLiteral("=\"");
|
| - appendAttributeValue(result, namespaceURI, false);
|
| - result.append('"');
|
| - }
|
| + m_formatter.appendNamespace(result, prefix, namespaceURI, namespaces);
|
| }
|
|
|
| void MarkupAccumulator::appendText(StringBuilder& result, Text& text)
|
| {
|
| - const String& str = text.data();
|
| - appendCharactersReplacingEntities(result, str, 0, str.length(), entityMaskForText(text));
|
| -}
|
| -
|
| -void MarkupAccumulator::appendComment(StringBuilder& result, const String& comment)
|
| -{
|
| - // FIXME: Comment content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "-->".
|
| - result.appendLiteral("<!--");
|
| - result.append(comment);
|
| - result.appendLiteral("-->");
|
| + m_formatter.appendText(result, text);
|
| }
|
|
|
| void MarkupAccumulator::appendXMLDeclaration(StringBuilder& result, const Document& document)
|
| {
|
| - if (!document.hasXMLDeclaration())
|
| - return;
|
| -
|
| - result.appendLiteral("<?xml version=\"");
|
| - result.append(document.xmlVersion());
|
| - const String& encoding = document.xmlEncoding();
|
| - if (!encoding.isEmpty()) {
|
| - result.appendLiteral("\" encoding=\"");
|
| - result.append(encoding);
|
| - }
|
| - if (document.xmlStandaloneStatus() != Document::StandaloneUnspecified) {
|
| - result.appendLiteral("\" standalone=\"");
|
| - if (document.xmlStandalone())
|
| - result.appendLiteral("yes");
|
| - else
|
| - result.appendLiteral("no");
|
| - }
|
| -
|
| - result.appendLiteral("\"?>");
|
| + m_formatter.appendXMLDeclaration(result, document);
|
| }
|
|
|
| void MarkupAccumulator::appendDocumentType(StringBuilder& result, const DocumentType& n)
|
| {
|
| - if (n.name().isEmpty())
|
| - return;
|
| -
|
| - result.appendLiteral("<!DOCTYPE ");
|
| - result.append(n.name());
|
| - if (!n.publicId().isEmpty()) {
|
| - result.appendLiteral(" PUBLIC \"");
|
| - result.append(n.publicId());
|
| - result.append('"');
|
| - if (!n.systemId().isEmpty()) {
|
| - result.appendLiteral(" \"");
|
| - result.append(n.systemId());
|
| - result.append('"');
|
| - }
|
| - } else if (!n.systemId().isEmpty()) {
|
| - result.appendLiteral(" SYSTEM \"");
|
| - result.append(n.systemId());
|
| - result.append('"');
|
| - }
|
| - result.append('>');
|
| + m_formatter.appendDocumentType(result, n);
|
| }
|
|
|
| void MarkupAccumulator::appendProcessingInstruction(StringBuilder& result, const String& target, const String& data)
|
| {
|
| - // FIXME: PI data is not escaped, but XMLSerializer (and possibly other callers) this should raise an exception if it includes "?>".
|
| - result.appendLiteral("<?");
|
| - result.append(target);
|
| - result.append(' ');
|
| - result.append(data);
|
| - result.appendLiteral("?>");
|
| + m_formatter.appendProcessingInstruction(result, target, data);
|
| }
|
|
|
| bool MarkupAccumulator::shouldIgnoreAttribute(const Attribute& attribute)
|
| @@ -361,162 +182,47 @@ void MarkupAccumulator::appendElement(StringBuilder& result, Element& element, N
|
|
|
| void MarkupAccumulator::appendOpenTag(StringBuilder& result, const Element& element, Namespaces* namespaces)
|
| {
|
| - result.append('<');
|
| - result.append(element.tagQName().toString());
|
| - if (!serializeAsHTMLDocument(element) && namespaces && shouldAddNamespaceElement(element, *namespaces))
|
| - appendNamespace(result, element.prefix(), element.namespaceURI(), *namespaces);
|
| + m_formatter.appendOpenTag(result, element, namespaces);
|
| }
|
|
|
| void MarkupAccumulator::appendCloseTag(StringBuilder& result, const Element& element)
|
| {
|
| - if (shouldSelfClose(element)) {
|
| - if (element.isHTMLElement())
|
| - result.append(' '); // XHTML 1.0 <-> HTML compatibility.
|
| - result.append('/');
|
| - }
|
| - result.append('>');
|
| -}
|
| -
|
| -static inline bool attributeIsInSerializedNamespace(const Attribute& attribute)
|
| -{
|
| - return attribute.namespaceURI() == XMLNames::xmlNamespaceURI
|
| - || attribute.namespaceURI() == XLinkNames::xlinkNamespaceURI
|
| - || attribute.namespaceURI() == XMLNSNames::xmlnsNamespaceURI;
|
| + m_formatter.appendCloseTag(result, element);
|
| }
|
|
|
| void MarkupAccumulator::appendAttribute(StringBuilder& result, const Element& element, const Attribute& attribute, Namespaces* namespaces)
|
| {
|
| - bool documentIsHTML = serializeAsHTMLDocument(element);
|
| -
|
| - QualifiedName prefixedName = attribute.name();
|
| - if (documentIsHTML && !attributeIsInSerializedNamespace(attribute)) {
|
| - result.append(' ');
|
| - result.append(attribute.name().localName());
|
| - } else {
|
| - if (attribute.namespaceURI() == XMLNSNames::xmlnsNamespaceURI) {
|
| - if (!attribute.prefix() && attribute.localName() != xmlnsAtom)
|
| - prefixedName.setPrefix(xmlnsAtom);
|
| - if (namespaces) { // Account for the namespace attribute we're about to append.
|
| - const AtomicString& lookupKey = (!attribute.prefix()) ? emptyAtom : attribute.localName();
|
| - namespaces->set(lookupKey, attribute.value());
|
| - }
|
| - } else if (attribute.namespaceURI() == XMLNames::xmlNamespaceURI) {
|
| - if (!attribute.prefix())
|
| - prefixedName.setPrefix(xmlAtom);
|
| - } else {
|
| - if (attribute.namespaceURI() == XLinkNames::xlinkNamespaceURI) {
|
| - if (!attribute.prefix())
|
| - prefixedName.setPrefix(xlinkAtom);
|
| - }
|
| -
|
| - if (namespaces && shouldAddNamespaceAttribute(attribute, element)) {
|
| - if (!prefixedName.prefix()) {
|
| - // This behavior is in process of being standardized. See crbug.com/248044 and https://www.w3.org/Bugs/Public/show_bug.cgi?id=24208
|
| - String prefixPrefix("ns", 2);
|
| - for (unsigned i = attribute.namespaceURI().impl()->existingHash(); ; ++i) {
|
| - AtomicString newPrefix(String(prefixPrefix + String::number(i)));
|
| - AtomicString foundURI = namespaces->get(newPrefix);
|
| - if (foundURI == attribute.namespaceURI() || foundURI == nullAtom) {
|
| - // We already generated a prefix for this namespace.
|
| - prefixedName.setPrefix(newPrefix);
|
| - break;
|
| - }
|
| - }
|
| - }
|
| - ASSERT(prefixedName.prefix());
|
| - appendNamespace(result, prefixedName.prefix(), attribute.namespaceURI(), *namespaces);
|
| - }
|
| - }
|
| - result.append(' ');
|
| - result.append(prefixedName.toString());
|
| - }
|
| -
|
| - result.append('=');
|
| -
|
| - if (element.isURLAttribute(attribute)) {
|
| - appendQuotedURLAttributeValue(result, element, attribute);
|
| - } else {
|
| - result.append('"');
|
| - appendAttributeValue(result, attribute.value(), documentIsHTML);
|
| - result.append('"');
|
| - }
|
| + m_formatter.appendAttribute(result, element, attribute, namespaces);
|
| }
|
|
|
| void MarkupAccumulator::appendCDATASection(StringBuilder& result, const String& section)
|
| {
|
| - // FIXME: CDATA content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "]]>".
|
| - result.appendLiteral("<![CDATA[");
|
| - result.append(section);
|
| - result.appendLiteral("]]>");
|
| + m_formatter.appendCDATASection(result, section);
|
| }
|
|
|
| bool MarkupAccumulator::shouldAddNamespaceElement(const Element& element, Namespaces& namespaces) const
|
| {
|
| - // Don't add namespace attribute if it is already defined for this elem.
|
| - const AtomicString& prefix = element.prefix();
|
| - if (prefix.isEmpty()) {
|
| - if (element.hasAttribute(xmlnsAtom)) {
|
| - namespaces.set(emptyAtom, element.namespaceURI());
|
| - return false;
|
| - }
|
| - return true;
|
| - }
|
| -
|
| - return !element.hasAttribute(WTF::xmlnsWithColon + prefix);
|
| + return m_formatter.shouldAddNamespaceElement(element, namespaces);
|
| }
|
|
|
| bool MarkupAccumulator::shouldAddNamespaceAttribute(const Attribute& attribute, const Element& element) const
|
| {
|
| - // xmlns and xmlns:prefix attributes should be handled by another branch in appendAttribute.
|
| - ASSERT(attribute.namespaceURI() != XMLNSNames::xmlnsNamespaceURI);
|
| -
|
| - // Attributes are in the null namespace by default.
|
| - if (!attribute.namespaceURI())
|
| - return false;
|
| -
|
| - // Attributes without a prefix will need one generated for them, and an xmlns attribute for that prefix.
|
| - if (!attribute.prefix())
|
| - return true;
|
| -
|
| - return !element.hasAttribute(WTF::xmlnsWithColon + attribute.prefix());
|
| + return m_formatter.shouldAddNamespaceAttribute(attribute, element);
|
| }
|
|
|
| EntityMask MarkupAccumulator::entityMaskForText(const Text& text) const
|
| {
|
| - if (!serializeAsHTMLDocument(text))
|
| - return EntityMaskInPCDATA;
|
| -
|
| - // TODO(hajimehoshi): We need to switch EditingStrategy.
|
| - const QualifiedName* parentName = nullptr;
|
| - if (text.parentElement())
|
| - parentName = &(text.parentElement())->tagQName();
|
| -
|
| - if (parentName && (*parentName == scriptTag || *parentName == styleTag || *parentName == xmpTag))
|
| - return EntityMaskInCDATA;
|
| - return EntityMaskInHTMLPCDATA;
|
| + return m_formatter.entityMaskForText(text);
|
| }
|
|
|
| -// Rules of self-closure
|
| -// 1. No elements in HTML documents use the self-closing syntax.
|
| -// 2. Elements w/ children never self-close because they use a separate end tag.
|
| -// 3. HTML elements which do not have a "forbidden" end tag will close with a separate end tag.
|
| -// 4. Other elements self-close.
|
| bool MarkupAccumulator::shouldSelfClose(const Element& element) const
|
| {
|
| - if (serializeAsHTMLDocument(element))
|
| - return false;
|
| - if (element.hasChildren())
|
| - return false;
|
| - if (element.isHTMLElement() && !elementCannotHaveEndTag(element))
|
| - return false;
|
| - return true;
|
| + return m_formatter.shouldSelfClose(element);
|
| }
|
|
|
| bool MarkupAccumulator::serializeAsHTMLDocument(const Node& node) const
|
| {
|
| - if (m_serializationType == SerializationType::ForcedXML)
|
| - return false;
|
| - return node.document().isHTMLDocument();
|
| + return m_formatter.serializeAsHTMLDocument(node);
|
| }
|
|
|
| template<typename Strategy>
|
|
|