Node Masking
One of the core bits of functionality of the elementals.js is how various DOM walking functions are able to mask the returned or walked elements by either their Element.nodeType
or Element.tagName
.
All functions that take a nodeMask can accept either a tagName
or nodeMask
. The former is relatively self explanatory. If you were to call:
var firstLi = _.Node..first(_d.body, 'li');
You will get the first LI
in the document body.
Using a nodeMask is a bit more complex. It is performed using a binary mask where the Element.nodeType being tested is used to shift the value "1" left by nodeType minus 1. This value is then binary "and"ed against the mask as our test.
For example, if we wanted to find text nodes -- aka nodeType 3 -- we would send a mask of 4, as 1 << (3 - 1) == 4
. Because a binary AND is performed, we can check for multiple nodeTypes at once. For example if we wanted to search for TEXT_NODE (type 3, mask 4) and COMMENT_NODE (type 8, mask 128) at the same time, we would pass 131, aka 0x84 hex.
The following values are predefined in the Elementals library to aid with this:
_.Node.mask.ELEMENT = 0x0001; // nodeType 1
_.Node.mask.TEXT = 0x0004; // nodeType 3
_.Node.mask.PROCESSING = 0x0040; // nodeType 7
_.Node.mask.COMMENT = 0x0080; // nodeType 8
_.Node.mask.DOCUMENT = 0x0100; // nodeType 9
_.Node.mask.DOCTYPE = 0x0200; // nodetype 10
_.Node.mask.FRAGMENT = 0x0400; // nodeType 11
No reference values are provided for deprecated node types.
As such if you wanted to search body for the first text or comment node, you could simply call:
var e = _.Node..first(_d.body, _.Node.mask.TEXT || _.Node.mask.COMMENT);
On all functions that verify node masks, if no mask or tagName is passed _.Node.mask.ELEMENT
is used as the default.