- Revision
- 276697
- Author
- [email protected]
- Date
- 2021-04-28 01:39:01 -0700 (Wed, 28 Apr 2021)
Log Message
Make img elements perform source selection even when not in the document.
https://bugs.webkit.org/show_bug.cgi?id=222801
Reviewed by Ryosuke Niwa.
LayoutTests/imported/w3c:
* web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc-expected.txt: Added.
* web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html: Added.
Source/WebCore:
The rules for image source selection in the HTML spec do not prevent
them from running when an <img> is not in the document. So we update
HTMLImageElement to call selectImageSource() when inserted into and
removed from a <picture> without checking whether it's in a document.
Test: imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html
* html/HTMLImageElement.cpp:
(WebCore::HTMLImageElement::~HTMLImageElement): Remove useless call to
setPictureElement, since all it does is clear a weak pointer.
(WebCore::HTMLImageElement::insertedIntoAncestor):
(WebCore::HTMLImageElement::removedFromAncestor):
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/imported/w3c/ChangeLog (276696 => 276697)
--- trunk/LayoutTests/imported/w3c/ChangeLog 2021-04-28 08:09:16 UTC (rev 276696)
+++ trunk/LayoutTests/imported/w3c/ChangeLog 2021-04-28 08:39:01 UTC (rev 276697)
@@ -1,3 +1,13 @@
+2021-04-28 Cameron McCormack <[email protected]>
+
+ Make img elements perform source selection even when not in the document.
+ https://bugs.webkit.org/show_bug.cgi?id=222801
+
+ Reviewed by Ryosuke Niwa.
+
+ * web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc-expected.txt: Added.
+ * web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html: Added.
+
2021-04-27 Sergio Villar Senin <[email protected]>
[css-flexbox] Fix expectations of a WPT test that works fine
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc-expected.txt (0 => 276697)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc-expected.txt (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc-expected.txt 2021-04-28 08:39:01 UTC (rev 276697)
@@ -0,0 +1,4 @@
+
+
+PASS Image source selection using media queries is performed for img elements outside the document
+
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html (0 => 276697)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html 2021-04-28 08:39:01 UTC (rev 276697)
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<title>Image source selection using media queries is performed for img elements outside the document</title>
+<link rel="help" href=""
+<link rel="help" href=""
+<script src=""
+<script src=""
+<iframe width="350" height="100" _onload_="async_test(this.contentWindow.run)" srcdoc="
+<!DOCTYPE html>
+<script>
+const { assert_equals } = parent;
+const iframe = parent.document.querySelector('iframe');
+
+function run(t) {
+ const picture = document.createElement('picture');
+
+ const source1 = document.createElement('source');
+ source1.setAttribute('media', '(min-width: 300px)');
+ source1.setAttribute('srcset', 'data:,a');
+ picture.append(source1);
+
+ const source2 = document.createElement('source');
+ source2.setAttribute('media', '(min-width: 200px)');
+ source2.setAttribute('srcset', 'data:,b');
+ picture.append(source2);
+
+ const img = document.createElement('img');
+ img.src = '';
+ picture.append(img);
+
+ queueMicrotask(t.step_func(function() {
+ assert_equals(img.currentSrc, 'data:,a', 'Initial currentSrc value');
+ matchMedia(source1.media).addEventListener(
+ 'change',
+ function() {
+ queueMicrotask(t.step_func(function() {
+ assert_equals(img.currentSrc, 'data:,b', 'After MQ change');
+ img.remove();
+ queueMicrotask(t.step_func(function() {
+ assert_equals(img.currentSrc, 'data:,c', 'After removing img');
+ t.done();
+ }));
+ }));
+ },
+ { once: true }
+ );
+ iframe.width = 250;
+ }));
+}
+</script>
+"></iframe>
Modified: trunk/Source/WebCore/ChangeLog (276696 => 276697)
--- trunk/Source/WebCore/ChangeLog 2021-04-28 08:09:16 UTC (rev 276696)
+++ trunk/Source/WebCore/ChangeLog 2021-04-28 08:39:01 UTC (rev 276697)
@@ -1,3 +1,23 @@
+2021-04-28 Cameron McCormack <[email protected]>
+
+ Make img elements perform source selection even when not in the document.
+ https://bugs.webkit.org/show_bug.cgi?id=222801
+
+ Reviewed by Ryosuke Niwa.
+
+ The rules for image source selection in the HTML spec do not prevent
+ them from running when an <img> is not in the document. So we update
+ HTMLImageElement to call selectImageSource() when inserted into and
+ removed from a <picture> without checking whether it's in a document.
+
+ Test: imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/source-media-outside-doc.html
+
+ * html/HTMLImageElement.cpp:
+ (WebCore::HTMLImageElement::~HTMLImageElement): Remove useless call to
+ setPictureElement, since all it does is clear a weak pointer.
+ (WebCore::HTMLImageElement::insertedIntoAncestor):
+ (WebCore::HTMLImageElement::removedFromAncestor):
+
2021-04-28 Ian Gilbert <[email protected]>
Use a WeakHashSet for Document::m_mediaElements
Modified: trunk/Source/WebCore/html/HTMLImageElement.cpp (276696 => 276697)
--- trunk/Source/WebCore/html/HTMLImageElement.cpp 2021-04-28 08:09:16 UTC (rev 276696)
+++ trunk/Source/WebCore/html/HTMLImageElement.cpp 2021-04-28 08:39:01 UTC (rev 276697)
@@ -94,7 +94,6 @@
if (m_form)
m_form->removeImgElement(this);
- setPictureElement(nullptr);
}
Ref<HTMLImageElement> HTMLImageElement::createForLegacyFactoryFunction(Document& document, Optional<unsigned> width, Optional<unsigned> height)
@@ -386,16 +385,8 @@
// FIXME: When the hack in HTMLConstructionSite::createHTMLElementOrFindCustomElementInterface to eagerly call setPictureElement is removed, we can just assert !pictureElement().
ASSERT(!pictureElement() || pictureElement() == &parentOfInsertedTree);
setPictureElement(&downcast<HTMLPictureElement>(parentOfInsertedTree));
- // FIXME: We should unconditionally call selectImageSource so that source selection is performed even for <img> elements outside the document.
- if (insertionType.connectedToDocument) {
- selectImageSource(RelevantMutation::Yes);
- return insertNotificationRequest;
- }
- auto candidate = bestFitSourceFromPictureElement();
- if (!candidate.isEmpty()) {
- setBestFitURLAndDPRFromImageCandidate(candidate);
- m_imageLoader->updateFromElementIgnoringPreviousError(RelevantMutation::Yes);
- }
+ selectImageSource(RelevantMutation::Yes);
+ return insertNotificationRequest;
}
// If we have been inserted from a renderer-less document,
@@ -417,8 +408,7 @@
if (is<HTMLPictureElement>(oldParentOfRemovedTree) && !parentElement()) {
ASSERT(pictureElement() == &oldParentOfRemovedTree);
setPictureElement(nullptr);
- // FIXME: We should call selectImageSource so that source selection is performed, now that we no longer have a <picture> context.
- m_imageLoader->updateFromElementIgnoringPreviousError(RelevantMutation::Yes);
+ selectImageSource(RelevantMutation::Yes);
}
m_form = nullptr;