Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / page / PageSerializer.cpp
index 9143fdd..7eb82d7 100644 (file)
@@ -31,7 +31,8 @@
 #include "config.h"
 #include "core/page/PageSerializer.h"
 
-#include "HTMLNames.h"
+#include "core/HTMLNames.h"
+#include "core/InputTypeNames.h"
 #include "core/css/CSSFontFaceRule.h"
 #include "core/css/CSSFontFaceSrcValue.h"
 #include "core/css/CSSImageValue.h"
@@ -67,7 +68,7 @@
 #include "wtf/text/TextEncoding.h"
 #include "wtf/text/WTFString.h"
 
-namespace WebCore {
+namespace blink {
 
 static bool isCharsetSpecifyingNode(const Node& node)
 {
@@ -75,16 +76,13 @@ static bool isCharsetSpecifyingNode(const Node& node)
         return false;
 
     const HTMLMetaElement& element = toHTMLMetaElement(node);
-    HTMLAttributeList attributes;
-    if (element.hasAttributes()) {
-        unsigned attributeCount = element.attributeCount();
-        for (unsigned i = 0; i < attributeCount; ++i) {
-            const Attribute& attribute = element.attributeItem(i);
-            // FIXME: We should deal appropriately with the attribute if they have a namespace.
-            attributes.append(std::make_pair(attribute.name().localName(), attribute.value().string()));
-        }
+    HTMLAttributeList attributeList;
+    AttributeCollection attributes = element.attributes();
+    for (const Attribute& attr : attributes) {
+        // FIXME: We should deal appropriately with the attribute if they have a namespace.
+        attributeList.append(std::make_pair(attr.name().localName(), attr.value().string()));
     }
-    WTF::TextEncoding textEncoding = encodingFromMetaAttributes(attributes);
+    WTF::TextEncoding textEncoding = encodingFromMetaAttributes(attributeList);
     return textEncoding.isValid();
 }
 
@@ -99,24 +97,24 @@ static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme
     return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::srcAttr;
 }
 
-class SerializerMarkupAccumulator FINAL : public MarkupAccumulator {
+class SerializerMarkupAccumulator final : public MarkupAccumulator {
 public:
-    SerializerMarkupAccumulator(PageSerializer*, const Document&, Vector<Node*>*);
+    SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVector<RawPtrWillBeMember<Node>>*);
     virtual ~SerializerMarkupAccumulator();
 
 protected:
-    virtual void appendText(StringBuilder& out, Text&) OVERRIDE;
-    virtual void appendElement(StringBuilder& out, Element&, Namespaces*) OVERRIDE;
-    virtual void appendCustomAttributes(StringBuilder& out, const Element&, Namespaces*) OVERRIDE;
-    virtual void appendEndTag(const Node&) OVERRIDE;
+    virtual void appendText(StringBuilder& out, Text&) override;
+    virtual void appendElement(StringBuilder& out, Element&, Namespaces*) override;
+    virtual void appendCustomAttributes(StringBuilder& out, const Element&, Namespaces*) override;
+    virtual void appendEndTag(const Element&) override;
 
 private:
     PageSerializer* m_serializer;
     const Document& m_document;
 };
 
-SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* serializer, const Document& document, Vector<Node*>* nodes)
-    : MarkupAccumulator(nodes, ResolveAllURLs)
+SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* serializer, const Document& document, WillBeHeapVector<RawPtrWillBeMember<Node>>* nodes)
+    : MarkupAccumulator(nodes, ResolveAllURLs, nullptr)
     , m_serializer(serializer)
     , m_document(document)
 {
@@ -139,9 +137,9 @@ void SerializerMarkupAccumulator::appendElement(StringBuilder& out, Element& ele
         MarkupAccumulator::appendElement(out, element, namespaces);
 
     if (isHTMLHeadElement(element)) {
-        out.append("<meta charset=\"");
+        out.appendLiteral("<meta charset=\"");
         out.append(m_document.charset());
-        out.append("\">");
+        out.appendLiteral("\">");
     }
 
     // FIXME: For object (plugins) tags and video tag we could replace them by an image of their current contents.
@@ -153,23 +151,24 @@ void SerializerMarkupAccumulator::appendCustomAttributes(StringBuilder& out, con
         return;
 
     const HTMLFrameOwnerElement& frameOwner = toHTMLFrameOwnerElement(element);
-    LocalFrame* frame = frameOwner.contentFrame();
-    if (!frame)
+    Frame* frame = frameOwner.contentFrame();
+    // FIXME: RemoteFrames not currently supported here.
+    if (!frame || !frame->isLocalFrame())
         return;
 
-    KURL url = frame->document()->url();
-    if (url.isValid() && !url.isBlankURL())
+    KURL url = toLocalFrame(frame)->document()->url();
+    if (url.isValid() && !url.protocolIsAbout())
         return;
 
     // We need to give a fake location to blank frames so they can be referenced by the serialized frame.
-    url = m_serializer->urlForBlankFrame(frame);
+    url = m_serializer->urlForBlankFrame(toLocalFrame(frame));
     appendAttribute(out, element, Attribute(frameOwnerURLAttributeName(frameOwner), AtomicString(url.string())), namespaces);
 }
 
-void SerializerMarkupAccumulator::appendEndTag(const Node& node)
+void SerializerMarkupAccumulator::appendEndTag(const Element& element)
 {
-    if (node.isElementNode() && !shouldIgnoreElement(toElement(node)))
-        MarkupAccumulator::appendEndTag(node);
+    if (!shouldIgnoreElement(element))
+        MarkupAccumulator::appendEndTag(element);
 }
 
 PageSerializer::PageSerializer(Vector<SerializedResource>* resources)
@@ -180,7 +179,7 @@ PageSerializer::PageSerializer(Vector<SerializedResource>* resources)
 
 void PageSerializer::serialize(Page* page)
 {
-    serializeFrame(page->mainFrame());
+    serializeFrame(page->deprecatedLocalMainFrame());
 }
 
 void PageSerializer::serializeFrame(LocalFrame* frame)
@@ -188,7 +187,8 @@ void PageSerializer::serializeFrame(LocalFrame* frame)
     ASSERT(frame->document());
     Document& document = *frame->document();
     KURL url = document.url();
-    if (!url.isValid() || url.isBlankURL()) {
+    // FIXME: This probably wants isAboutBlankURL? to exclude other about: urls (like about:srcdoc)?
+    if (!url.isValid() || url.protocolIsAbout()) {
         // For blank frames we generate a fake URL so they can be referenced by their containing frame.
         url = urlForBlankFrame(frame);
     }
@@ -206,20 +206,19 @@ void PageSerializer::serializeFrame(LocalFrame* frame)
         return;
     }
 
-    Vector<Node*> serializedNodes;
+    WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes;
     SerializerMarkupAccumulator accumulator(this, document, &serializedNodes);
     String text = accumulator.serializeNodes(document, IncludeNode);
     CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUnencodables);
     m_resources->append(SerializedResource(url, document.suggestedMIMEType(), SharedBuffer::create(frameHTML.data(), frameHTML.length())));
     m_resourceURLs.add(url);
 
-    for (Vector<Node*>::iterator iter = serializedNodes.begin(); iter != serializedNodes.end(); ++iter) {
-        ASSERT(*iter);
-        Node& node = **iter;
-        if (!node.isElementNode())
+    for (Node* node : serializedNodes) {
+        ASSERT(node);
+        if (!node->isElementNode())
             continue;
 
-        Element& element = toElement(node);
+        Element& element = toElement(*node);
         // We have to process in-line style as it might contain some resources (typically background images).
         if (element.isStyledElement())
             retrieveResourcesForProperties(element.inlineStyle(), document);
@@ -231,7 +230,7 @@ void PageSerializer::serializeFrame(LocalFrame* frame)
             addImageToResources(cachedImage, imageElement.renderer(), url);
         } else if (isHTMLInputElement(element)) {
             HTMLInputElement& inputElement = toHTMLInputElement(element);
-            if (inputElement.isImageButton() && inputElement.hasImageLoader()) {
+            if (inputElement.type() == InputTypeNames::image && inputElement.hasImageLoader()) {
                 KURL url = inputElement.src();
                 ImageResource* cachedImage = inputElement.imageLoader()->image();
                 addImageToResources(cachedImage, inputElement.renderer(), url);
@@ -240,40 +239,43 @@ void PageSerializer::serializeFrame(LocalFrame* frame)
             HTMLLinkElement& linkElement = toHTMLLinkElement(element);
             if (CSSStyleSheet* sheet = linkElement.sheet()) {
                 KURL url = document.completeURL(linkElement.getAttribute(HTMLNames::hrefAttr));
-                serializeCSSStyleSheet(sheet, url);
+                serializeCSSStyleSheet(*sheet, url);
                 ASSERT(m_resourceURLs.contains(url));
             }
         } else if (isHTMLStyleElement(element)) {
             HTMLStyleElement& styleElement = toHTMLStyleElement(element);
             if (CSSStyleSheet* sheet = styleElement.sheet())
-                serializeCSSStyleSheet(sheet, KURL());
+                serializeCSSStyleSheet(*sheet, KURL());
         }
     }
 
-    for (LocalFrame* childFrame = frame->tree().firstChild(); childFrame; childFrame = childFrame->tree().nextSibling())
-        serializeFrame(childFrame);
+    for (Frame* childFrame = frame->tree().firstChild(); childFrame; childFrame = childFrame->tree().nextSibling()) {
+        if (childFrame->isLocalFrame())
+            serializeFrame(toLocalFrame(childFrame));
+    }
 }
 
-void PageSerializer::serializeCSSStyleSheet(CSSStyleSheet* styleSheet, const KURL& url)
+void PageSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, const KURL& url)
 {
     StringBuilder cssText;
-    for (unsigned i = 0; i < styleSheet->length(); ++i) {
-        CSSRule* rule = styleSheet->item(i);
+    for (unsigned i = 0; i < styleSheet.length(); ++i) {
+        CSSRule* rule = styleSheet.item(i);
         String itemText = rule->cssText();
         if (!itemText.isEmpty()) {
             cssText.append(itemText);
-            if (i < styleSheet->length() - 1)
-                cssText.append("\n\n");
+            if (i < styleSheet.length() - 1)
+                cssText.appendLiteral("\n\n");
         }
-        ASSERT(styleSheet->ownerDocument());
-        Document& document = *styleSheet->ownerDocument();
+        ASSERT(styleSheet.ownerDocument());
+        Document& document = *styleSheet.ownerDocument();
         // Some rules have resources associated with them that we need to retrieve.
         if (rule->type() == CSSRule::IMPORT_RULE) {
             CSSImportRule* importRule = toCSSImportRule(rule);
             KURL importURL = document.completeURL(importRule->href());
             if (m_resourceURLs.contains(importURL))
                 continue;
-            serializeCSSStyleSheet(importRule->styleSheet(), importURL);
+            if (importRule->styleSheet())
+                serializeCSSStyleSheet(*importRule->styleSheet(), importURL);
         } else if (rule->type() == CSSRule::FONT_FACE_RULE) {
             retrieveResourcesForProperties(&toCSSFontFaceRule(rule)->styleRule()->properties(), document);
         } else if (rule->type() == CSSRule::STYLE_RULE) {
@@ -283,7 +285,7 @@ void PageSerializer::serializeCSSStyleSheet(CSSStyleSheet* styleSheet, const KUR
 
     if (url.isValid() && !m_resourceURLs.contains(url)) {
         // FIXME: We should check whether a charset has been specified and if none was found add one.
-        WTF::TextEncoding textEncoding(styleSheet->contents()->charset());
+        WTF::TextEncoding textEncoding(styleSheet.contents()->charset());
         ASSERT(textEncoding.isValid());
         String textString = cssText.toString();
         CString text = textEncoding.normalizeAndEncode(textString, WTF::EntitiesForUnencodables);
@@ -317,7 +319,7 @@ void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima
     if (!image || image->image() == Image::nullImage() || image->errorOccurred())
         return;
 
-    RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRenderer)->data() : 0;
+    RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRenderer)->data() : nullptr;
     if (!data)
         data = image->image()->data();
 
@@ -358,7 +360,7 @@ void PageSerializer::retrieveResourcesForCSSValue(CSSValue* cssValue, Document&
         if (!styleImage || !styleImage->isImageResource())
             return;
 
-        addImageToResources(styleImage->cachedImage(), 0, styleImage->cachedImage()->url());
+        addImageToResources(styleImage->cachedImage(), nullptr, styleImage->cachedImage()->url());
     } else if (cssValue->isFontFaceSrcValue()) {
         CSSFontFaceSrcValue* fontFaceSrcValue = toCSSFontFaceSrcValue(cssValue);
         if (fontFaceSrcValue->isLocal()) {