2021-09-28 15:17:11 +01:00
/*
2022-04-23 11:29:31 +01:00
* Copyright ( c ) 2021 - 2022 , Sam Atkins < atkinssj @ serenityos . org >
2021-09-28 15:17:11 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2021-10-08 16:40:50 +01:00
# include <AK/TypeCasts.h>
2022-09-24 16:34:04 -06:00
# include <LibWeb/Bindings/CSSRuleListPrototype.h>
# include <LibWeb/Bindings/Intrinsics.h>
2021-10-08 16:40:50 +01:00
# include <LibWeb/CSS/CSSImportRule.h>
2023-05-26 23:30:54 +03:30
# include <LibWeb/CSS/CSSKeyframesRule.h>
2021-10-08 17:02:47 +01:00
# include <LibWeb/CSS/CSSMediaRule.h>
2022-08-07 15:46:44 +02:00
# include <LibWeb/CSS/CSSRule.h>
2021-09-28 15:17:11 +01:00
# include <LibWeb/CSS/CSSRuleList.h>
2021-10-08 17:48:14 +01:00
# include <LibWeb/CSS/CSSSupportsRule.h>
2022-04-23 11:29:31 +01:00
# include <LibWeb/CSS/Parser/Parser.h>
2022-08-28 13:42:07 +02:00
# include <LibWeb/HTML/Window.h>
2021-09-28 15:17:11 +01:00
namespace Web : : CSS {
2023-11-19 19:47:52 +01:00
JS_DEFINE_ALLOCATOR ( CSSRuleList ) ;
2023-08-13 13:05:26 +02:00
JS : : NonnullGCPtr < CSSRuleList > CSSRuleList : : create ( JS : : Realm & realm , JS : : MarkedVector < CSSRule * > const & rules )
2022-08-07 13:51:40 +02:00
{
2023-08-13 13:05:26 +02:00
auto rule_list = realm . heap ( ) . allocate < CSSRuleList > ( realm , realm ) ;
2022-08-07 15:46:44 +02:00
for ( auto * rule : rules )
rule_list - > m_rules . append ( * rule ) ;
return rule_list ;
2022-08-07 13:51:40 +02:00
}
2022-09-24 16:34:04 -06:00
CSSRuleList : : CSSRuleList ( JS : : Realm & realm )
2024-01-09 16:05:03 -07:00
: Bindings : : PlatformObject ( realm )
2021-09-28 15:17:11 +01:00
{
2024-01-09 16:05:03 -07:00
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { . supports_indexed_properties = 1 } ;
2021-09-28 15:17:11 +01:00
}
2023-08-13 13:05:26 +02:00
JS : : NonnullGCPtr < CSSRuleList > CSSRuleList : : create_empty ( JS : : Realm & realm )
2022-08-07 15:46:44 +02:00
{
2023-08-13 13:05:26 +02:00
return realm . heap ( ) . allocate < CSSRuleList > ( realm , realm ) ;
2022-08-07 15:46:44 +02:00
}
2023-08-07 08:41:28 +02:00
void CSSRuleList : : initialize ( JS : : Realm & realm )
2023-01-10 06:56:59 -05:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( CSSRuleList ) ;
2023-01-10 06:56:59 -05:00
}
2022-08-07 15:46:44 +02:00
void CSSRuleList : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
for ( auto & rule : m_rules )
2023-02-26 16:09:02 -07:00
visitor . visit ( rule ) ;
2022-08-07 15:46:44 +02:00
}
2021-09-29 19:41:46 +02:00
bool CSSRuleList : : is_supported_property_index ( u32 index ) const
{
// The object’ s supported property indices are the numbers in the range zero to one less than the number of CSSRule objects represented by the collection.
// If there are no such CSSRule objects, then there are no supported property indices.
return index < m_rules . size ( ) ;
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#insert-a-css-rule
2022-09-25 17:03:42 +01:00
WebIDL : : ExceptionOr < unsigned > CSSRuleList : : insert_a_css_rule ( Variant < StringView , CSSRule * > rule , u32 index )
2021-09-29 21:12:39 +02:00
{
// 1. Set length to the number of items in list.
auto length = m_rules . size ( ) ;
// 2. If index is greater than length, then throw an IndexSizeError exception.
if ( index > length )
2023-09-06 16:03:01 +12:00
return WebIDL : : IndexSizeError : : create ( realm ( ) , " CSS rule index out of bounds. " _fly_string ) ;
2021-09-29 21:12:39 +02:00
// 3. Set new rule to the results of performing parse a CSS rule on argument rule.
2022-04-23 11:29:31 +01:00
// NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
// spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
// if that variant holds a CSSRule already.
2022-08-07 15:46:44 +02:00
CSSRule * new_rule = nullptr ;
2022-04-23 11:29:31 +01:00
if ( rule . has < StringView > ( ) ) {
2022-08-07 15:46:44 +02:00
new_rule = parse_css_rule (
2022-09-24 16:34:04 -06:00
CSS : : Parser : : ParsingContext { realm ( ) } ,
2022-08-07 15:46:44 +02:00
rule . get < StringView > ( ) ) ;
2022-04-23 11:29:31 +01:00
} else {
2022-08-07 15:46:44 +02:00
new_rule = rule . get < CSSRule * > ( ) ;
2022-04-23 11:29:31 +01:00
}
2021-09-29 21:12:39 +02:00
// 4. If new rule is a syntax error, throw a SyntaxError exception.
2022-04-23 11:29:31 +01:00
if ( ! new_rule )
2023-09-06 16:03:01 +12:00
return WebIDL : : SyntaxError : : create ( realm ( ) , " Unable to parse CSS rule. " _fly_string ) ;
2021-09-29 21:12:39 +02:00
// FIXME: 5. If new rule cannot be inserted into list at the zero-index position index due to constraints specified by CSS, then throw a HierarchyRequestError exception. [CSS21]
// FIXME: 6. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
// 7. Insert new rule into list at the zero-indexed position index.
2022-08-07 15:46:44 +02:00
m_rules . insert ( index , * new_rule ) ;
2021-09-29 21:12:39 +02:00
// 8. Return index.
2023-07-31 19:48:50 +01:00
if ( on_change )
on_change ( ) ;
2021-09-29 21:12:39 +02:00
return index ;
}
2021-10-15 19:38:39 +01:00
// https://www.w3.org/TR/cssom/#remove-a-css-rule
2022-09-25 17:03:42 +01:00
WebIDL : : ExceptionOr < void > CSSRuleList : : remove_a_css_rule ( u32 index )
2021-09-29 20:28:32 +02:00
{
// 1. Set length to the number of items in list.
auto length = m_rules . size ( ) ;
// 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
if ( index > = length )
2023-09-06 16:03:01 +12:00
return WebIDL : : IndexSizeError : : create ( realm ( ) , " CSS rule index out of bounds. " _fly_string ) ;
2021-09-29 20:28:32 +02:00
// 3. Set old rule to the indexth item in list.
2022-08-07 15:46:44 +02:00
CSSRule & old_rule = m_rules [ index ] ;
2021-09-29 20:28:32 +02:00
// FIXME: 4. If old rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
// 5. Remove rule old rule from list at the zero-indexed position index.
m_rules . remove ( index ) ;
2022-04-22 19:56:22 +01:00
// 6. Set old rule’ s parent CSS rule and parent CSS style sheet to null.
2022-08-07 15:46:44 +02:00
old_rule . set_parent_rule ( nullptr ) ;
old_rule . set_parent_style_sheet ( nullptr ) ;
2021-09-29 20:28:32 +02:00
2023-07-31 19:48:50 +01:00
if ( on_change )
on_change ( ) ;
2021-09-29 20:28:32 +02:00
return { } ;
}
2021-10-08 16:40:50 +01:00
void CSSRuleList : : for_each_effective_style_rule ( Function < void ( CSSStyleRule const & ) > const & callback ) const
{
2022-01-19 09:42:49 +01:00
for ( auto const & rule : m_rules ) {
2023-02-26 16:09:02 -07:00
switch ( rule - > type ( ) ) {
2022-03-28 20:30:26 +01:00
case CSSRule : : Type : : FontFace :
2021-10-08 17:02:47 +01:00
break ;
case CSSRule : : Type : : Import : {
2023-02-26 16:09:02 -07:00
auto const & import_rule = static_cast < CSSImportRule const & > ( * rule ) ;
2023-03-30 12:08:41 +02:00
if ( import_rule . loaded_style_sheet ( ) )
2021-10-08 16:40:50 +01:00
import_rule . loaded_style_sheet ( ) - > for_each_effective_style_rule ( callback ) ;
2021-10-08 17:02:47 +01:00
break ;
}
case CSSRule : : Type : : Media :
2023-02-26 16:09:02 -07:00
static_cast < CSSMediaRule const & > ( * rule ) . for_each_effective_style_rule ( callback ) ;
2021-10-08 17:02:47 +01:00
break ;
2022-03-28 20:30:26 +01:00
case CSSRule : : Type : : Style :
2023-02-26 16:09:02 -07:00
callback ( static_cast < CSSStyleRule const & > ( * rule ) ) ;
2022-03-28 20:30:26 +01:00
break ;
2021-10-08 17:48:14 +01:00
case CSSRule : : Type : : Supports :
2023-02-26 16:09:02 -07:00
static_cast < CSSSupportsRule const & > ( * rule ) . for_each_effective_style_rule ( callback ) ;
2021-10-08 17:48:14 +01:00
break ;
2023-05-26 23:30:54 +03:30
case CSSRule : : Type : : Keyframe :
case CSSRule : : Type : : Keyframes :
2023-07-29 11:51:15 -05:00
case CSSRule : : Type : : Namespace :
2023-05-26 23:30:54 +03:30
break ;
}
}
}
void CSSRuleList : : for_each_effective_keyframes_at_rule ( Function < void ( CSSKeyframesRule const & ) > const & callback ) const
{
for ( auto const & rule : m_rules ) {
switch ( rule - > type ( ) ) {
case CSSRule : : Type : : FontFace :
break ;
case CSSRule : : Type : : Import : {
auto const & import_rule = static_cast < CSSImportRule const & > ( * rule ) ;
if ( import_rule . loaded_style_sheet ( ) )
import_rule . loaded_style_sheet ( ) - > for_each_effective_keyframes_at_rule ( callback ) ;
break ;
}
case CSSRule : : Type : : Media :
static_cast < CSSMediaRule const & > ( * rule ) . for_each_effective_keyframes_at_rule ( callback ) ;
break ;
case CSSRule : : Type : : Style :
break ;
case CSSRule : : Type : : Supports :
static_cast < CSSSupportsRule const & > ( * rule ) . for_each_effective_keyframes_at_rule ( callback ) ;
break ;
case CSSRule : : Type : : Keyframe :
break ;
case CSSRule : : Type : : Keyframes :
callback ( static_cast < CSSKeyframesRule const & > ( * rule ) ) ;
break ;
2023-07-29 11:51:15 -05:00
case CSSRule : : Type : : Namespace :
break ;
2021-10-08 16:40:50 +01:00
}
}
}
2022-03-07 23:08:26 +01:00
bool CSSRuleList : : evaluate_media_queries ( HTML : : Window const & window )
2021-10-08 20:21:46 +01:00
{
2022-02-17 12:34:36 +00:00
bool any_media_queries_changed_match_state = false ;
2021-10-08 20:21:46 +01:00
for ( auto & rule : m_rules ) {
2023-02-26 16:09:02 -07:00
switch ( rule - > type ( ) ) {
2022-03-28 20:30:26 +01:00
case CSSRule : : Type : : FontFace :
2021-10-08 20:21:46 +01:00
break ;
case CSSRule : : Type : : Import : {
2023-02-26 16:09:02 -07:00
auto & import_rule = verify_cast < CSSImportRule > ( * rule ) ;
2023-03-30 12:08:41 +02:00
if ( import_rule . loaded_style_sheet ( ) & & import_rule . loaded_style_sheet ( ) - > evaluate_media_queries ( window ) )
2022-02-17 12:34:36 +00:00
any_media_queries_changed_match_state = true ;
2021-10-08 20:21:46 +01:00
break ;
}
case CSSRule : : Type : : Media : {
2023-02-26 16:09:02 -07:00
auto & media_rule = verify_cast < CSSMediaRule > ( * rule ) ;
2022-02-17 12:34:36 +00:00
bool did_match = media_rule . condition_matches ( ) ;
bool now_matches = media_rule . evaluate ( window ) ;
if ( did_match ! = now_matches )
any_media_queries_changed_match_state = true ;
if ( now_matches & & media_rule . css_rules ( ) . evaluate_media_queries ( window ) )
any_media_queries_changed_match_state = true ;
2021-10-08 20:21:46 +01:00
break ;
}
2022-03-28 20:30:26 +01:00
case CSSRule : : Type : : Style :
break ;
2021-10-08 20:21:46 +01:00
case CSSRule : : Type : : Supports : {
2023-02-26 16:09:02 -07:00
auto & supports_rule = verify_cast < CSSSupportsRule > ( * rule ) ;
2022-02-17 12:34:36 +00:00
if ( supports_rule . condition_matches ( ) & & supports_rule . css_rules ( ) . evaluate_media_queries ( window ) )
any_media_queries_changed_match_state = true ;
2021-10-08 20:21:46 +01:00
break ;
}
2023-05-26 23:30:54 +03:30
case CSSRule : : Type : : Keyframe :
case CSSRule : : Type : : Keyframes :
2023-07-29 11:51:15 -05:00
case CSSRule : : Type : : Namespace :
2023-05-26 23:30:54 +03:30
break ;
2021-10-08 20:21:46 +01:00
}
}
2022-02-17 12:34:36 +00:00
return any_media_queries_changed_match_state ;
2021-10-08 20:21:46 +01:00
}
2023-02-28 00:05:39 +00:00
WebIDL : : ExceptionOr < JS : : Value > CSSRuleList : : item_value ( size_t index ) const
2022-08-07 13:51:40 +02:00
{
return item ( index ) ;
}
2021-09-28 15:17:11 +01:00
}