2021-10-15 09:57:07 -04:00
/*
2022-01-31 13:07:22 -05:00
* Copyright ( c ) 2021 , Tim Flynn < trflynn89 @ serenityos . org >
2021-10-15 09:57:07 -04:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2022-09-25 16:15:49 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2022-09-18 01:03:58 +02:00
# include <LibWeb/DOM/Attr.h>
2021-10-15 09:57:07 -04:00
# include <LibWeb/DOM/Document.h>
2021-10-17 15:09:47 -04:00
# include <LibWeb/DOM/Element.h>
2022-07-11 16:40:01 +01:00
# include <LibWeb/DOM/MutationType.h>
# include <LibWeb/DOM/StaticNodeList.h>
2021-10-15 09:57:07 -04:00
namespace Web : : DOM {
2023-01-08 19:23:00 -05:00
JS : : NonnullGCPtr < Attr > Attr : : create ( Document & document , DeprecatedFlyString local_name , DeprecatedString value , Element const * owner_element )
2021-10-15 09:57:07 -04:00
{
2022-12-14 17:40:33 +00:00
return document . heap ( ) . allocate < Attr > ( document . realm ( ) , document , QualifiedName ( move ( local_name ) , { } , { } ) , move ( value ) , owner_element ) ;
2021-10-15 09:57:07 -04:00
}
2022-12-13 13:08:46 +01:00
JS : : NonnullGCPtr < Attr > Attr : : clone ( Document & document )
{
return * heap ( ) . allocate < Attr > ( realm ( ) , document , m_qualified_name , m_value , nullptr ) ;
}
Attr : : Attr ( Document & document , QualifiedName qualified_name , DeprecatedString value , Element const * owner_element )
2021-10-15 09:57:07 -04:00
: Node ( document , NodeType : : ATTRIBUTE_NODE )
2022-12-13 13:08:46 +01:00
, m_qualified_name ( move ( qualified_name ) )
2021-10-15 09:57:07 -04:00
, m_value ( move ( value ) )
2021-10-15 12:03:08 -04:00
, m_owner_element ( owner_element )
2021-10-15 09:57:07 -04:00
{
2022-09-25 16:15:49 -06:00
set_prototype ( & Bindings : : cached_web_prototype ( document . realm ( ) , " Attr " ) ) ;
2022-08-28 13:42:07 +02:00
}
2022-09-18 01:03:58 +02:00
void Attr : : visit_edges ( Cell : : Visitor & visitor )
2022-08-28 13:42:07 +02:00
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( m_owner_element . ptr ( ) ) ;
2021-10-15 09:57:07 -04:00
}
2022-09-18 01:03:58 +02:00
Element * Attr : : owner_element ( )
2022-07-11 16:40:01 +01:00
{
2022-08-28 13:42:07 +02:00
return m_owner_element . ptr ( ) ;
2022-07-11 16:40:01 +01:00
}
2022-09-18 01:03:58 +02:00
Element const * Attr : : owner_element ( ) const
2021-10-17 15:09:47 -04:00
{
2022-08-28 13:42:07 +02:00
return m_owner_element . ptr ( ) ;
2021-10-17 15:09:47 -04:00
}
2022-09-18 01:03:58 +02:00
void Attr : : set_owner_element ( Element const * owner_element )
2021-10-17 15:09:47 -04:00
{
m_owner_element = owner_element ;
}
2022-07-11 16:40:01 +01:00
// https://dom.spec.whatwg.org/#set-an-existing-attribute-value
2022-12-04 18:02:33 +00:00
void Attr : : set_value ( DeprecatedString value )
2022-07-11 16:40:01 +01:00
{
// 1. If attribute’ s element is null, then set attribute’ s value to value.
if ( ! owner_element ( ) ) {
m_value = move ( value ) ;
return ;
}
// 2. Otherwise, change attribute to value.
// https://dom.spec.whatwg.org/#concept-element-attributes-change
// 1. Handle attribute changes for attribute with attribute’ s element, attribute’ s value, and value.
handle_attribute_changes ( * owner_element ( ) , m_value , value ) ;
// 2. Set attribute’ s value to value.
m_value = move ( value ) ;
}
// https://dom.spec.whatwg.org/#handle-attribute-changes
2022-12-04 18:02:33 +00:00
void Attr : : handle_attribute_changes ( Element & element , DeprecatedString const & old_value , [[maybe_unused]] DeprecatedString const & new_value )
2022-07-11 16:40:01 +01:00
{
// 1. Queue a mutation record of "attributes" for element with attribute’ s local name, attribute’ s namespace, oldValue, « », « », null, and null.
2022-09-25 16:15:49 -06:00
element . queue_mutation_record ( MutationType : : attributes , local_name ( ) , namespace_uri ( ) , old_value , StaticNodeList : : create ( realm ( ) , { } ) , StaticNodeList : : create ( realm ( ) , { } ) , nullptr , nullptr ) ;
2022-07-11 16:40:01 +01:00
// FIXME: 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an argument list containing attribute’ s local name, oldValue, newValue, and attribute’ s namespace.
// FIXME: 3. Run the attribute change steps with element, attribute’ s local name, oldValue, newValue, and attribute’ s namespace.
}
2021-10-15 09:57:07 -04:00
}