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

Unified Diff: Source/core/page/PageSerializer.cpp

Issue 1177733002: Merge page serializers [11/12] (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 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/page/PageSerializer.h ('k') | Source/web/tests/PageSerializerTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/page/PageSerializer.cpp
diff --git a/Source/core/page/PageSerializer.cpp b/Source/core/page/PageSerializer.cpp
index a7c6ffd0d36109194c4d0c279731c0e79b946074..f1b339b5aadbf1b9e87270e99f59ce5519fc4885 100644
--- a/Source/core/page/PageSerializer.cpp
+++ b/Source/core/page/PageSerializer.cpp
@@ -98,7 +98,7 @@ static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme
return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::srcAttr;
}
-class SerializerMarkupAccumulator final : public MarkupAccumulator {
+class SerializerMarkupAccumulator : public MarkupAccumulator {
STACK_ALLOCATED();
public:
SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVector<RawPtrWillBeMember<Node>>&);
@@ -112,7 +112,6 @@ protected:
virtual void appendStartTag(Node&, Namespaces* = nullptr) override;
virtual void appendEndTag(const Element&) override;
-private:
PageSerializer* m_serializer;
RawPtrWillBeMember<const Document> m_document;
hajimehoshi 2015/06/11 01:55:32 Create a getter and make member variables private.
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
@@ -203,8 +202,91 @@ void SerializerMarkupAccumulator::appendEndTag(const Element& element)
MarkupAccumulator::appendEndTag(element);
}
-PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr<Delegate> delegate)
+class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator {
+public:
+ LinkChangeSerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVector<RawPtrWillBeMember<Node>>&, LinkLocalPathMap*, String);
+
+protected:
+ virtual void appendElement(StringBuilder&, Element&, Namespaces*) override;
+ virtual void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*) override;
+
+private:
+ // m_replaceLinks include all pair of local resource path and corresponding original link.
+ LinkLocalPathMap* m_replaceLinks;
+ String m_directoryName;
+};
+
+LinkChangeSerializerMarkupAccumulator::LinkChangeSerializerMarkupAccumulator(PageSerializer* serializer, const Document& document, WillBeHeapVector<RawPtrWillBeMember<Node>>& nodes, LinkLocalPathMap* links, String directoryName)
+ : SerializerMarkupAccumulator(serializer, document, nodes)
+ , m_replaceLinks(links)
+ , m_directoryName(directoryName)
+{
+}
+
+void LinkChangeSerializerMarkupAccumulator::appendElement(StringBuilder& result, Element& element, Namespaces* namespaces)
+{
+ // FIXME: We could move the uncommenting to appendOpenTag and appendCloseTag, or just remove it
hajimehoshi 2015/06/11 01:55:33 Use TODO(yourname)
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
+ if (element.hasTagName(HTMLNames::baseTag)) {
+ // Comment the BASE tag when serializing dom.
+ result.appendLiteral("<!--");
hajimehoshi 2015/06/11 01:55:33 Can you avoid appending <base> element instead of
Tiger (Sony Mobile) 2015/06/11 12:23:29 It was done like this in the old serializer, but y
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
+ } else if (element.hasTagName(HTMLNames::htmlTag)) {
+ // Add MOTW (Mark of the Web) declaration before html tag.
+ // See http://msdn2.microsoft.com/en-us/library/ms537628(VS.85).aspx.
+ result.append(String::format("\n<!-- saved from url=(%04d)%s -->\n",
hajimehoshi 2015/06/11 01:55:32 Use MarkupFormatter::appendComment
+ static_cast<int>(m_document->url().string().utf8().length()),
+ m_document->url().string().utf8().data()));
+ }
+
+ SerializerMarkupAccumulator::appendElement(result, element, namespaces);
+
+ if (element.hasTagName(HTMLNames::baseTag)) {
+ // Comment the BASE tag when serializing dom.
+ result.appendLiteral("-->");
+
+ // FIXME: Refactor MarkupAccumulator so it is easier to append an element like this, without special cases for XHTML
hajimehoshi 2015/06/11 01:55:32 Use TODO(yourname)
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
+ // Append a new base tag declaration.
+ result.appendLiteral("<base href=\".\"");
+ if (!m_document->baseTarget().isEmpty()) {
+ result.appendLiteral(" target=\"");
+ result.append(m_document->baseTarget());
hajimehoshi 2015/06/11 01:55:32 Use MarkupFormatter::appendAttributeValue
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
+ result.append('"');
+ }
+ if (m_document->isXHTMLDocument())
+ result.appendLiteral(" />");
+ else
+ result.appendLiteral(">");
+ }
+}
+
+void LinkChangeSerializerMarkupAccumulator::appendAttribute(StringBuilder& result, const Element& element, const Attribute& attribute, Namespaces* namespaces)
+{
+ if (m_replaceLinks && element.isURLAttribute(attribute) && !element.isJavaScriptURLAttribute(attribute)) {
+
+ String completeURL = m_document->completeURL(attribute.value());
+
+ if (m_replaceLinks->contains(completeURL)) {
+ // FIXME: Refactor MarkupAccumulator so it is easier to append an attribute like this.
+ result.append(' ');
+ result.append(attribute.name().toString());
+ result.appendLiteral("=\"");
+ if (!m_directoryName.isEmpty()) {
+ result.appendLiteral("./");
+ result.append(m_directoryName);
+ result.append('/');
+ }
+ result.append(m_replaceLinks->get(completeURL));
hajimehoshi 2015/06/11 01:55:32 Use MarkupFormatter::appendAttributeValue
Tiger (Sony Mobile) 2015/06/11 14:15:10 Done.
+ result.appendLiteral("\"");
+ return;
+ }
+ }
+ MarkupAccumulator::appendAttribute(result, element, attribute, namespaces);
+}
+
+
+PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr<Delegate> delegate, LinkLocalPathMap* urls, String directory)
: m_resources(resources)
+ , m_URLs(urls)
+ , m_directory(directory)
, m_blankFrameCounter(0)
, m_delegate(delegate)
{
@@ -241,8 +323,15 @@ void PageSerializer::serializeFrame(LocalFrame* frame)
}
WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes;
- SerializerMarkupAccumulator accumulator(this, document, serializedNodes);
- String text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNode);
+ String text;
+ if (m_URLs) {
+ LinkChangeSerializerMarkupAccumulator accumulator(this, document, serializedNodes, m_URLs, m_directory);
+ text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNode);
+ } else {
+ SerializerMarkupAccumulator accumulator(this, document, serializedNodes);
+ text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNode);
+ }
+
WTF::TextEncoding textEncoding(document.charset());
CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUnencodables);
m_resources->append(SerializedResource(url, document.suggestedMIMEType(), SharedBuffer::create(frameHTML.data(), frameHTML.length())));
« no previous file with comments | « Source/core/page/PageSerializer.h ('k') | Source/web/tests/PageSerializerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698