1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:37:46 +00:00

LibWeb: Add a dedicated function for parsing HTML length values

Presentation attribute lengths (width, height, etc.) can always be
unit-less (e.g "400") so going via the normal CSS parsing path only
works when the document is in quirks mode.

Add a separate parse_html_length() that always allows unit-less values.
This commit is contained in:
Andreas Kling 2020-07-22 01:13:18 +02:00
parent 9fc00d5d12
commit 5a7e57457e
4 changed files with 12 additions and 2 deletions

View file

@ -55,7 +55,7 @@ void HTMLTableCellElement::apply_presentational_hints(StyleProperties& style) co
return; return;
} }
if (name == HTML::AttributeNames::width) { if (name == HTML::AttributeNames::width) {
if (auto parsed_value = parse_css_value(CSS::ParsingContext(document()), value)) if (auto parsed_value = parse_html_length(document(), value))
style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull()); style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
return; return;
} }

View file

@ -42,7 +42,7 @@ void HTMLTableElement::apply_presentational_hints(StyleProperties& style) const
{ {
for_each_attribute([&](auto& name, auto& value) { for_each_attribute([&](auto& name, auto& value) {
if (name == HTML::AttributeNames::width) { if (name == HTML::AttributeNames::width) {
if (auto parsed_value = parse_css_value(CSS::ParsingContext(document()), value)) if (auto parsed_value = parse_html_length(document(), value))
style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull()); style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
return; return;
} }

View file

@ -928,4 +928,12 @@ RefPtr<StyleDeclaration> parse_css_declaration(const CSS::ParsingContext& contex
return parser.parse_standalone_declaration(); return parser.parse_standalone_declaration();
} }
RefPtr<StyleValue> parse_html_length(const Document& document, const StringView& string)
{
auto integer = string.to_int();
if (integer.has_value())
return LengthStyleValue::create(Length::make_px(integer.value()));
return parse_css_value(CSS::ParsingContext(document), string);
}
} }

View file

@ -53,4 +53,6 @@ RefPtr<LengthStyleValue> parse_line_width(const CSS::ParsingContext&, const Stri
RefPtr<ColorStyleValue> parse_color(const CSS::ParsingContext&, const StringView&); RefPtr<ColorStyleValue> parse_color(const CSS::ParsingContext&, const StringView&);
RefPtr<StringStyleValue> parse_line_style(const CSS::ParsingContext&, const StringView&); RefPtr<StringStyleValue> parse_line_style(const CSS::ParsingContext&, const StringView&);
RefPtr<StyleValue> parse_html_length(const Document&, const StringView&);
} }