Index: Source/core/page/PageSerializer.cpp |
diff --git a/Source/core/page/PageSerializer.cpp b/Source/core/page/PageSerializer.cpp |
index a7c6ffd0d36109194c4d0c279731c0e79b946074..bbbc4c749e5920d61a7d11c513733ef6fe87040f 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,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) 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>>&, LinkLocalPathMap*, String); |
+ |
+protected: |
yosin_UTC9
2015/06/16 01:21:50
Since this class is marked |final|, please move pr
Tiger (Sony Mobile)
2015/06/16 09:28:47
Done.
|
+ void appendElement(StringBuilder&, Element&, Namespaces*) override; |
+ void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*) override; |
+ |
+ bool shouldIgnoreElement(const Element&) const 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) |
+{ |
+ 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=\""); |
+ result.append(document().baseTarget()); |
hajimehoshi
2015/06/16 04:46:54
MarkupFormatter::appendAttributeValue
Tiger (Sony Mobile)
2015/06/16 09:28:47
Done.
|
+ 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_replaceLinks && element.isURLAttribute(attribute) && !element.isJavaScriptURLAttribute(attribute)) { |
+ |
+ String completeURL = document().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, document().isHTMLDocument()); |
+ result.append('/'); |
hajimehoshi
2015/06/16 04:46:55
MarkupFormatter::appendAttributeValue(result, "./"
Tiger (Sony Mobile)
2015/06/16 09:28:47
Done.
|
+ } |
+ MarkupFormatter::appendAttributeValue(result, m_replaceLinks->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, 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()))); |