LibWeb: Bring parse_as_sizes_attribute() up to date with the spec

The following spec algorithms had changed since we implemented them:
- "parse a sizes attribute"
- "update the source set"
- "create a source set"

This commit brings them up to date, as well as adding some additional
logging when parsing the sizes attribute fails in some way.
This commit is contained in:
Sam Atkins 2024-09-11 16:41:18 +01:00 committed by Andreas Kling
commit 2a0f6fd23e
Notes: github-actions[bot] 2024-09-12 05:40:06 +00:00
5 changed files with 80 additions and 33 deletions

View file

@ -990,7 +990,12 @@ static void update_the_source_set(DOM::Element& element)
});
}
// 4. For each child in elements:
// 4. Let img be el if el is an img element, otherwise null.
HTMLImageElement* img = nullptr;
if (is<HTMLImageElement>(element))
img = static_cast<HTMLImageElement*>(&element);
// 5. For each child in elements:
for (auto child : elements) {
// 1. If child is el:
if (child == &element) {
@ -1039,11 +1044,13 @@ static void update_the_source_set(DOM::Element& element)
default_source = href_value.release_value();
}
// 10. Let el's source set be the result of creating a source set given default source, srcset, and sizes.
// 10. Let el's source set be the result of creating a source set given default source, srcset, sizes, and img.
if (is<HTMLImageElement>(element))
static_cast<HTMLImageElement&>(element).set_source_set(SourceSet::create(element, default_source, srcset, sizes));
static_cast<HTMLImageElement&>(element).set_source_set(SourceSet::create(element, default_source, srcset, sizes, img));
else if (is<HTMLLinkElement>(element))
TODO();
// 11. Return.
return;
}
// 2. If child is not a source element, then continue.
@ -1070,8 +1077,8 @@ static void update_the_source_set(DOM::Element& element)
}
}
// 7. Parse child's sizes attribute, and let source set's source size be the returned value.
source_set.m_source_size = parse_a_sizes_attribute(element.document(), child->get_attribute_value(HTML::AttributeNames::sizes));
// 7. Parse child's sizes attribute with img, and let source set's source size be the returned value.
source_set.m_source_size = parse_a_sizes_attribute(element, child->get_attribute_value(HTML::AttributeNames::sizes), img);
// 8. If child has a type attribute, and its value is an unknown or unsupported MIME type, continue to the next child.
if (child->has_attribute(HTML::AttributeNames::type)) {

View file

@ -339,15 +339,17 @@ descriptor_parser:
}
// https://html.spec.whatwg.org/multipage/images.html#parse-a-sizes-attribute
CSS::LengthOrCalculated parse_a_sizes_attribute(DOM::Document const& document, StringView sizes)
CSS::LengthOrCalculated parse_a_sizes_attribute(DOM::Element const& element, StringView sizes, HTML::HTMLImageElement const* img)
{
auto css_parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext { document }, sizes);
return css_parser.parse_as_sizes_attribute();
auto css_parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext { element.document() }, sizes);
return css_parser.parse_as_sizes_attribute(element, img);
}
// https://html.spec.whatwg.org/multipage/images.html#create-a-source-set
SourceSet SourceSet::create(DOM::Element const& element, String default_source, String srcset, String sizes)
SourceSet SourceSet::create(DOM::Element const& element, String const& default_source, String const& srcset, String const& sizes, HTML::HTMLImageElement const* img)
{
// When asked to create a source set given a string default source, a string srcset, a string sizes, and an element or null img:
// 1. Let source set be an empty source set.
SourceSet source_set;
@ -355,8 +357,8 @@ SourceSet SourceSet::create(DOM::Element const& element, String default_source,
if (!srcset.is_empty())
source_set = parse_a_srcset_attribute(srcset);
// 3. Let source size be the result of parsing sizes.
source_set.m_source_size = parse_a_sizes_attribute(element.document(), sizes);
// 3. Let source size be the result of parsing sizes with img.
source_set.m_source_size = parse_a_sizes_attribute(element, sizes, img);
// 4. If default source is not the empty string and source set does not contain an image source
// with a pixel density descriptor value of 1, and no image source with a width descriptor,

View file

@ -33,7 +33,7 @@ struct ImageSourceAndPixelDensity {
// https://html.spec.whatwg.org/multipage/images.html#source-set
struct SourceSet {
static SourceSet create(DOM::Element const&, String default_source, String srcset, String sizes);
static SourceSet create(DOM::Element const& element, String const& default_source, String const& srcset, String const& sizes, HTML::HTMLImageElement const* img = nullptr);
[[nodiscard]] bool is_empty() const;
@ -50,6 +50,6 @@ struct SourceSet {
};
SourceSet parse_a_srcset_attribute(StringView);
[[nodiscard]] CSS::LengthOrCalculated parse_a_sizes_attribute(DOM::Document const&, StringView);
[[nodiscard]] CSS::LengthOrCalculated parse_a_sizes_attribute(DOM::Element const& element, StringView sizes, HTML::HTMLImageElement const* img = nullptr);
}