2020-01-18 09:38:21 +01:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 09:38:21 +01:00
*/
2022-09-24 16:34:04 -06:00
# include <LibWeb/Bindings/CSSStyleRulePrototype.h>
# include <LibWeb/Bindings/Intrinsics.h>
2021-03-07 15:00:02 +01:00
# include <LibWeb/CSS/CSSStyleRule.h>
2021-10-15 16:53:38 +01:00
# include <LibWeb/CSS/Parser/Parser.h>
2023-02-12 23:38:05 +01:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2019-06-21 19:19:49 +02:00
2020-07-26 20:01:35 +02:00
namespace Web : : CSS {
2020-03-07 10:27:02 +01:00
2023-11-19 19:47:52 +01:00
JS_DEFINE_ALLOCATOR ( CSSStyleRule ) ;
2023-08-13 13:05:26 +02:00
JS : : NonnullGCPtr < CSSStyleRule > CSSStyleRule : : create ( JS : : Realm & realm , Vector < NonnullRefPtr < Web : : CSS : : Selector > > & & selectors , CSSStyleDeclaration & declaration )
2022-08-07 15:46:44 +02:00
{
2023-08-13 13:05:26 +02:00
return realm . heap ( ) . allocate < CSSStyleRule > ( realm , realm , move ( selectors ) , declaration ) ;
2022-08-07 15:46:44 +02:00
}
2023-03-06 14:17:01 +01:00
CSSStyleRule : : CSSStyleRule ( JS : : Realm & realm , Vector < NonnullRefPtr < Selector > > & & selectors , CSSStyleDeclaration & declaration )
2022-09-24 16:34:04 -06:00
: CSSRule ( realm )
2022-08-07 15:46:44 +02:00
, m_selectors ( move ( selectors ) )
2022-08-07 16:21:26 +02:00
, m_declaration ( declaration )
2019-06-21 19:19:49 +02:00
{
2023-01-10 06:28:20 -05:00
}
2023-08-07 08:41:28 +02:00
void CSSStyleRule : : initialize ( JS : : Realm & realm )
2023-01-10 06:28:20 -05:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2023-11-22 12:55:21 +13:00
set_prototype ( & Bindings : : ensure_web_prototype < Bindings : : CSSStyleRulePrototype > ( realm , " CSSStyleRule " _fly_string ) ) ;
2019-06-21 20:54:13 +02:00
}
2019-06-21 19:19:49 +02:00
2022-08-07 16:21:26 +02:00
void CSSStyleRule : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
2023-02-26 16:09:02 -07:00
visitor . visit ( m_declaration ) ;
2022-08-07 16:21:26 +02:00
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#dom-cssstylerule-style
2021-10-01 19:57:45 +02:00
CSSStyleDeclaration * CSSStyleRule : : style ( )
{
2023-02-26 16:09:02 -07:00
return m_declaration ;
2021-10-01 19:57:45 +02:00
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#serialize-a-css-rule
2023-11-20 23:16:39 +13:00
String CSSStyleRule : : serialized ( ) const
2021-10-01 19:57:45 +02:00
{
StringBuilder builder ;
// 1. Let s initially be the result of performing serialize a group of selectors on the rule’ s associated selectors,
// followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
2023-08-22 12:45:29 +01:00
builder . append ( serialize_a_group_of_selectors ( selectors ( ) ) ) ;
2021-10-01 19:57:45 +02:00
builder . append ( " { " sv ) ;
// 2. Let decls be the result of performing serialize a CSS declaration block on the rule’ s associated declarations, or null if there are no such declarations.
2023-11-21 10:39:54 +13:00
auto decls = declaration ( ) . length ( ) > 0 ? declaration ( ) . serialized ( ) : Optional < String > ( ) ;
2021-10-01 19:57:45 +02:00
// FIXME: 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’ s cssRules list, or null if there are no such rules.
2023-11-20 23:16:39 +13:00
Optional < String > rules ;
2021-10-01 19:57:45 +02:00
// 4. If decls and rules are both null, append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)) and return s.
2023-10-10 15:00:58 +03:30
if ( ! decls . has_value ( ) & & ! rules . has_value ( ) ) {
2021-10-01 19:57:45 +02:00
builder . append ( " } " sv ) ;
2023-11-20 23:16:39 +13:00
return MUST ( builder . to_string ( ) ) ;
2021-10-01 19:57:45 +02:00
}
// 5. If rules is null:
2023-10-10 15:00:58 +03:30
if ( ! rules . has_value ( ) ) {
2021-10-01 19:57:45 +02:00
// 1. Append a single SPACE (U+0020) to s
builder . append ( ' ' ) ;
// 2. Append decls to s
2023-10-10 15:00:58 +03:30
builder . append ( * decls ) ;
2021-10-01 19:57:45 +02:00
// 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
builder . append ( " } " sv ) ;
// 4. Return s.
2023-11-20 23:16:39 +13:00
return MUST ( builder . to_string ( ) ) ;
2021-10-01 19:57:45 +02:00
}
// FIXME: 6. Otherwise:
// FIXME: 1. If decls is not null, prepend it to rules.
// FIXME: 2. For each rule in rules:
// FIXME: 1. Append a newline followed by two spaces to s.
// FIXME: 2. Append rule to s.
// FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
// FIXME: 4. Return s.
TODO ( ) ;
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
2022-12-04 18:02:33 +00:00
DeprecatedString CSSStyleRule : : selector_text ( ) const
2021-09-29 23:43:18 +02:00
{
2021-10-01 19:57:45 +02:00
// The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
2023-08-22 12:45:29 +01:00
return serialize_a_group_of_selectors ( selectors ( ) ) . to_deprecated_string ( ) ;
2021-09-29 23:43:18 +02:00
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
2021-09-29 23:43:18 +02:00
void CSSStyleRule : : set_selector_text ( StringView selector_text )
{
2021-10-15 16:53:38 +01:00
// 1. Run the parse a group of selectors algorithm on the given value.
2023-02-28 16:54:25 +00:00
auto parsed_selectors = parse_selector ( Parser : : ParsingContext { realm ( ) } , selector_text ) ;
2021-09-29 23:43:18 +02:00
2021-10-15 16:53:38 +01:00
// 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
if ( parsed_selectors . has_value ( ) )
m_selectors = parsed_selectors . release_value ( ) ;
2021-09-29 23:43:18 +02:00
2021-10-15 16:53:38 +01:00
// 3. Otherwise, if the algorithm returns a null value, do nothing.
2021-09-29 23:43:18 +02:00
}
2020-03-07 10:27:02 +01:00
}