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

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: More issues 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
Index: Source/core/page/PageSerializer.cpp
diff --git a/Source/core/page/PageSerializer.cpp b/Source/core/page/PageSerializer.cpp
index a7c6ffd0d36109194c4d0c279731c0e79b946074..00a63a1fa4af52115b5d66e7d28fefa1a60339c4 100644
--- a/Source/core/page/PageSerializer.cpp
+++ b/Source/core/page/PageSerializer.cpp
@@ -87,18 +87,13 @@ static bool isCharsetSpecifyingNode(const Node& node)
return textEncoding.isValid();
}
-static bool shouldIgnoreElement(const Element& element)
-{
- return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isCharsetSpecifyingNode(element);
-}
-
static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerElement& frameOwner)
{
// FIXME: We should support all frame owners including applets.
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,6 +107,11 @@ protected:
virtual void appendStartTag(Node&, Namespaces* = nullptr) override;
virtual void appendEndTag(const Element&) override;
+ virtual bool shouldIgnoreElement(const Element&) const;
+
+ PageSerializer* pageSerializer();
+ const Document& document();
+
private:
PageSerializer* m_serializer;
RawPtrWillBeMember<const Document> m_document;
@@ -203,6 +203,101 @@ void SerializerMarkupAccumulator::appendEndTag(const Element& element)
MarkupAccumulator::appendEndTag(element);
}
+bool SerializerMarkupAccumulator::shouldIgnoreElement(const Element& element) const
+{
+ return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isCharsetSpecifyingNode(element);
+}
+
+PageSerializer* SerializerMarkupAccumulator::pageSerializer()
+{
+ return m_serializer;
+}
+
+const Document& SerializerMarkupAccumulator::document()
+{
+ return *m_document;
+}
+
+class LinkChangeSerializerMarkupAccumulator final : public SerializerMarkupAccumulator {
+public:
+ LinkChangeSerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVector<RawPtrWillBeMember<Node>>&, HashMap<String, String>&, String&);
yosin_UTC9 2015/06/18 06:45:46 |const String&| for the last parameter? I think it
Tiger (Sony Mobile) 2015/06/24 22:29:23 Done.
+
+private:
+ void appendElement(StringBuilder&, Element&, Namespaces*) override;
+ void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*) override;
+
+ bool shouldIgnoreElement(const Element&) const override;
+
+ // m_rewriteURLs include all pair of local resource path and corresponding original link.
+ HashMap<String, String> m_rewriteURLs;
+ String m_rewriteFolder;
+};
+
+LinkChangeSerializerMarkupAccumulator::LinkChangeSerializerMarkupAccumulator(PageSerializer* serializer, const Document& document, WillBeHeapVector<RawPtrWillBeMember<Node>>& nodes, HashMap<String, String>& rewriteURLs, String& rewriteFolder)
+ : SerializerMarkupAccumulator(serializer, document, nodes)
+ , m_rewriteURLs(rewriteURLs)
+ , m_rewriteFolder(rewriteFolder)
+{
+}
+
+void LinkChangeSerializerMarkupAccumulator::appendElement(StringBuilder& result, Element& element, Namespaces* namespaces)
+{
+ 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('\n');
+ MarkupFormatter::appendComment(result, String::format(" saved from url=(%04d)%s ",
+ static_cast<int>(document().url().string().utf8().length()),
+ document().url().string().utf8().data()));
+ result.append('\n');
+ }
+
+ SerializerMarkupAccumulator::appendElement(result, element, namespaces);
+
+ if (element.hasTagName(HTMLNames::baseTag)) {
+ // TODO(tiger): Refactor MarkupAccumulator so it is easier to append an element like this, without special cases for XHTML
+ // Append a new base tag declaration.
+ result.appendLiteral("<base href=\".\"");
+ if (!document().baseTarget().isEmpty()) {
+ result.appendLiteral(" target=\"");
+ MarkupFormatter::appendAttributeValue(result, document().baseTarget(), document().isHTMLDocument());
+ result.append('"');
+ }
+ if (document().isXHTMLDocument())
+ result.appendLiteral(" />");
+ else
+ result.appendLiteral(">");
+ }
+}
+
+void LinkChangeSerializerMarkupAccumulator::appendAttribute(StringBuilder& result, const Element& element, const Attribute& attribute, Namespaces* namespaces)
+{
+ if (!m_rewriteURLs.isEmpty() && element.isURLAttribute(attribute) && !element.isJavaScriptURLAttribute(attribute)) {
+
+ String completeURL = document().completeURL(attribute.value());
+
+ if (m_rewriteURLs.contains(completeURL)) {
+ // TODO(tiger): Refactor MarkupAccumulator so it is easier to append an attribute like this.
+ result.append(' ');
+ result.append(attribute.name().toString());
+ result.appendLiteral("=\"");
+ if (!m_rewriteFolder.isEmpty()) {
+ MarkupFormatter::appendAttributeValue(result, "./" + m_rewriteFolder + "/", document().isHTMLDocument());
yosin_UTC9 2015/06/18 06:45:46 Do we need to have "./"?
Tiger (Sony Mobile) 2015/06/24 22:29:23 I don't think it makes a difference no. This is ho
+ }
+ MarkupFormatter::appendAttributeValue(result, m_rewriteURLs.get(completeURL), document().isHTMLDocument());
+ result.appendLiteral("\"");
+ return;
+ }
+ }
+ MarkupAccumulator::appendAttribute(result, element, attribute, namespaces);
+}
+
+bool LinkChangeSerializerMarkupAccumulator::shouldIgnoreElement(const Element& element) const
+{
+ return SerializerMarkupAccumulator::shouldIgnoreElement(element) || isHTMLBaseElement(element);
+}
+
+
PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr<Delegate> delegate)
: m_resources(resources)
, m_blankFrameCounter(0)
@@ -241,8 +336,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_rewriteURLs.isEmpty()) {
+ LinkChangeSerializerMarkupAccumulator accumulator(this, document, serializedNodes, m_rewriteURLs, m_rewriteFolder);
+ 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())));
@@ -413,6 +515,16 @@ void PageSerializer::retrieveResourcesForCSSValue(CSSValue* cssValue, Document&
}
}
+void PageSerializer::registerRewriteURL(String from, String to)
+{
+ m_rewriteURLs.set(from, to);
+}
+
+void PageSerializer::setRewriteURLFolder(String rewriteFolder)
+{
+ m_rewriteFolder = rewriteFolder;
+}
+
KURL PageSerializer::urlForBlankFrame(LocalFrame* frame)
{
BlankFrameURLMap::iterator iter = m_blankFrameURLs.find(frame);

Powered by Google App Engine
This is Rietveld 408576698