@@ -1401,11 +1401,28 @@ impl Element {
14011401 // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
14021402 pub fn get_attribute_by_name ( & self , name : DOMString ) -> Option < DomRoot < Attr > > {
14031403 let name = & self . parsed_name ( name) ;
1404- self . attrs
1404+ let maybe_attribute = self
1405+ . attrs
14051406 . borrow ( )
14061407 . iter ( )
14071408 . find ( |a| a. name ( ) == name)
1408- . map ( |js| DomRoot :: from_ref ( & * * js) )
1409+ . map ( |js| DomRoot :: from_ref ( & * * js) ) ;
1410+
1411+ fn id_and_name_must_be_atoms ( name : & LocalName , maybe_attr : & Option < DomRoot < Attr > > ) -> bool {
1412+ if * name == local_name ! ( "id" ) || * name == local_name ! ( "name" ) {
1413+ match maybe_attr {
1414+ None => true ,
1415+ Some ( ref attr) => match * attr. value ( ) {
1416+ AttrValue :: Atom ( _) => true ,
1417+ _ => false ,
1418+ } ,
1419+ }
1420+ } else {
1421+ true
1422+ }
1423+ }
1424+ debug_assert ! ( id_and_name_must_be_atoms( name, & maybe_attribute) ) ;
1425+ maybe_attribute
14091426 }
14101427
14111428 pub fn set_attribute_from_parser (
@@ -1800,6 +1817,14 @@ impl Element {
18001817 let other = other. upcast :: < Element > ( ) ;
18011818 self . root_element ( ) == other. root_element ( )
18021819 }
1820+
1821+ pub fn get_id ( & self ) -> Option < Atom > {
1822+ self . id_attribute . borrow ( ) . clone ( )
1823+ }
1824+
1825+ pub fn get_name ( & self ) -> Option < Atom > {
1826+ self . rare_data ( ) . as_ref ( ) ?. name_attribute . clone ( )
1827+ }
18031828}
18041829
18051830impl ElementMethods for Element {
@@ -1836,6 +1861,8 @@ impl ElementMethods for Element {
18361861 }
18371862
18381863 // https://dom.spec.whatwg.org/#dom-element-id
1864+ // This always returns a string; if you'd rather see None
1865+ // on a null id, call get_id
18391866 fn Id ( & self ) -> DOMString {
18401867 self . get_string_attribute ( & local_name ! ( "id" ) )
18411868 }
@@ -2783,6 +2810,20 @@ impl VirtualMethods for Element {
27832810 }
27842811 }
27852812 } ,
2813+ & local_name ! ( "name" ) => {
2814+ // Keep the name in rare data for fast access
2815+ self . ensure_rare_data ( ) . name_attribute =
2816+ mutation. new_value ( attr) . and_then ( |value| {
2817+ let value = value. as_atom ( ) ;
2818+ if value != & atom ! ( "" ) {
2819+ Some ( value. clone ( ) )
2820+ } else {
2821+ None
2822+ }
2823+ } ) ;
2824+ // TODO: notify the document about the name change
2825+ // once it has a name_map (#25548)
2826+ } ,
27862827 _ => {
27872828 // FIXME(emilio): This is pretty dubious, and should be done in
27882829 // the relevant super-classes.
@@ -2801,6 +2842,7 @@ impl VirtualMethods for Element {
28012842 fn parse_plain_attribute ( & self , name : & LocalName , value : DOMString ) -> AttrValue {
28022843 match name {
28032844 & local_name ! ( "id" ) => AttrValue :: from_atomic ( value. into ( ) ) ,
2845+ & local_name ! ( "name" ) => AttrValue :: from_atomic ( value. into ( ) ) ,
28042846 & local_name ! ( "class" ) => AttrValue :: from_serialized_tokenlist ( value. into ( ) ) ,
28052847 _ => self
28062848 . super_type ( )
0 commit comments