2020-01-18 09:38:21 +01:00
/*
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2018 - 2020 , Andreas Kling < andreas @ ladybird . org >
2025-02-18 07:25:09 +00:00
* Copyright ( c ) 2025 , Tim Ledbetter < tim . ledbetter @ ladybird . org >
2020-01-18 09:38:21 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 09:38:21 +01:00
*/
2024-04-27 12:09:58 +12:00
# include <LibWeb/Bindings/HTMLHRElementPrototype.h>
2022-09-30 17:16:16 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2024-12-20 11:32:17 +01:00
# include <LibWeb/CSS/ComputedProperties.h>
2025-08-08 11:24:15 +01:00
# include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
2025-08-08 10:28:41 +01:00
# include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
2024-10-01 17:19:10 +01:00
# include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
2020-07-26 15:08:16 +02:00
# include <LibWeb/HTML/HTMLHRElement.h>
2025-09-10 20:45:24 +03:00
# include <LibWeb/HTML/Numbers.h>
2024-10-01 17:19:10 +01:00
# include <LibWeb/HTML/Parser/HTMLParser.h>
2019-10-01 20:50:11 +02:00
2020-07-28 18:20:36 +02:00
namespace Web : : HTML {
2020-03-07 10:27:02 +01:00
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( HTMLHRElement ) ;
2023-11-19 19:47:52 +01:00
2022-02-18 21:00:52 +01:00
HTMLHRElement : : HTMLHRElement ( DOM : : Document & document , DOM : : QualifiedName qualified_name )
2021-02-07 11:20:15 +01:00
: HTMLElement ( document , move ( qualified_name ) )
2019-10-01 20:50:11 +02:00
{
}
2022-03-14 13:21:51 -06:00
HTMLHRElement : : ~ HTMLHRElement ( ) = default ;
2023-01-10 06:28:20 -05:00
2023-08-07 08:41:28 +02:00
void HTMLHRElement : : initialize ( JS : : Realm & realm )
2023-01-10 06:28:20 -05:00
{
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( HTMLHRElement ) ;
2025-04-20 16:22:57 +02:00
Base : : initialize ( realm ) ;
2023-01-10 06:28:20 -05:00
}
2024-12-23 17:51:10 +01:00
bool HTMLHRElement : : is_presentational_hint ( FlyString const & name ) const
{
if ( Base : : is_presentational_hint ( name ) )
return true ;
2025-09-10 20:45:24 +03:00
return first_is_one_of ( name , HTML : : AttributeNames : : align , HTML : : AttributeNames : : color , HTML : : AttributeNames : : noshade , HTML : : AttributeNames : : width , HTML : : AttributeNames : : size ) ;
2024-12-23 17:51:10 +01:00
}
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
void HTMLHRElement : : apply_presentational_hints ( GC : : Ref < CSS : : CascadedProperties > cascaded_properties ) const
2024-10-01 17:19:10 +01:00
{
for_each_attribute ( [ & ] ( auto & name , auto & value ) {
2025-02-18 07:25:09 +00:00
// https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2
if ( name = = HTML : : AttributeNames : : color | | name = = HTML : : AttributeNames : : noshade ) {
2025-08-08 10:28:41 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderTopStyle , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderRightStyle , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderBottomStyle , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderLeftStyle , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) ) ;
2025-02-18 07:25:09 +00:00
}
2025-02-18 07:56:59 +00:00
if ( name = = HTML : : AttributeNames : : align ) {
if ( value . equals_ignoring_ascii_case ( " left " sv ) ) {
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginLeft , CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) ) ;
2025-08-08 10:28:41 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginRight , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) ) ;
2025-02-18 07:56:59 +00:00
} else if ( value . equals_ignoring_ascii_case ( " right " sv ) ) {
2025-08-08 10:28:41 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginLeft , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) ) ;
2025-02-18 07:56:59 +00:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginRight , CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) ) ;
} else if ( value . equals_ignoring_ascii_case ( " center " sv ) ) {
2025-08-08 10:28:41 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginLeft , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : MarginRight , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) ) ;
2025-02-18 07:56:59 +00:00
}
}
2025-02-18 07:25:09 +00:00
// https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2:attr-hr-color-3
// When an hr element has a color attribute, its value is expected to be parsed using the rules for parsing a legacy color value, and if that does not return failure,
// the user agent is expected to treat the attribute as a presentational hint setting the element's 'color' property to the resulting color.
if ( name = = HTML : : AttributeNames : : color ) {
if ( auto parsed_value = parse_legacy_color_value ( value ) ; parsed_value . has_value ( ) ) {
2025-08-08 11:24:15 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : Color , CSS : : ColorStyleValue : : create_from_color ( * parsed_value , CSS : : ColorSyntax : : Legacy ) ) ;
2025-02-18 07:25:09 +00:00
}
}
2025-09-10 20:45:24 +03:00
// If an hr element has either a color attribute or a noshade attribute, and furthermore also has a size attribute,
// and parsing that attribute's value using the rules for parsing non-negative integers doesn't generate an error,
// then the user agent is expected to use the parsed value divided by two as a pixel length for presentational hints
// for the properties 'border-top-width', 'border-right-width', 'border-bottom-width', and 'border-left-width' on the element.
bool has_color_or_noshade = has_attribute ( HTML : : AttributeNames : : color ) | | has_attribute ( HTML : : AttributeNames : : noshade ) ;
if ( name = = HTML : : AttributeNames : : size & & has_color_or_noshade ) {
if ( auto parsed_value = parse_non_negative_integer ( value ) ; parsed_value . has_value ( ) ) {
auto size_value = CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( parsed_value . value ( ) / 2.0 ) ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderTopWidth , size_value ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderRightWidth , size_value ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderBottomWidth , size_value ) ;
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderLeftWidth , size_value ) ;
}
} else if ( name = = HTML : : AttributeNames : : size & & ! has_color_or_noshade ) {
// Otherwise, if an hr element has neither a color attribute nor a noshade attribute, but does have a size attribute,
// and parsing that attribute's value using the rules for parsing non-negative integers doesn't generate an error,
// then: if the parsed value is one, then the user agent is expected to use the attribute as a presentational hint
// setting the element's 'border-bottom-width' to 0; otherwise, if the parsed value is greater than one,
// then the user agent is expected to use the parsed value minus two as a pixel length for presentational hints
// for the 'height' property on the element.
if ( auto parsed_value = parse_non_negative_integer ( value ) ; parsed_value . has_value ( ) ) {
if ( parsed_value . value ( ) = = 1 ) {
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : BorderBottomWidth ,
CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) ) ;
} else if ( parsed_value . value ( ) > 1 ) {
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : Height ,
CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( parsed_value . value ( ) - 2 ) ) ) ;
}
}
}
2024-10-01 17:19:10 +01:00
// https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2:maps-to-the-dimension-property
if ( name = = HTML : : AttributeNames : : width ) {
if ( auto parsed_value = parse_dimension_value ( value ) ) {
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
cascaded_properties - > set_property_from_presentational_hint ( CSS : : PropertyID : : Width , * parsed_value ) ;
2024-10-01 17:19:10 +01:00
}
}
} ) ;
}
2020-03-07 10:27:02 +01:00
}