Skip to content

Commit

Permalink
Move namespace handling code in MarkupFormatter::AppendOpenTag()
Browse files Browse the repository at this point in the history
Move namespace handling code in MarkupFormatter::AppendOpenTag() to
MarkupAccumulator::AppendOpenTag(), and move
MarkupFormatter::ShouldAddNamespaceElement() to MarkupAccumulator.

MarkupFormatter::AppendOpenTag() is used by MakupAccumulator and
StyledMarkupAccumulator, however only MarkupAccumulator needs the
namespace handling.

This CL doesn't change any behavior.

Bug: 927166
Change-Id: I941a6c4dc1154fdbd57fd7f48bae0bd1cda33b18
Reviewed-on: https://chromium-review.googlesource.com/c/1448083
Commit-Queue: Kent Tamura <[email protected]>
Commit-Queue: Yoshifumi Inoue <[email protected]>
Auto-Submit: Kent Tamura <[email protected]>
Reviewed-by: Yoshifumi Inoue <[email protected]>
Cr-Commit-Position: refs/heads/master@{#628214}
  • Loading branch information
tkent-google authored and Commit Bot committed Feb 1, 2019
1 parent ecc3b41 commit 38fc330
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ void MarkupAccumulator::AppendElement(StringBuilder& result,
void MarkupAccumulator::AppendOpenTag(StringBuilder& result,
const Element& element,
Namespaces* namespaces) {
formatter_.AppendOpenTag(result, element, namespaces);
formatter_.AppendOpenTag(result, element);
if (!SerializeAsHTMLDocument(element) && namespaces &&
ShouldAddNamespaceElement(element, *namespaces)) {
MarkupFormatter::AppendNamespace(result, element.prefix(),
element.namespaceURI(), *namespaces);
}
}

void MarkupAccumulator::AppendCloseTag(StringBuilder& result,
Expand All @@ -161,6 +166,22 @@ bool MarkupAccumulator::SerializeAsHTMLDocument(const Node& node) const {
return formatter_.SerializeAsHTMLDocument(node);
}

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(g_xmlns_atom)) {
namespaces.Set(g_empty_atom, element.namespaceURI());
return false;
}
return true;
}

return !element.hasAttribute(WTF::g_xmlns_with_colon + prefix);
}

std::pair<Node*, Element*> MarkupAccumulator::GetAuxiliaryDOMTree(
const Element& element) const {
return std::pair<Node*, Element*>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class MarkupAccumulator {
virtual std::pair<Node*, Element*> GetAuxiliaryDOMTree(const Element&) const;

private:
bool ShouldAddNamespaceElement(const Element&, Namespaces&) const;

MarkupFormatter formatter_;
StringBuilder markup_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,9 @@ void MarkupFormatter::AppendProcessingInstruction(StringBuilder& result,
}

void MarkupFormatter::AppendOpenTag(StringBuilder& result,
const Element& element,
Namespaces* namespaces) {
const Element& element) {
result.Append('<');
result.Append(element.TagQName().ToString());
if (!SerializeAsHTMLDocument(element) && namespaces &&
ShouldAddNamespaceElement(element, *namespaces))
AppendNamespace(result, element.prefix(), element.namespaceURI(),
*namespaces);
}

void MarkupFormatter::AppendCloseTag(StringBuilder& result,
Expand Down Expand Up @@ -428,21 +423,6 @@ void MarkupFormatter::AppendCDATASection(StringBuilder& result,
result.Append("]]>");
}

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();
if (prefix.IsEmpty()) {
if (element.hasAttribute(g_xmlns_atom)) {
namespaces.Set(g_empty_atom, element.namespaceURI());
return false;
}
return true;
}

return !element.hasAttribute(WTF::g_xmlns_with_colon + prefix);
}

bool MarkupFormatter::ShouldAddNamespaceAttribute(
const Attribute& attribute,
const Element& element) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,13 @@ class MarkupFormatter final {
bool SerializeAsHTMLDocument(const Node&) const;

void AppendText(StringBuilder&, Text&);
void AppendOpenTag(StringBuilder&, const Element&, Namespaces*);
void AppendOpenTag(StringBuilder&, const Element&);
void AppendCloseTag(StringBuilder&, const Element&);
void AppendAttribute(StringBuilder&,
const Element&,
const Attribute&,
Namespaces*);

bool ShouldAddNamespaceElement(const Element&, Namespaces&) const;
bool ShouldAddNamespaceAttribute(const Attribute&, const Element&) const;
EntityMask EntityMaskForText(const Text&) const;
bool ShouldSelfClose(const Element&) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void StyledMarkupAccumulator::AppendElementWithInlineStyle(
const Element& element,
EditingStyle* style) {
const bool document_is_html = element.GetDocument().IsHTMLDocument();
formatter_.AppendOpenTag(out, element, nullptr);
formatter_.AppendOpenTag(out, element);
AttributeCollection attributes = element.Attributes();
for (const auto& attribute : attributes) {
// We'll handle the style attribute separately, below.
Expand All @@ -162,7 +162,7 @@ void StyledMarkupAccumulator::AppendElement(const Element& element) {

void StyledMarkupAccumulator::AppendElement(StringBuilder& out,
const Element& element) {
formatter_.AppendOpenTag(out, element, nullptr);
formatter_.AppendOpenTag(out, element);
AttributeCollection attributes = element.Attributes();
for (const auto& attribute : attributes)
formatter_.AppendAttribute(out, element, attribute, nullptr);
Expand Down

0 comments on commit 38fc330

Please sign in to comment.