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
mongo-express - Web-based MongoDB admin interface, written with No... 2017-05-17
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
android - Smartisan open source code for full build.(repo ma... 2017-05-16
word_cloud - A little word cloud generator in Pytho 2017-05-16
mobileplayer-ios - 2017-05-16
Destroy-Windows-10-Spying - Destroy Windows Spying tool ... 2017-05-15

 Back to top