@@ -80,4 +80,60 @@ describe("Utils tests", function () {
8080 expect ( ( ) => utils . pemToDer ( "not a pem" ) ) . to . throw ( ) ;
8181 } ) ;
8282 } ) ;
83+
84+ describe ( "findAttr" , function ( ) {
85+ it ( "should find attribute with no namespace when null is passed as namespace" , function ( ) {
86+ const xml =
87+ '<root testAttr="value" xmlns:ns="http://example.com" ns:testAttr="nsValue"></root>' ;
88+ const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
89+ const rootElement = doc . documentElement ;
90+
91+ const attr = utils . findAttr ( rootElement , "testAttr" , null ) ;
92+
93+ expect ( attr ) . to . not . be . null ;
94+ expect ( attr ?. value ) . to . equal ( "value" ) ;
95+ expect ( attr ?. namespaceURI ) . to . be . undefined ;
96+ } ) ;
97+
98+ it ( "should not find namespaced attribute when null is passed as namespace" , function ( ) {
99+ const xml = '<root xmlns:ns="http://example.com" ns:testAttr="nsValue"></root>' ;
100+ const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
101+ const rootElement = doc . documentElement ;
102+
103+ const attr = utils . findAttr ( rootElement , "testAttr" , null ) ;
104+
105+ expect ( attr ) . to . be . null ;
106+ } ) ;
107+
108+ it ( "should find namespaced attribute when matching namespace is provided" , function ( ) {
109+ const xml = '<root xmlns:ns="http://example.com" ns:testAttr="nsValue"></root>' ;
110+ const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
111+ const rootElement = doc . documentElement ;
112+
113+ const attr = utils . findAttr ( rootElement , "testAttr" , "http://example.com" ) ;
114+
115+ expect ( attr ) . to . not . be . null ;
116+ expect ( attr ?. value ) . to . equal ( "nsValue" ) ;
117+ expect ( attr ?. namespaceURI ) . to . equal ( "http://example.com" ) ;
118+ } ) ;
119+
120+ it ( "should distinguish between namespaced and non-namespaced attributes with same localName" , function ( ) {
121+ const xml =
122+ '<root testAttr="noNsValue" xmlns:ns="http://example.com" ns:testAttr="nsValue"></root>' ;
123+ const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
124+ const rootElement = doc . documentElement ;
125+
126+ // Find the non-namespaced attribute
127+ const noNsAttr = utils . findAttr ( rootElement , "testAttr" , null ) ;
128+ expect ( noNsAttr ) . to . not . be . null ;
129+ expect ( noNsAttr ?. value ) . to . equal ( "noNsValue" ) ;
130+ expect ( noNsAttr ?. namespaceURI ) . to . be . undefined ;
131+
132+ // Find the namespaced attribute
133+ const nsAttr = utils . findAttr ( rootElement , "testAttr" , "http://example.com" ) ;
134+ expect ( nsAttr ) . to . not . be . null ;
135+ expect ( nsAttr ?. value ) . to . equal ( "nsValue" ) ;
136+ expect ( nsAttr ?. namespaceURI ) . to . equal ( "http://example.com" ) ;
137+ } ) ;
138+ } ) ;
83139} ) ;
0 commit comments