Skip to content

Commit a75ff81

Browse files
Ahmad-S792Ahmad Saleem
authored andcommitted
Fix HTMLImageElement.currentSrc for empty src attribute
https://bugs.webkit.org/show_bug.cgi?id=304723 rdar://167229274 Reviewed by Chris Dumez. This patch aligns WebKit with Gecko / Firefox and Blink / Chromium. Don't resolve empty src attributes to the document base URL when setting the image's current URL. The completeURL() call should only be made for non-empty source URLs to comply with the HTML specification [1]. When the source URL is empty, the current URL should be set to an empty URL rather than resolving to the document's base URL, ensuring currentSrc returns the empty string as specified. [1] https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-currentsrc * Source/WebCore/html/HTMLImageElement.cpp: (WebCore::HTMLImageElement::setBestFitURLAndDPRFromImageCandidate): > Progression: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/environment-changes/viewport-change-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt: * LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt: * LayoutTests/platform/ios/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt: * LayoutTests/platform/mac/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt: Canonical link: https://commits.webkit.org/304982@main
1 parent 31f2a10 commit a75ff81

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/environment-changes/viewport-change-expected.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
PASS img (no src), onload, narrow
33
PASS img (no src), resize to wide
4-
FAIL img (empty src), onload, narrow assert_equals: expected "" but got "http://localhost:8800/html/semantics/embedded-content/the-img-element/environment-changes/iframed.sub.html?id=a7ed0945-cbf4-4916-a4b1-edd6d1c0c3b1"
4+
PASS img (empty src), onload, narrow
55
PASS img (empty src), resize to wide
66
PASS img (src only) broken image, onload, narrow
77
PASS img (src only) broken image, resize to wide
@@ -12,18 +12,18 @@ PASS img (srcset 1 cand) broken image, resize to wide
1212
PASS img (srcset 1 cand) valid image, onload, narrow
1313
PASS img (srcset 1 cand) valid image, resize to wide
1414
PASS picture: source (max-width:500px) broken image, img broken image, onload, narrow
15-
FAIL picture: source (max-width:500px) broken image, img broken image, resize to wide assert_unreached: Got unexpected load event Reached unreachable code
15+
FAIL picture: source (max-width:500px) broken image, img broken image, resize to wide assert_unreached: Got unexpected error event Reached unreachable code
1616
PASS picture: source (max-width:500px) broken image, img valid image, onload, narrow
1717
PASS picture: source (max-width:500px) broken image, img valid image, resize to wide
1818
PASS picture: source (max-width:500px) valid image, img broken image, onload, narrow
19-
FAIL picture: source (max-width:500px) valid image, img broken image, resize to wide assert_unreached: Got unexpected load event Reached unreachable code
19+
FAIL picture: source (max-width:500px) valid image, img broken image, resize to wide assert_unreached: Got unexpected error event Reached unreachable code
2020
PASS picture: source (max-width:500px) valid image, img valid image, onload, narrow
2121
PASS picture: source (max-width:500px) valid image, img valid image, resize to wide
2222
PASS picture: same URL in source (max-width:500px) and img, onload, narrow
2323
PASS picture: same URL in source (max-width:500px) and img, resize to wide
2424
PASS img (no src), onload, wide
2525
PASS img (no src), resize to narrow
26-
FAIL img (empty src), onload, wide assert_equals: expected "" but got "http://localhost:8800/html/semantics/embedded-content/the-img-element/environment-changes/iframed.sub.html?id=3c874e49-a578-4024-91cf-0e1ddb0b0396"
26+
PASS img (empty src), onload, wide
2727
PASS img (empty src), resize to narrow
2828
PASS img (src only) broken image, onload, wide
2929
PASS img (src only) broken image, resize to narrow
@@ -34,9 +34,9 @@ PASS img (srcset 1 cand) broken image, resize to narrow
3434
PASS img (srcset 1 cand) valid image, onload, wide
3535
PASS img (srcset 1 cand) valid image, resize to narrow
3636
PASS picture: source (max-width:500px) broken image, img broken image, onload, wide
37-
FAIL picture: source (max-width:500px) broken image, img broken image, resize to narrow assert_unreached: Got unexpected load event Reached unreachable code
37+
FAIL picture: source (max-width:500px) broken image, img broken image, resize to narrow assert_unreached: Got unexpected error event Reached unreachable code
3838
PASS picture: source (max-width:500px) broken image, img valid image, onload, wide
39-
FAIL picture: source (max-width:500px) broken image, img valid image, resize to narrow assert_unreached: Got unexpected load event Reached unreachable code
39+
FAIL picture: source (max-width:500px) broken image, img valid image, resize to narrow assert_unreached: Got unexpected error event Reached unreachable code
4040
PASS picture: source (max-width:500px) valid image, img broken image, onload, wide
4141
PASS picture: source (max-width:500px) valid image, img broken image, resize to narrow
4242
PASS picture: source (max-width:500px) valid image, img valid image, onload, wide

LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
PASS <img data-expect="">
3-
FAIL <img src="" data-expect=""> assert_equals: expected "" but got "http://localhost:8800/html/semantics/embedded-content/the-img-element/update-the-source-set.html"
3+
PASS <img src="" data-expect="">
44
PASS <img src="data:,a" data-expect="data:,a">
55
PASS <img srcset="" src="data:,a" data-expect="data:,a">
66
PASS <img srcset="data:,b" src="data:,a" data-expect="data:,b">

LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
PASS <img data-expect="">
3-
FAIL <img src="" data-expect=""> assert_equals: expected "" but got "http://web-platform.test:8800/html/semantics/embedded-content/the-img-element/update-the-source-set.html"
3+
PASS <img src="" data-expect="">
44
PASS <img src="data:,a" data-expect="data:,a">
55
PASS <img srcset="" src="data:,a" data-expect="data:,a">
66
PASS <img srcset="data:,b" src="data:,a" data-expect="data:,b">

LayoutTests/platform/ios/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
PASS <img data-expect="">
3-
FAIL <img src="" data-expect=""> assert_equals: expected "" but got "http://web-platform.test:8800/html/semantics/embedded-content/the-img-element/update-the-source-set.html"
3+
PASS <img src="" data-expect="">
44
PASS <img src="data:,a" data-expect="data:,a">
55
PASS <img srcset="" src="data:,a" data-expect="data:,a">
66
PASS <img srcset="data:,b" src="data:,a" data-expect="data:,b">

LayoutTests/platform/mac/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/update-the-source-set-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
PASS <img data-expect="">
3-
FAIL <img src="" data-expect=""> assert_equals: expected "" but got "http://web-platform.test:8800/html/semantics/embedded-content/the-img-element/update-the-source-set.html"
3+
PASS <img src="" data-expect="">
44
PASS <img src="data:,a" data-expect="data:,a">
55
PASS <img srcset="" src="data:,a" data-expect="data:,a">
66
PASS <img srcset="data:,b" src="data:,a" data-expect="data:,b">

Source/WebCore/html/HTMLImageElement.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ const AtomString& HTMLImageElement::currentSrc()
247247
void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidate& candidate)
248248
{
249249
m_bestFitImageURL = candidate.string.toAtomString();
250-
m_currentURL = protectedDocument()->completeURL(imageSourceURL());
250+
251+
auto& sourceURL = imageSourceURL();
252+
// Only complete the URL if it's non-empty to avoid resolving "" to the document base URL.
253+
m_currentURL = sourceURL.isEmpty() ? URL() : protectedDocument()->completeURL(sourceURL);
254+
251255
m_currentSrc = { };
252256
if (candidate.density >= 0)
253257
m_imageDevicePixelRatio = 1 / candidate.density;

0 commit comments

Comments
 (0)