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
*/
2026-04-18 10:54:06 +02:00
# include <LibWeb/Bindings/HTMLHRElement.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
}
2026-04-30 10:44:26 +01:00
void HTMLHRElement : : apply_presentational_hints ( Vector < CSS : : StyleProperty > & properties ) const
2024-10-01 17:19:10 +01:00
{
2026-04-30 10:44:26 +01:00
Base : : apply_presentational_hints ( properties ) ;
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 ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : BorderTopStyle , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderRightStyle , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderBottomStyle , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Solid ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderLeftStyle , . value = 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 ) ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : MarginLeft , . value = CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : MarginRight , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) } ) ;
2025-02-18 07:56:59 +00:00
} else if ( value . equals_ignoring_ascii_case ( " right " sv ) ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : MarginLeft , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : MarginRight , . value = CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) } ) ;
2025-02-18 07:56:59 +00:00
} else if ( value . equals_ignoring_ascii_case ( " center " sv ) ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : MarginLeft , . value = CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Auto ) } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : MarginRight , . value = 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 ( ) ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : Color , . value = 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 ) ) ;
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : BorderTopWidth , . value = size_value } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderRightWidth , . value = size_value } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderBottomWidth , . value = size_value } ) ;
properties . append ( { . property_id = CSS : : PropertyID : : BorderLeftWidth , . value = size_value } ) ;
2025-09-10 20:45:24 +03:00
}
} 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 ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : BorderBottomWidth , . value = CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( 0 ) ) } ) ;
2025-09-10 20:45:24 +03:00
} else if ( parsed_value . value ( ) > 1 ) {
2026-04-30 10:44:26 +01:00
properties . append ( { . property_id = CSS : : PropertyID : : Height , . value = CSS : : LengthStyleValue : : create ( CSS : : Length : : make_px ( parsed_value . value ( ) - 2 ) ) } ) ;
2025-09-10 20:45:24 +03:00
}
}
}
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 ) {
2026-04-30 10:44:26 +01:00
if ( auto parsed_value = parse_dimension_value ( value ) )
properties . append ( { . property_id = CSS : : PropertyID : : Width , . value = * parsed_value } ) ;
2024-10-01 17:19:10 +01:00
}
} ) ;
}
2020-03-07 10:27:02 +01:00
}