mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:08:10 +00:00
LibWeb: Add SVG-presentation-attribute-parsing mode to CSS parser
When parsing these, <number> is allowed anywhere that would usually allow a <length>, <length-percentage>, or <angle>. The spec is not clear on exactly how this should work (see https://github.com/w3c/svgwg/issues/792 ) so I'm using some artistic license until things are clearer: - If we expected a <length>, treat the <number> as pixels. - If we expected an <angle>, treat the <number> as degrees. - Only allow direct <number> tokens, not calc() or other functions. From what I can tell this is what the spec *intended* but I may be very wrong. In any case, telling the ParsingContext whether we're parsing one of these attributes is a cleaner approach and more correct than temporarily enabling quirks mode, which we did previously.
This commit is contained in:
parent
c46ce53ce3
commit
79a30c209d
3 changed files with 43 additions and 8 deletions
|
@ -6200,6 +6200,25 @@ Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(Readon
|
|||
|| any_property_accepts_type(property_ids, ValueType::Time).has_value();
|
||||
|
||||
if (property_accepts_dimension) {
|
||||
if (peek_token.is(Token::Type::Number) && m_context.is_parsing_svg_presentation_attribute()) {
|
||||
auto transaction = tokens.begin_transaction();
|
||||
auto token = tokens.next_token();
|
||||
// https://svgwg.org/svg2-draft/types.html#presentation-attribute-css-value
|
||||
// We need to allow <number> in any place that expects a <length> or <angle>.
|
||||
// FIXME: How should these numbers be interpreted? https://github.com/w3c/svgwg/issues/792
|
||||
// For now: Convert them to px lengths, or deg angles.
|
||||
auto angle = Angle::make_degrees(token.token().number_value());
|
||||
if (auto property = any_property_accepts_type(property_ids, ValueType::Angle); property.has_value() && property_accepts_angle(*property, angle)) {
|
||||
transaction.commit();
|
||||
return PropertyAndValue { *property, AngleStyleValue::create(angle) };
|
||||
}
|
||||
auto length = Length::make_px(CSSPixels::nearest_value_for(token.token().number_value()));
|
||||
if (auto property = any_property_accepts_type(property_ids, ValueType::Length); property.has_value() && property_accepts_length(*property, length)) {
|
||||
transaction.commit();
|
||||
return PropertyAndValue { *property, LengthStyleValue::create(length) };
|
||||
}
|
||||
}
|
||||
|
||||
auto transaction = tokens.begin_transaction();
|
||||
if (auto maybe_dimension = parse_dimension(peek_token); maybe_dimension.has_value()) {
|
||||
(void)tokens.next_token();
|
||||
|
@ -6249,6 +6268,9 @@ Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(Readon
|
|||
(void)tokens.next_token();
|
||||
auto& calculated = *maybe_calculated;
|
||||
// This is a bit sensitive to ordering: `<foo>` and `<percentage>` have to be checked before `<foo-percentage>`.
|
||||
// FIXME: When parsing SVG presentation attributes, <number> is permitted wherever <length>, <length-percentage>, or <angle> are.
|
||||
// The specifics are unclear, so I'm ignoring this for calculated values for now.
|
||||
// See https://github.com/w3c/svgwg/issues/792
|
||||
if (calculated.resolves_to_percentage()) {
|
||||
if (auto property = any_property_accepts_type(property_ids, ValueType::Percentage); property.has_value())
|
||||
return PropertyAndValue { *property, calculated };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue