Module: Dommy::NodeFilter

Defined in:
lib/dommy/tree_walker.rb

Overview

NodeFilter constants — bitmasks for ‘whatToShow` and return values for the optional filter callable. Standard DOM Level 2 Traversal.

Constant Summary collapse

SHOW_ALL =
0xFFFFFFFF
SHOW_ELEMENT =
0x1
SHOW_ATTRIBUTE =
0x2
SHOW_TEXT =
0x4
SHOW_CDATA_SECTION =
0x8
SHOW_PROCESSING_INSTRUCTION =
0x40
SHOW_COMMENT =
0x80
SHOW_DOCUMENT =
0x100
SHOW_DOCUMENT_TYPE =
0x200
SHOW_DOCUMENT_FRAGMENT =
0x400
FILTER_ACCEPT =
1
FILTER_REJECT =
2
FILTER_SKIP =
3

Class Method Summary collapse

Class Method Details

.bitmask_for(node) ⇒ Object

Map a wrapped Dommy node to its NodeFilter bitmask. Returns 0 for unknown node types (effectively “doesn’t pass any filter”). whatToShow bit for a node: WHATWG defines it as ‘1 << (nodeType - 1)`, which covers every node type (Element, Text, CDATASection, PI, Comment, Document, DocumentType, DocumentFragment, Attribute) uniformly.



27
28
29
30
31
32
33
34
35
# File 'lib/dommy/tree_walker.rb', line 27

def self.bitmask_for(node)
  nt =
    if node.respond_to?(:node_type) then node.node_type
    elsif node.respond_to?(:__js_get__) then node.__js_get__("nodeType")
    end
  return 0 unless nt.is_a?(Integer) && nt >= 1

  1 << (nt - 1)
end