Namespaces in Ext DomQuery

Do you want to use custom attributes in your XHTML? Do you use the YUI Library and Jack Slocum’s wonderful DomQuery selector engine?
If you want to select attribute nodes with a namespace in your XHTML, DomQuery does not support namespaces as an op…


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman

Do you want to use custom attributes in your XHTML? Do you use the YUI Library and Jack Slocum’s wonderful DomQuery selector engine?

If you want to select attribute nodes with a namespace in your XHTML, DomQuery does not support namespaces as an option to do so. But by adding a small snippet of code to DomQuery, we can make it do so.

As a standalone snippet executed after DomQuery is loaded.

Ext.DomQuery.matchers.push( {
  re: /^(?:([\[\{])(?:@)?([\w-]+(?:\:[\w-]+))\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
  select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
} );

OR by adding an array entry into the DomQuery code matchers array (paste after line 479 of the 17 January 2007 2:26:32 PM version)

,{
  re: /^(?:([\[\{])(?:@)?([\w-]+(?:\:[\w-]+))\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
  select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
}

I did not modify the existing matcher for attribute selection because I didn’t want to modify the speed of the existing code for non-namespaced attributes. So by appending another entry to the end of the array, it’ll be used as a last resort if the other entries aren’t matched.

This approach relies on the assumption that when selecting an attribute with getAttribute, the browser interprets any namespace automatically: getAttribute(‘myNamespace:myAttributeName’), as documented by the peterned weblog. This was tested in Firefox 2 and IE 6.

Usage:

Ext.query( '#test-data span[myNameSpace:myAttribute=myValue]' );

on the following DOM

<div id="test-data">
<span myNameSpace:myAttribute="myValue"></span>
</div>

Other notes regarding DomQuery:

To select a node with a non-empty attribute value:

Ext.query( '#test-data span[myNameSpace:myAttribute]' );


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman


Print Share Comment Cite Upload Translate Updates
APA

Zach Leatherman | Sciencx (2007-03-06T06:00:00+00:00) Namespaces in Ext DomQuery. Retrieved from https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/

MLA
" » Namespaces in Ext DomQuery." Zach Leatherman | Sciencx - Tuesday March 6, 2007, https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/
HARVARD
Zach Leatherman | Sciencx Tuesday March 6, 2007 » Namespaces in Ext DomQuery., viewed ,<https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/>
VANCOUVER
Zach Leatherman | Sciencx - » Namespaces in Ext DomQuery. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/
CHICAGO
" » Namespaces in Ext DomQuery." Zach Leatherman | Sciencx - Accessed . https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/
IEEE
" » Namespaces in Ext DomQuery." Zach Leatherman | Sciencx [Online]. Available: https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/. [Accessed: ]
rf:citation
» Namespaces in Ext DomQuery | Zach Leatherman | Sciencx | https://www.scien.cx/2007/03/06/namespaces-in-ext-domquery/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.