2020-08-01 03:07:00 +01:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-08-01 03:07:00 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:07:00 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-04-18 10:54:06 +02:00
|
|
|
#include <LibWeb/Bindings/HTMLSourceElement.h>
|
2022-09-30 17:16:16 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2023-05-12 15:04:58 -04:00
|
|
|
#include <LibWeb/HTML/AttributeNames.h>
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
#include <LibWeb/HTML/HTMLImageElement.h>
|
2023-05-12 15:04:58 -04:00
|
|
|
#include <LibWeb/HTML/HTMLMediaElement.h>
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
#include <LibWeb/HTML/HTMLPictureElement.h>
|
2020-08-01 03:07:00 +01:00
|
|
|
#include <LibWeb/HTML/HTMLSourceElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(HTMLSourceElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
HTMLSourceElement::HTMLSourceElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:07:00 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
HTMLSourceElement::~HTMLSourceElement() = default;
|
2020-08-01 03:07:00 +01:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void HTMLSourceElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLSourceElement);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
static void update_image_children_of_picture(DOM::Node& picture)
|
|
|
|
|
{
|
|
|
|
|
for (auto* child = picture.first_child(); child; child = child->next_sibling()) {
|
|
|
|
|
if (auto* img = as_if<HTMLImageElement>(child))
|
|
|
|
|
img->update_the_image_data(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-19 11:24:58 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:html-element-insertion-steps
|
2023-05-12 15:04:58 -04:00
|
|
|
void HTMLSourceElement::inserted()
|
|
|
|
|
{
|
|
|
|
|
// The source HTML element insertion steps, given insertedNode, are:
|
|
|
|
|
Base::inserted();
|
|
|
|
|
|
2025-03-19 11:24:58 +00:00
|
|
|
// 1. Let parent be insertedNode's parent.
|
|
|
|
|
auto* parent = this->parent();
|
2023-05-12 15:04:58 -04:00
|
|
|
|
2025-03-19 11:24:58 +00:00
|
|
|
// 2. If parent is a media element that has no src attribute and whose networkState has the value NETWORK_EMPTY,
|
|
|
|
|
// then invoke that media element's resource selection algorithm.
|
|
|
|
|
if (auto* media_element = as_if<HTMLMediaElement>(parent); media_element
|
|
|
|
|
&& !media_element->has_attribute(HTML::AttributeNames::src)
|
|
|
|
|
&& media_element->network_state() == HTMLMediaElement::NetworkState::Empty) {
|
2025-10-31 12:30:47 +00:00
|
|
|
media_element->select_resource();
|
2023-05-12 15:04:58 -04:00
|
|
|
}
|
|
|
|
|
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
// 3. If parent is a picture element, then for each child of parent's children, if child is an img element, then
|
|
|
|
|
// count this as a relevant mutation for child.
|
|
|
|
|
if (auto* picture = as_if<HTMLPictureElement>(parent))
|
|
|
|
|
update_image_children_of_picture(*picture);
|
2023-05-12 15:04:58 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 12:45:26 +13:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:the-source-element-17
|
2026-04-16 20:04:54 +01:00
|
|
|
void HTMLSourceElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<DOM::Node> old_ancestor)
|
2025-03-08 12:45:26 +13:00
|
|
|
{
|
2026-04-16 20:04:54 +01:00
|
|
|
// The source HTML element moving steps, given movedNode, isSubtreeRoot, and oldAncestor are:
|
|
|
|
|
Base::moved_from(is_subtree_root, old_ancestor);
|
2025-03-08 12:45:26 +13:00
|
|
|
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
// 1. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
|
|
|
|
|
// children: if child is an img element, then count this as a relevant mutation for child.
|
|
|
|
|
if (is_subtree_root == IsSubtreeRoot::Yes) {
|
|
|
|
|
if (auto* picture = as_if<HTMLPictureElement>(old_ancestor.ptr()))
|
|
|
|
|
update_image_children_of_picture(*picture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The img element may also have moved into a (new) picture parent; the "picture's children
|
|
|
|
|
// changed" mutation covers that for the new ancestor too.
|
|
|
|
|
if (auto* picture = as_if<HTMLPictureElement>(parent()))
|
|
|
|
|
update_image_children_of_picture(*picture);
|
2025-03-08 12:45:26 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:the-source-element-18
|
2026-04-16 20:04:54 +01:00
|
|
|
void HTMLSourceElement::removed_from(IsSubtreeRoot is_subtree_root, DOM::Node* old_ancestor, DOM::Node& old_root)
|
2023-05-12 15:04:58 -04:00
|
|
|
{
|
2026-04-16 20:04:54 +01:00
|
|
|
// The source HTML element removing steps, given removedNode, isSubtreeRoot, and oldAncestor are:
|
|
|
|
|
Base::removed_from(is_subtree_root, old_ancestor, old_root);
|
2023-05-12 15:04:58 -04:00
|
|
|
|
LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.
Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
is a previous sibling has its srcset, sizes, media, type, width or
height attributes set, changed, or removed."
The same applies to source insertion, moving, and removal.
Fixes image loading on https://www.apple.com/mac/
2026-04-26 15:52:55 +02:00
|
|
|
// 1. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
|
|
|
|
|
// children: if child is an img element, then count this as a relevant mutation for child.
|
|
|
|
|
if (is_subtree_root == IsSubtreeRoot::Yes) {
|
|
|
|
|
if (auto* picture = as_if<HTMLPictureElement>(old_ancestor))
|
|
|
|
|
update_image_children_of_picture(*picture);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#relevant-mutations
|
|
|
|
|
// "The element's parent is a picture element and a source element that is a previous sibling has
|
|
|
|
|
// its srcset, sizes, media, type, width or height attributes set, changed, or removed."
|
|
|
|
|
void HTMLSourceElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
|
|
|
|
{
|
|
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
|
|
|
|
|
|
|
|
|
if (!name.is_one_of(
|
|
|
|
|
HTML::AttributeNames::srcset,
|
|
|
|
|
HTML::AttributeNames::sizes,
|
|
|
|
|
HTML::AttributeNames::media,
|
|
|
|
|
HTML::AttributeNames::type,
|
|
|
|
|
HTML::AttributeNames::width,
|
|
|
|
|
HTML::AttributeNames::height))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!is<HTMLPictureElement>(parent()))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Only following img siblings consider this source a "previous sibling".
|
|
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
|
if (auto* img = as_if<HTMLImageElement>(sibling))
|
|
|
|
|
img->update_the_image_data(true);
|
|
|
|
|
}
|
2023-05-12 15:04:58 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-01 03:07:00 +01:00
|
|
|
}
|