BVB Source Codes

Squire Show TreeWalker.js Source code

Return Download Squire: download TreeWalker.js Source code - Download Squire Source code - Type:.js
  1. /*jshint strict:false */
  2.  
  3. /*
  4.     Native TreeWalker is buggy in IE and Opera:
  5.     * IE9/10 sometimes throw errors when calling TreeWalker#nextNode or
  6.       TreeWalker#previousNode. No way to feature detect this.
  7.     * Some versions of Opera have a bug in TreeWalker#previousNode which makes
  8.       it skip to the wrong node.
  9.  
  10.     Rather than risk further bugs, it's easiest just to implement our own
  11.     (subset) of the spec in all browsers.
  12. */
  13.  
  14. var typeToBitArray = {
  15.     // ELEMENT_NODE
  16.     1: 1,
  17.     // ATTRIBUTE_NODE
  18.     2: 2,
  19.     // TEXT_NODE
  20.     3: 4,
  21.     // COMMENT_NODE
  22.     8: 128,
  23.     // DOCUMENT_NODE
  24.     9: 256,
  25.     // DOCUMENT_FRAGMENT_NODE
  26.     11: 1024
  27. };
  28.  
  29. function TreeWalker ( root, nodeType, filter ) {
  30.     this.root = this.currentNode = root;
  31.     this.nodeType = nodeType;
  32.     this.filter = filter;
  33. }
  34.  
  35. TreeWalker.prototype.nextNode = function () {
  36.     var current = this.currentNode,
  37.         root = this.root,
  38.         nodeType = this.nodeType,
  39.         filter = this.filter,
  40.         node;
  41.     while ( true ) {
  42.         node = current.firstChild;
  43.         while ( !node && current ) {
  44.             if ( current === root ) {
  45.                 break;
  46.             }
  47.             node = current.nextSibling;
  48.             if ( !node ) { current = current.parentNode; }
  49.         }
  50.         if ( !node ) {
  51.             return null;
  52.         }
  53.         if ( ( typeToBitArray[ node.nodeType ] & nodeType ) &&
  54.                 filter( node ) ) {
  55.             this.currentNode = node;
  56.             return node;
  57.         }
  58.         current = node;
  59.     }
  60. };
  61.  
  62. TreeWalker.prototype.previousNode = function () {
  63.     var current = this.currentNode,
  64.         root = this.root,
  65.         nodeType = this.nodeType,
  66.         filter = this.filter,
  67.         node;
  68.     while ( true ) {
  69.         if ( current === root ) {
  70.             return null;
  71.         }
  72.         node = current.previousSibling;
  73.         if ( node ) {
  74.             while ( current = node.lastChild ) {
  75.                 node = current;
  76.             }
  77.         } else {
  78.             node = current.parentNode;
  79.         }
  80.         if ( !node ) {
  81.             return null;
  82.         }
  83.         if ( ( typeToBitArray[ node.nodeType ] & nodeType ) &&
  84.                 filter( node ) ) {
  85.             this.currentNode = node;
  86.             return node;
  87.         }
  88.         current = node;
  89.     }
  90. };
  91.  
  92. // Previous node in post-order.
  93. TreeWalker.prototype.previousPONode = function () {
  94.     var current = this.currentNode,
  95.         root = this.root,
  96.         nodeType = this.nodeType,
  97.         filter = this.filter,
  98.         node;
  99.     while ( true ) {
  100.         node = current.lastChild;
  101.         while ( !node && current ) {
  102.             if ( current === root ) {
  103.                 break;
  104.             }
  105.             node = current.previousSibling;
  106.             if ( !node ) { current = current.parentNode; }
  107.         }
  108.         if ( !node ) {
  109.             return null;
  110.         }
  111.         if ( ( typeToBitArray[ node.nodeType ] & nodeType ) &&
  112.                 filter( node ) ) {
  113.             this.currentNode = node;
  114.             return node;
  115.         }
  116.         current = node;
  117.     }
  118. };
  119.  
downloadTreeWalker.js Source code - Download Squire Source code
Related Source Codes/Software:
thor - Thor is a toolkit for building powerful command-li... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
TextFieldEffects - Custom UITextFields effects inspired by Codrops, b... 2017-01-08
flowchart.js - Draws simple SVG flow chart diagrams from textual ... 2017-01-08
RoundedImageView - A fast ImageView that supports rounded corners, ov... 2017-01-07
webpack-demos - a collection of simple demos of Webpack 2017-01-08
amazon-dsstne - Deep Scalable Sparse Tensor Network Engine (DSSTNE... 2017-01-08
rq - Simple job queues for Python ht... 2017-01-08
emmet-vim - emmet for vim: http://emmet.io/ ... 2017-01-08
prose - A Content Editor for GitHub. ht... 2017-01-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top