Title: [195132] trunk/Source/WebCore
Revision
195132
Author
[email protected]
Date
2016-01-15 09:06:51 -0800 (Fri, 15 Jan 2016)

Log Message

Avoid downloading the wrong image for <picture> elements.
https://bugs.webkit.org/show_bug.cgi?id=153027

Reviewed by Dean Jackson.

No tests, since they are always flaky.

* html/HTMLImageElement.cpp:
(WebCore::HTMLImageElement::HTMLImageElement):
(WebCore::HTMLImageElement::~HTMLImageElement):
(WebCore::HTMLImageElement::createForJSConstructor):
(WebCore::HTMLImageElement::bestFitSourceFromPictureElement):
(WebCore::HTMLImageElement::insertedInto):
(WebCore::HTMLImageElement::removedFrom):
(WebCore::HTMLImageElement::pictureElement):
(WebCore::HTMLImageElement::setPictureElement):
(WebCore::HTMLImageElement::width):
* html/HTMLImageElement.h:
(WebCore::HTMLImageElement::hasShadowControls):
* html/HTMLPictureElement.h:
* html/parser/HTMLConstructionSite.cpp:
(WebCore::HTMLConstructionSite::createHTMLElement):
* html/parser/HTMLPreloadScanner.cpp:
(WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):

Images that are built underneath a <picture> element are now connected
to that picture element via a setPictureNode call from the parser. This
ensures that the correct <source> elements are examined before checking the image.

This connection between images and their picture owners is handled using a static
HashMap in HTMLImageElement. This connection is made both from the parser and from
DOM insertions, and the map is queried now instead of looking directly at the
image's parentNode().

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (195131 => 195132)


--- trunk/Source/WebCore/ChangeLog	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/ChangeLog	2016-01-15 17:06:51 UTC (rev 195132)
@@ -1,3 +1,39 @@
+2016-01-15  Dave Hyatt  <[email protected]>
+
+        Avoid downloading the wrong image for <picture> elements.
+        https://bugs.webkit.org/show_bug.cgi?id=153027
+
+        Reviewed by Dean Jackson.
+
+        No tests, since they are always flaky.
+
+        * html/HTMLImageElement.cpp:
+        (WebCore::HTMLImageElement::HTMLImageElement):
+        (WebCore::HTMLImageElement::~HTMLImageElement):
+        (WebCore::HTMLImageElement::createForJSConstructor):
+        (WebCore::HTMLImageElement::bestFitSourceFromPictureElement):
+        (WebCore::HTMLImageElement::insertedInto):
+        (WebCore::HTMLImageElement::removedFrom):
+        (WebCore::HTMLImageElement::pictureElement):
+        (WebCore::HTMLImageElement::setPictureElement):
+        (WebCore::HTMLImageElement::width):
+        * html/HTMLImageElement.h:
+        (WebCore::HTMLImageElement::hasShadowControls):
+        * html/HTMLPictureElement.h:
+        * html/parser/HTMLConstructionSite.cpp:
+        (WebCore::HTMLConstructionSite::createHTMLElement):
+        * html/parser/HTMLPreloadScanner.cpp:
+        (WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
+
+        Images that are built underneath a <picture> element are now connected
+        to that picture element via a setPictureNode call from the parser. This
+        ensures that the correct <source> elements are examined before checking the image.
+
+        This connection between images and their picture owners is handled using a static
+        HashMap in HTMLImageElement. This connection is made both from the parser and from
+        DOM insertions, and the map is queried now instead of looking directly at the
+        image's parentNode().
+
 2016-01-15  Youenn Fablet  <[email protected]>
 
         [Streams API] Expose ReadableStream and relatives to Worker

Modified: trunk/Source/WebCore/html/HTMLImageElement.cpp (195131 => 195132)


--- trunk/Source/WebCore/html/HTMLImageElement.cpp	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/html/HTMLImageElement.cpp	2016-01-15 17:06:51 UTC (rev 195132)
@@ -53,6 +53,9 @@
 
 using namespace HTMLNames;
 
+typedef HashMap<const HTMLImageElement*, WeakPtr<HTMLPictureElement>> PictureOwnerMap;
+static PictureOwnerMap* gPictureOwnerMap = nullptr;
+
 HTMLImageElement::HTMLImageElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
     : HTMLElement(tagName, document)
     , m_imageLoader(*this)
@@ -82,6 +85,7 @@
 {
     if (m_form)
         m_form->removeImgElement(this);
+    setPictureElement(nullptr);
 }
 
 Ref<HTMLImageElement> HTMLImageElement::createForJSConstructor(Document& document, const int* optionalWidth, const int* optionalHeight)
@@ -140,13 +144,12 @@
 
 ImageCandidate HTMLImageElement::bestFitSourceFromPictureElement()
 {
-    auto* parent = parentNode();
-    if (!is<HTMLPictureElement>(parent))
+    auto* picture = pictureElement();
+    if (!picture)
         return { };
-    auto* picture = downcast<HTMLPictureElement>(parent);
     picture->clearViewportDependentResults();
     document().removeViewportDependentPicture(*picture);
-    for (Node* child = parent->firstChild(); child && child != this; child = child->nextSibling()) {
+    for (Node* child = picture->firstChild(); child && child != this; child = child->nextSibling()) {
         if (!is<HTMLSourceElement>(*child))
             continue;
         auto& source = downcast<HTMLSourceElement>(*child);
@@ -163,7 +166,7 @@
             if (!type.isEmpty() && !MIMETypeRegistry::isSupportedImageMIMEType(type) && type != "image/svg+xml")
                 continue;
         }
-        MediaQueryEvaluator evaluator(document().printing() ? "print" : "screen", document().frame(), computedStyle());
+        MediaQueryEvaluator evaluator(document().printing() ? "print" : "screen", document().frame(), document().documentElement() ? document().documentElement()->computedStyle() : nullptr);
         bool evaluation = evaluator.evalCheckingViewportDependentResults(source.mediaQuerySet(), picture->viewportDependentResults());
         if (picture->hasViewportDependentResults())
             document().addViewportDependentPicture(*picture);
@@ -313,8 +316,10 @@
     if (insertionPoint.inDocument() && !m_lowercasedUsemap.isNull())
         document().addImageElementByLowercasedUsemap(*m_lowercasedUsemap.impl(), *this);
 
-    if (is<HTMLPictureElement>(parentNode()))
+    if (is<HTMLPictureElement>(parentNode())) {
+        setPictureElement(&downcast<HTMLPictureElement>(*parentNode()));
         selectImageSource();
+    }
 
     // If we have been inserted from a renderer-less document,
     // our loader may have not fetched the image, so do it now.
@@ -331,11 +336,37 @@
 
     if (insertionPoint.inDocument() && !m_lowercasedUsemap.isNull())
         document().removeImageElementByLowercasedUsemap(*m_lowercasedUsemap.impl(), *this);
-
+    
+    if (is<HTMLPictureElement>(parentNode()))
+        setPictureElement(nullptr);
+    
     m_form = nullptr;
     HTMLElement::removedFrom(insertionPoint);
 }
 
+HTMLPictureElement* HTMLImageElement::pictureElement() const
+{
+    if (!gPictureOwnerMap || !gPictureOwnerMap->contains(this))
+        return nullptr;
+    HTMLPictureElement* result = gPictureOwnerMap->get(this).get();
+    if (!result)
+        gPictureOwnerMap->remove(this);
+    return result;
+}
+    
+void HTMLImageElement::setPictureElement(HTMLPictureElement* pictureElement)
+{
+    if (!pictureElement) {
+        if (gPictureOwnerMap)
+            gPictureOwnerMap->remove(this);
+        return;
+    }
+    
+    if (!gPictureOwnerMap)
+        gPictureOwnerMap = new PictureOwnerMap();
+    gPictureOwnerMap->add(this, pictureElement->createWeakPtr());
+}
+    
 int HTMLImageElement::width(bool ignorePendingStylesheets)
 {
     if (!renderer()) {

Modified: trunk/Source/WebCore/html/HTMLImageElement.h (195131 => 195132)


--- trunk/Source/WebCore/html/HTMLImageElement.h	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/html/HTMLImageElement.h	2016-01-15 17:06:51 UTC (rev 195132)
@@ -87,6 +87,9 @@
     virtual const AtomicString& imageSourceURL() const override;
 
     bool hasShadowControls() const { return m_experimentalImageMenuEnabled; }
+    
+    HTMLPictureElement* pictureElement() const;
+    void setPictureElement(HTMLPictureElement*);
 
 protected:
     HTMLImageElement(const QualifiedName&, Document&, HTMLFormElement* = 0);
@@ -127,6 +130,7 @@
     HTMLImageLoader m_imageLoader;
     HTMLFormElement* m_form;
     HTMLFormElement* m_formSetByParser;
+
     CompositeOperator m_compositeOperator;
     AtomicString m_bestFitImageURL;
     AtomicString m_currentSrc;

Modified: trunk/Source/WebCore/html/HTMLPictureElement.h (195131 => 195132)


--- trunk/Source/WebCore/html/HTMLPictureElement.h	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/html/HTMLPictureElement.h	2016-01-15 17:06:51 UTC (rev 195132)
@@ -46,9 +46,12 @@
     
     bool viewportChangeAffectedPicture();
 
+    WeakPtr<HTMLPictureElement> createWeakPtr() { return m_weakFactory.createWeakPtr(); }
+
 private:
     HTMLPictureElement(const QualifiedName&, Document&);
     
+    WeakPtrFactory<HTMLPictureElement> m_weakFactory { this };
     Vector<std::unique_ptr<MediaQueryResult>> m_viewportDependentMediaQueryResults;
 
 };

Modified: trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp (195131 => 195132)


--- trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp	2016-01-15 17:06:51 UTC (rev 195132)
@@ -36,9 +36,11 @@
 #include "HTMLElementFactory.h"
 #include "HTMLFormElement.h"
 #include "HTMLHtmlElement.h"
+#include "HTMLImageElement.h"
 #include "HTMLOptGroupElement.h"
 #include "HTMLOptionElement.h"
 #include "HTMLParserIdioms.h"
+#include "HTMLPictureElement.h"
 #include "HTMLScriptElement.h"
 #include "HTMLTemplateElement.h"
 #include "NotImplemented.h"
@@ -640,6 +642,13 @@
     Document& ownerDocument = ownerDocumentForCurrentNode();
     bool insideTemplateElement = !ownerDocument.frame();
     RefPtr<Element> element = HTMLElementFactory::createElement(tagName, ownerDocument, insideTemplateElement ? nullptr : form(), true);
+    
+    // FIXME: This is a hack to connect images to pictures before the image has
+    // been inserted into the document. It can be removed once asynchronous image
+    // loading is working.
+    if (is<HTMLPictureElement>(currentNode()) && is<HTMLImageElement>(*element.get()))
+        downcast<HTMLImageElement>(*element.get()).setPictureElement(&downcast<HTMLPictureElement>(currentNode()));
+
     setAttributes(element.get(), token, m_parserContentPolicy);
     ASSERT(element->isHTMLElement());
     return element.release();

Modified: trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp (195131 => 195132)


--- trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp	2016-01-15 13:41:51 UTC (rev 195131)
+++ trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp	2016-01-15 17:06:51 UTC (rev 195132)
@@ -208,7 +208,7 @@
                 m_mediaAttribute = attributeValue;
                 Ref<MediaQuerySet> mediaSet = MediaQuerySet::createAllowingDescriptionSyntax(attributeValue);
                 Vector<std::unique_ptr<MediaQueryResult>> viewportDependentMediaQueryResults;
-                MediaQueryEvaluator evaluator(document.printing() ? "print" : "screen", document.frame(), document.documentElement()->computedStyle());
+                MediaQueryEvaluator evaluator(document.printing() ? "print" : "screen", document.frame(), document.documentElement() ? document.documentElement()->computedStyle() : nullptr);
                 m_mediaMatched = evaluator.evalCheckingViewportDependentResults(mediaSet.ptr(), viewportDependentMediaQueryResults);
             }
             break;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to