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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « Source/core/page/PageSerializer.h ('k') | Source/web/tests/PageSerializerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 { 91 {
92 return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isC harsetSpecifyingNode(element); 92 return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isC harsetSpecifyingNode(element);
93 } 93 }
94 94
95 static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme nt& frameOwner) 95 static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme nt& frameOwner)
96 { 96 {
97 // FIXME: We should support all frame owners including applets. 97 // FIXME: We should support all frame owners including applets.
98 return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::sr cAttr; 98 return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::sr cAttr;
99 } 99 }
100 100
101 class SerializerMarkupAccumulator final : public MarkupAccumulator { 101 class SerializerMarkupAccumulator : public MarkupAccumulator {
102 STACK_ALLOCATED(); 102 STACK_ALLOCATED();
103 public: 103 public:
104 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&); 104 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&);
105 virtual ~SerializerMarkupAccumulator(); 105 virtual ~SerializerMarkupAccumulator();
106 106
107 protected: 107 protected:
108 virtual void appendText(StringBuilder& out, Text&) override; 108 virtual void appendText(StringBuilder& out, Text&) override;
109 virtual bool shouldIgnoreAttribute(const Attribute&) override; 109 virtual bool shouldIgnoreAttribute(const Attribute&) override;
110 virtual void appendElement(StringBuilder& out, Element&, Namespaces*) overri de; 110 virtual void appendElement(StringBuilder& out, Element&, Namespaces*) overri de;
111 virtual void appendCustomAttributes(StringBuilder& out, const Element&, Name spaces*) override; 111 virtual void appendCustomAttributes(StringBuilder& out, const Element&, Name spaces*) override;
112 virtual void appendStartTag(Node&, Namespaces* = nullptr) override; 112 virtual void appendStartTag(Node&, Namespaces* = nullptr) override;
113 virtual void appendEndTag(const Element&) override; 113 virtual void appendEndTag(const Element&) override;
114 114
115 private:
116 PageSerializer* m_serializer; 115 PageSerializer* m_serializer;
117 RawPtrWillBeMember<const Document> m_document; 116 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.
118 117
119 // FIXME: |PageSerializer| uses |m_nodes| for collecting nodes in document 118 // FIXME: |PageSerializer| uses |m_nodes| for collecting nodes in document
120 // included into serialized text then extracts image, object, etc. The size 119 // included into serialized text then extracts image, object, etc. The size
121 // of this vector isn't small for large document. It is better to use 120 // of this vector isn't small for large document. It is better to use
122 // callback like functionality. 121 // callback like functionality.
123 WillBeHeapVector<RawPtrWillBeMember<Node>>& m_nodes; 122 WillBeHeapVector<RawPtrWillBeMember<Node>>& m_nodes;
124 }; 123 };
125 124
126 SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* seriali zer, const Document& document, WillBeHeapVector<RawPtrWillBeMember<Node>>& nodes ) 125 SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* seriali zer, const Document& document, WillBeHeapVector<RawPtrWillBeMember<Node>>& nodes )
127 : MarkupAccumulator(ResolveAllURLs) 126 : MarkupAccumulator(ResolveAllURLs)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 MarkupAccumulator::appendStartTag(node, namespaces); 195 MarkupAccumulator::appendStartTag(node, namespaces);
197 m_nodes.append(&node); 196 m_nodes.append(&node);
198 } 197 }
199 198
200 void SerializerMarkupAccumulator::appendEndTag(const Element& element) 199 void SerializerMarkupAccumulator::appendEndTag(const Element& element)
201 { 200 {
202 if (!shouldIgnoreElement(element)) 201 if (!shouldIgnoreElement(element))
203 MarkupAccumulator::appendEndTag(element); 202 MarkupAccumulator::appendEndTag(element);
204 } 203 }
205 204
206 PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr <Delegate> delegate) 205 class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator {
206 public:
207 LinkChangeSerializerMarkupAccumulator(PageSerializer*, const Document&, Will BeHeapVector<RawPtrWillBeMember<Node>>&, LinkLocalPathMap*, String);
208
209 protected:
210 virtual void appendElement(StringBuilder&, Element&, Namespaces*) override;
211 virtual void appendAttribute(StringBuilder&, const Element&, const Attribute &, Namespaces*) override;
212
213 private:
214 // m_replaceLinks include all pair of local resource path and corresponding original link.
215 LinkLocalPathMap* m_replaceLinks;
216 String m_directoryName;
217 };
218
219 LinkChangeSerializerMarkupAccumulator::LinkChangeSerializerMarkupAccumulator(Pag eSerializer* serializer, const Document& document, WillBeHeapVector<RawPtrWillB eMember<Node>>& nodes, LinkLocalPathMap* links, String directoryName)
220 : SerializerMarkupAccumulator(serializer, document, nodes)
221 , m_replaceLinks(links)
222 , m_directoryName(directoryName)
223 {
224 }
225
226 void LinkChangeSerializerMarkupAccumulator::appendElement(StringBuilder& result, Element& element, Namespaces* namespaces)
227 {
228 // 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.
229 if (element.hasTagName(HTMLNames::baseTag)) {
230 // Comment the BASE tag when serializing dom.
231 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.
232 } else if (element.hasTagName(HTMLNames::htmlTag)) {
233 // Add MOTW (Mark of the Web) declaration before html tag.
234 // See http://msdn2.microsoft.com/en-us/library/ms537628(VS.85).aspx.
235 result.append(String::format("\n<!-- saved from url=(%04d)%s -->\n",
hajimehoshi 2015/06/11 01:55:32 Use MarkupFormatter::appendComment
236 static_cast<int>(m_document->url().string().utf8().length()),
237 m_document->url().string().utf8().data()));
238 }
239
240 SerializerMarkupAccumulator::appendElement(result, element, namespaces);
241
242 if (element.hasTagName(HTMLNames::baseTag)) {
243 // Comment the BASE tag when serializing dom.
244 result.appendLiteral("-->");
245
246 // FIXME: Refactor MarkupAccumulator so it is easier to append an elemen t 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.
247 // Append a new base tag declaration.
248 result.appendLiteral("<base href=\".\"");
249 if (!m_document->baseTarget().isEmpty()) {
250 result.appendLiteral(" target=\"");
251 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.
252 result.append('"');
253 }
254 if (m_document->isXHTMLDocument())
255 result.appendLiteral(" />");
256 else
257 result.appendLiteral(">");
258 }
259 }
260
261 void LinkChangeSerializerMarkupAccumulator::appendAttribute(StringBuilder& resul t, const Element& element, const Attribute& attribute, Namespaces* namespaces)
262 {
263 if (m_replaceLinks && element.isURLAttribute(attribute) && !element.isJavaSc riptURLAttribute(attribute)) {
264
265 String completeURL = m_document->completeURL(attribute.value());
266
267 if (m_replaceLinks->contains(completeURL)) {
268 // FIXME: Refactor MarkupAccumulator so it is easier to append an at tribute like this.
269 result.append(' ');
270 result.append(attribute.name().toString());
271 result.appendLiteral("=\"");
272 if (!m_directoryName.isEmpty()) {
273 result.appendLiteral("./");
274 result.append(m_directoryName);
275 result.append('/');
276 }
277 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.
278 result.appendLiteral("\"");
279 return;
280 }
281 }
282 MarkupAccumulator::appendAttribute(result, element, attribute, namespaces);
283 }
284
285
286 PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr <Delegate> delegate, LinkLocalPathMap* urls, String directory)
207 : m_resources(resources) 287 : m_resources(resources)
288 , m_URLs(urls)
289 , m_directory(directory)
208 , m_blankFrameCounter(0) 290 , m_blankFrameCounter(0)
209 , m_delegate(delegate) 291 , m_delegate(delegate)
210 { 292 {
211 } 293 }
212 294
213 void PageSerializer::serialize(Page* page) 295 void PageSerializer::serialize(Page* page)
214 { 296 {
215 serializeFrame(page->deprecatedLocalMainFrame()); 297 serializeFrame(page->deprecatedLocalMainFrame());
216 } 298 }
217 299
(...skipping 16 matching lines...) Expand all
234 } 316 }
235 317
236 // If frame is an image document, add the image and don't continue 318 // If frame is an image document, add the image and don't continue
237 if (document.isImageDocument()) { 319 if (document.isImageDocument()) {
238 ImageDocument& imageDocument = toImageDocument(document); 320 ImageDocument& imageDocument = toImageDocument(document);
239 addImageToResources(imageDocument.cachedImage(), imageDocument.imageElem ent()->layoutObject(), url); 321 addImageToResources(imageDocument.cachedImage(), imageDocument.imageElem ent()->layoutObject(), url);
240 return; 322 return;
241 } 323 }
242 324
243 WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes; 325 WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes;
244 SerializerMarkupAccumulator accumulator(this, document, serializedNodes); 326 String text;
245 String text = serializeNodes<EditingStrategy>(accumulator, document, Include Node); 327 if (m_URLs) {
328 LinkChangeSerializerMarkupAccumulator accumulator(this, document, serial izedNodes, m_URLs, m_directory);
329 text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNod e);
330 } else {
331 SerializerMarkupAccumulator accumulator(this, document, serializedNodes) ;
332 text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNod e);
333 }
334
246 WTF::TextEncoding textEncoding(document.charset()); 335 WTF::TextEncoding textEncoding(document.charset());
247 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables); 336 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables);
248 m_resources->append(SerializedResource(url, document.suggestedMIMEType(), Sh aredBuffer::create(frameHTML.data(), frameHTML.length()))); 337 m_resources->append(SerializedResource(url, document.suggestedMIMEType(), Sh aredBuffer::create(frameHTML.data(), frameHTML.length())));
249 m_resourceURLs.add(url); 338 m_resourceURLs.add(url);
250 339
251 for (Node* node: serializedNodes) { 340 for (Node* node: serializedNodes) {
252 ASSERT(node); 341 ASSERT(node);
253 if (!node->isElementNode()) 342 if (!node->isElementNode())
254 continue; 343 continue;
255 344
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 513
425 return fakeURL; 514 return fakeURL;
426 } 515 }
427 516
428 PageSerializer::Delegate* PageSerializer::delegate() 517 PageSerializer::Delegate* PageSerializer::delegate()
429 { 518 {
430 return m_delegate.get(); 519 return m_delegate.get();
431 } 520 }
432 521
433 } // namespace blink 522 } // namespace blink
OLDNEW
« 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