ladybird/Libraries/LibWeb/Animations/PseudoElementParsing.cpp
Sam Atkins 6a4d80b9b6 LibWeb/CSS: Integrate ParsingContext into the Parser
This is not really a context, but more of a set of parameters for
creating a Parser. So, treat it as such: Rename it to ParsingParams,
and store its values and methods directly in the Parser instead of
keeping the ParsingContext around.

This has a nice side-effect of not including DOM/Document.h everywhere
that needs a Parser.
2025-02-06 16:47:25 +00:00

38 lines
1.5 KiB
C++

/*
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "PseudoElementParsing.h"
#include <LibWeb/CSS/Parser/Parser.h>
namespace Web::Animations {
// https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-pseudo-element-parsing
WebIDL::ExceptionOr<Optional<CSS::Selector::PseudoElement>> pseudo_element_parsing(JS::Realm& realm, Optional<String> const& value)
{
// 1. Given the value value, perform the following steps:
// 2. If value is not null and is an invalid <pseudo-element-selector>,
Optional<CSS::Selector::PseudoElement> pseudo_element;
if (value.has_value()) {
pseudo_element = parse_pseudo_element_selector(CSS::Parser::ParsingParams { realm }, *value);
if (!pseudo_element.has_value()) {
// 1. Throw a DOMException with error name "SyntaxError".
// 2. Abort.
return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid pseudo-element selector: \"{}\"", value.value())));
}
}
// 3. If value is one of the legacy Selectors Level 2 single-colon selectors (':before', ':after', ':first-letter', or ':first-line'),
// then return the equivalent two-colon selector (e.g. '::before').
if (value.has_value() && value->is_one_of(":before", ":after", ":first-letter", ":first-line")) {
return CSS::Selector::PseudoElement::from_string(MUST(value->substring_from_byte_offset(1)));
}
// 4. Otherwise, return value.
return pseudo_element;
}
}