Index: Source/core/page/PageSerializer.cpp |
diff --git a/Source/core/page/PageSerializer.cpp b/Source/core/page/PageSerializer.cpp |
index a7c6ffd0d36109194c4d0c279731c0e79b946074..75ac1ef676ee765371ad0ec2d544c5cc1cedd405 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&); |
yosin_UTC9
2015/06/15 09:34:37
Can we mark this function |const|?
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+ |
+ PageSerializer* getPageSerializer(); |
+ const Document& getDocument(); |
+ |
private: |
PageSerializer* m_serializer; |
RawPtrWillBeMember<const Document> m_document; |
@@ -203,8 +203,108 @@ void SerializerMarkupAccumulator::appendEndTag(const Element& element) |
MarkupAccumulator::appendEndTag(element); |
} |
-PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr<Delegate> delegate) |
+bool SerializerMarkupAccumulator::shouldIgnoreElement(const Element& element) |
+{ |
+ return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isCharsetSpecifyingNode(element); |
+} |
+ |
+PageSerializer* SerializerMarkupAccumulator::getPageSerializer() |
yosin_UTC9
2015/06/15 09:34:37
In Blink, we don't use "get" prefix.
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+{ |
+ return m_serializer; |
+} |
+ |
+const Document& SerializerMarkupAccumulator::getDocument() |
yosin_UTC9
2015/06/15 09:34:37
In Blink, we don't use "get" prefix.
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+{ |
+ return *m_document; |
+} |
+ |
+class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator { |
yosin_UTC9
2015/06/15 09:34:36
|final|?
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+public: |
+ LinkChangeSerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVector<RawPtrWillBeMember<Node>>&, LinkLocalPathMap*, String); |
+ |
+protected: |
+ virtual void appendElement(StringBuilder&, Element&, Namespaces*) override; |
yosin_UTC9
2015/06/15 09:34:36
no need to have |virtual|.
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+ virtual void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*) override; |
yosin_UTC9
2015/06/15 09:34:37
no need to have |virtual|.
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+ |
+ virtual bool shouldIgnoreElement(const Element&); |
yosin_UTC9
2015/06/15 09:34:37
Please use |override| instead of |virtual|.
Tiger (Sony Mobile)
2015/06/15 11:26:45
Done.
|
+ |
+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) |
+{ |
+ 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>(getDocument().url().string().utf8().length()), |
+ getDocument().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 (!getDocument().baseTarget().isEmpty()) { |
+ result.appendLiteral(" target=\""); |
+ result.append(getDocument().baseTarget()); |
+ result.append('"'); |
+ } |
+ if (getDocument().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 = getDocument().completeURL(attribute.value()); |
+ |
+ if (m_replaceLinks->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_directoryName.isEmpty()) { |
+ result.appendLiteral("./"); |
+ MarkupFormatter::appendAttributeValue(result, m_directoryName, getDocument().isHTMLDocument()); |
+ result.append('/'); |
+ } |
+ MarkupFormatter::appendAttributeValue(result, m_replaceLinks->get(completeURL), getDocument().isHTMLDocument()); |
+ result.appendLiteral("\""); |
+ return; |
+ } |
+ } |
+ MarkupAccumulator::appendAttribute(result, element, attribute, namespaces); |
+} |
+ |
+bool LinkChangeSerializerMarkupAccumulator::shouldIgnoreElement(const Element& element) |
+{ |
+ return SerializerMarkupAccumulator::shouldIgnoreElement(element) || isHTMLBaseElement(element); |
+} |
+ |
+ |
+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 +341,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()))); |