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

Unified Diff: Source/core/editing/MarkupFormatter.cpp

Issue 1148633010: Refactoring: Separate MarkupAccumulator into MarkupFormatter and MarkupAccumulator (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/editing/MarkupFormatter.h ('k') | Source/core/editing/StyledMarkupAccumulator.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/editing/MarkupFormatter.cpp
diff --git a/Source/core/editing/MarkupAccumulator.cpp b/Source/core/editing/MarkupFormatter.cpp
similarity index 70%
copy from Source/core/editing/MarkupAccumulator.cpp
copy to Source/core/editing/MarkupFormatter.cpp
index dc08c42de75994243bea07922ae34bdc044a2fd5..66add9d955fd18f6ec83a828c7af20675c899fa3 100644
--- a/Source/core/editing/MarkupAccumulator.cpp
+++ b/Source/core/editing/MarkupFormatter.cpp
@@ -25,7 +25,7 @@
*/
#include "config.h"
-#include "core/editing/MarkupAccumulator.h"
+#include "core/editing/MarkupFormatter.h"
#include "core/HTMLNames.h"
#include "core/XLinkNames.h"
@@ -71,7 +71,7 @@ static inline void appendCharactersReplacingEntitiesInternal(StringBuilder& resu
result.append(text + positionAfterLastEntity, length - positionAfterLastEntity);
}
-void MarkupAccumulator::appendCharactersReplacingEntities(StringBuilder& result, const String& source, unsigned offset, unsigned length, EntityMask entityMask)
+void MarkupFormatter::appendCharactersReplacingEntities(StringBuilder& result, const String& source, unsigned offset, unsigned length, EntityMask entityMask)
{
DEFINE_STATIC_LOCAL(const CString, ampReference, ("&"));
DEFINE_STATIC_LOCAL(const CString, ltReference, ("<"));
@@ -97,7 +97,7 @@ void MarkupAccumulator::appendCharactersReplacingEntities(StringBuilder& result,
appendCharactersReplacingEntitiesInternal(result, source.characters16() + offset, length, entityMaps, WTF_ARRAY_LENGTH(entityMaps), entityMask);
}
-size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
+size_t MarkupFormatter::totalLength(const Vector<String>& strings)
{
size_t length = 0;
for (const auto& string : strings)
@@ -105,17 +105,17 @@ size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
return length;
}
-MarkupAccumulator::MarkupAccumulator(EAbsoluteURLs resolveUrlsMethod, SerializationType serializationType)
+MarkupFormatter::MarkupFormatter(EAbsoluteURLs resolveUrlsMethod, SerializationType serializationType)
: m_resolveURLsMethod(resolveUrlsMethod)
, m_serializationType(serializationType)
{
}
-MarkupAccumulator::~MarkupAccumulator()
+MarkupFormatter::~MarkupFormatter()
{
}
-String MarkupAccumulator::resolveURLIfNeeded(const Element& element, const String& urlString) const
+String MarkupFormatter::resolveURLIfNeeded(const Element& element, const String& urlString) const
{
switch (m_resolveURLsMethod) {
case ResolveAllURLs:
@@ -132,53 +132,6 @@ String MarkupAccumulator::resolveURLIfNeeded(const Element& element, const Strin
return urlString;
}
-void MarkupAccumulator::appendString(const String& string)
-{
- m_markup.append(string);
-}
-
-void MarkupAccumulator::appendStartTag(Node& node, Namespaces* namespaces)
-{
- appendStartMarkup(m_markup, node, namespaces);
-}
-
-void MarkupAccumulator::appendEndTag(const Element& element)
-{
- appendEndMarkup(m_markup, element);
-}
-
-void MarkupAccumulator::appendStartMarkup(StringBuilder& result, Node& node, Namespaces* namespaces)
-{
- switch (node.nodeType()) {
- case Node::TEXT_NODE:
- appendText(result, toText(node));
- break;
- case Node::COMMENT_NODE:
- appendComment(result, toComment(node).data());
- break;
- case Node::DOCUMENT_NODE:
- appendXMLDeclaration(result, toDocument(node));
- break;
- case Node::DOCUMENT_FRAGMENT_NODE:
- break;
- case Node::DOCUMENT_TYPE_NODE:
- appendDocumentType(result, toDocumentType(node));
- break;
- case Node::PROCESSING_INSTRUCTION_NODE:
- appendProcessingInstruction(result, toProcessingInstruction(node).target(), toProcessingInstruction(node).data());
- break;
- case Node::ELEMENT_NODE:
- appendElement(result, toElement(node), namespaces);
- break;
- case Node::CDATA_SECTION_NODE:
- appendCDATASection(result, toCDATASection(node).data());
- break;
- case Node::ATTRIBUTE_NODE:
- ASSERT_NOT_REACHED();
- break;
- }
-}
-
static bool elementCannotHaveEndTag(const Node& node)
{
if (!node.isHTMLElement())
@@ -191,7 +144,7 @@ static bool elementCannotHaveEndTag(const Node& node)
return toHTMLElement(node).ieForbidsInsertHTML();
}
-void MarkupAccumulator::appendEndMarkup(StringBuilder& result, const Element& element)
+void MarkupFormatter::appendEndMarkup(StringBuilder& result, const Element& element)
{
if (shouldSelfClose(element) || (!element.hasChildren() && elementCannotHaveEndTag(element)))
return;
@@ -201,22 +154,13 @@ void MarkupAccumulator::appendEndMarkup(StringBuilder& result, const Element& el
result.append('>');
}
-void MarkupAccumulator::concatenateMarkup(StringBuilder& result) const
-{
- result.append(m_markup);
-}
-
-void MarkupAccumulator::appendAttributeValue(StringBuilder& result, const String& attribute, bool documentIsHTML)
+void MarkupFormatter::appendAttributeValue(StringBuilder& result, const String& attribute, bool documentIsHTML)
{
appendCharactersReplacingEntities(result, attribute, 0, attribute.length(),
documentIsHTML ? EntityMaskInHTMLAttributeValue : EntityMaskInAttributeValue);
}
-void MarkupAccumulator::appendCustomAttributes(StringBuilder&, const Element&, Namespaces*)
-{
-}
-
-void MarkupAccumulator::appendQuotedURLAttributeValue(StringBuilder& result, const Element& element, const Attribute& attribute)
+void MarkupFormatter::appendQuotedURLAttributeValue(StringBuilder& result, const Element& element, const Attribute& attribute)
{
ASSERT(element.isURLAttribute(attribute));
const String resolvedURLString = resolveURLIfNeeded(element, attribute.value());
@@ -245,7 +189,7 @@ void MarkupAccumulator::appendQuotedURLAttributeValue(StringBuilder& result, con
result.append(quoteChar);
}
-void MarkupAccumulator::appendNamespace(StringBuilder& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces& namespaces)
+void MarkupFormatter::appendNamespace(StringBuilder& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces& namespaces)
{
if (namespaceURI.isEmpty())
return;
@@ -267,13 +211,13 @@ void MarkupAccumulator::appendNamespace(StringBuilder& result, const AtomicStrin
}
}
-void MarkupAccumulator::appendText(StringBuilder& result, Text& text)
+void MarkupFormatter::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)
+void MarkupFormatter::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("<!--");
@@ -281,7 +225,7 @@ void MarkupAccumulator::appendComment(StringBuilder& result, const String& comme
result.appendLiteral("-->");
}
-void MarkupAccumulator::appendXMLDeclaration(StringBuilder& result, const Document& document)
+void MarkupFormatter::appendXMLDeclaration(StringBuilder& result, const Document& document)
{
if (!document.hasXMLDeclaration())
return;
@@ -304,7 +248,7 @@ void MarkupAccumulator::appendXMLDeclaration(StringBuilder& result, const Docume
result.appendLiteral("\"?>");
}
-void MarkupAccumulator::appendDocumentType(StringBuilder& result, const DocumentType& n)
+void MarkupFormatter::appendDocumentType(StringBuilder& result, const DocumentType& n)
{
if (n.name().isEmpty())
return;
@@ -328,7 +272,7 @@ void MarkupAccumulator::appendDocumentType(StringBuilder& result, const Document
result.append('>');
}
-void MarkupAccumulator::appendProcessingInstruction(StringBuilder& result, const String& target, const String& data)
+void MarkupFormatter::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("<?");
@@ -338,28 +282,7 @@ void MarkupAccumulator::appendProcessingInstruction(StringBuilder& result, const
result.appendLiteral("?>");
}
-bool MarkupAccumulator::shouldIgnoreAttribute(const Attribute& attribute)
-{
- return false;
-}
-
-void MarkupAccumulator::appendElement(StringBuilder& result, Element& element, Namespaces* namespaces)
-{
- appendOpenTag(result, element, namespaces);
-
- AttributeCollection attributes = element.attributes();
- for (const auto& attribute : attributes) {
- if (!shouldIgnoreAttribute(attribute))
- appendAttribute(result, element, attribute, namespaces);
- }
-
- // Give an opportunity to subclasses to add their own attributes.
- appendCustomAttributes(result, element, namespaces);
-
- appendCloseTag(result, element);
-}
-
-void MarkupAccumulator::appendOpenTag(StringBuilder& result, const Element& element, Namespaces* namespaces)
+void MarkupFormatter::appendOpenTag(StringBuilder& result, const Element& element, Namespaces* namespaces)
{
result.append('<');
result.append(element.tagQName().toString());
@@ -367,7 +290,7 @@ void MarkupAccumulator::appendOpenTag(StringBuilder& result, const Element& elem
appendNamespace(result, element.prefix(), element.namespaceURI(), *namespaces);
}
-void MarkupAccumulator::appendCloseTag(StringBuilder& result, const Element& element)
+void MarkupFormatter::appendCloseTag(StringBuilder& result, const Element& element)
{
if (shouldSelfClose(element)) {
if (element.isHTMLElement())
@@ -384,7 +307,7 @@ static inline bool attributeIsInSerializedNamespace(const Attribute& attribute)
|| attribute.namespaceURI() == XMLNSNames::xmlnsNamespaceURI;
}
-void MarkupAccumulator::appendAttribute(StringBuilder& result, const Element& element, const Attribute& attribute, Namespaces* namespaces)
+void MarkupFormatter::appendAttribute(StringBuilder& result, const Element& element, const Attribute& attribute, Namespaces* namespaces)
{
bool documentIsHTML = serializeAsHTMLDocument(element);
@@ -442,7 +365,7 @@ void MarkupAccumulator::appendAttribute(StringBuilder& result, const Element& el
}
}
-void MarkupAccumulator::appendCDATASection(StringBuilder& result, const String& section)
+void MarkupFormatter::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[");
@@ -450,7 +373,7 @@ void MarkupAccumulator::appendCDATASection(StringBuilder& result, const String&
result.appendLiteral("]]>");
}
-bool MarkupAccumulator::shouldAddNamespaceElement(const Element& element, Namespaces& namespaces) const
+bool MarkupFormatter::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();
@@ -465,7 +388,7 @@ bool MarkupAccumulator::shouldAddNamespaceElement(const Element& element, Namesp
return !element.hasAttribute(WTF::xmlnsWithColon + prefix);
}
-bool MarkupAccumulator::shouldAddNamespaceAttribute(const Attribute& attribute, const Element& element) const
+bool MarkupFormatter::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);
@@ -481,7 +404,7 @@ bool MarkupAccumulator::shouldAddNamespaceAttribute(const Attribute& attribute,
return !element.hasAttribute(WTF::xmlnsWithColon + attribute.prefix());
}
-EntityMask MarkupAccumulator::entityMaskForText(const Text& text) const
+EntityMask MarkupFormatter::entityMaskForText(const Text& text) const
{
if (!serializeAsHTMLDocument(text))
return EntityMaskInPCDATA;
@@ -501,7 +424,7 @@ EntityMask MarkupAccumulator::entityMaskForText(const Text& text) const
// 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
+bool MarkupFormatter::shouldSelfClose(const Element& element) const
{
if (serializeAsHTMLDocument(element))
return false;
@@ -512,48 +435,11 @@ bool MarkupAccumulator::shouldSelfClose(const Element& element) const
return true;
}
-bool MarkupAccumulator::serializeAsHTMLDocument(const Node& node) const
+bool MarkupFormatter::serializeAsHTMLDocument(const Node& node) const
{
if (m_serializationType == SerializationType::ForcedXML)
return false;
return node.document().isHTMLDocument();
}
-template<typename Strategy>
-static void serializeNodesWithNamespaces(MarkupAccumulator& accumulator, Node& targetNode, EChildrenOnly childrenOnly, const Namespaces* namespaces)
-{
- Namespaces namespaceHash;
- if (namespaces)
- namespaceHash = *namespaces;
-
- if (!childrenOnly)
- accumulator.appendStartTag(targetNode, &namespaceHash);
-
- if (!(accumulator.serializeAsHTMLDocument(targetNode) && elementCannotHaveEndTag(targetNode))) {
- Node* current = isHTMLTemplateElement(targetNode) ? Strategy::firstChild(*toHTMLTemplateElement(targetNode).content()) : Strategy::firstChild(targetNode);
- for ( ; current; current = Strategy::nextSibling(*current))
- serializeNodesWithNamespaces<Strategy>(accumulator, *current, IncludeNode, &namespaceHash);
- }
-
- if (!childrenOnly && targetNode.isElementNode())
- accumulator.appendEndTag(toElement(targetNode));
-}
-
-template<typename Strategy>
-String serializeNodes(MarkupAccumulator& accumulator, Node& targetNode, EChildrenOnly childrenOnly)
-{
- Namespaces* namespaces = nullptr;
- Namespaces namespaceHash;
- if (!accumulator.serializeAsHTMLDocument(targetNode)) {
- // Add pre-bound namespaces for XML fragments.
- namespaceHash.set(xmlAtom, XMLNames::xmlNamespaceURI);
- namespaces = &namespaceHash;
- }
-
- serializeNodesWithNamespaces<Strategy>(accumulator, targetNode, childrenOnly, namespaces);
- return accumulator.toString();
-}
-
-template String serializeNodes<EditingStrategy>(MarkupAccumulator&, Node&, EChildrenOnly);
-
}
« no previous file with comments | « Source/core/editing/MarkupFormatter.h ('k') | Source/core/editing/StyledMarkupAccumulator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698