react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / htmlparser2 / node_modules / domutils / lib / helpers.js
81149 views// removeSubsets1// Given an array of nodes, remove any member that is contained by another.2exports.removeSubsets = function(nodes) {3var idx = nodes.length, node, ancestor, replace;45// Check if each node (or one of its ancestors) is already contained in the6// array.7while (--idx > -1) {8node = ancestor = nodes[idx];910// Temporarily remove the node under consideration11nodes[idx] = null;12replace = true;1314while (ancestor) {15if (nodes.indexOf(ancestor) > -1) {16replace = false;17nodes.splice(idx, 1);18break;19}20ancestor = ancestor.parent;21}2223// If the node has been found to be unique, re-insert it.24if (replace) {25nodes[idx] = node;26}27}2829return nodes;30};3132// Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition33var POSITION = {34DISCONNECTED: 1,35PRECEDING: 2,36FOLLOWING: 4,37CONTAINS: 8,38CONTAINED_BY: 1639};4041// Compare the position of one node against another node in any other document.42// The return value is a bitmask with the following values:43//44// document order:45// > There is an ordering, document order, defined on all the nodes in the46// > document corresponding to the order in which the first character of the47// > XML representation of each node occurs in the XML representation of the48// > document after expansion of general entities. Thus, the document element49// > node will be the first node. Element nodes occur before their children.50// > Thus, document order orders element nodes in order of the occurrence of51// > their start-tag in the XML (after expansion of entities). The attribute52// > nodes of an element occur after the element and before its children. The53// > relative order of attribute nodes is implementation-dependent./54// Source:55// http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order56//57// @argument {Node} nodaA The first node to use in the comparison58// @argument {Node} nodeB The second node to use in the comparison59//60// @return {Number} A bitmask describing the input nodes' relative position.61// See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for62// a description of these values.63var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) {64var aParents = [];65var bParents = [];66var current, sharedParent, siblings, aSibling, bSibling, idx;6768if (nodeA === nodeB) {69return 0;70}7172current = nodeA;73while (current) {74aParents.unshift(current);75current = current.parent;76}77current = nodeB;78while (current) {79bParents.unshift(current);80current = current.parent;81}8283idx = 0;84while (aParents[idx] === bParents[idx]) {85idx++;86}8788if (idx === 0) {89return POSITION.DISCONNECTED;90}9192sharedParent = aParents[idx - 1];93siblings = sharedParent.children;94aSibling = aParents[idx];95bSibling = bParents[idx];9697if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) {98if (sharedParent === nodeB) {99return POSITION.FOLLOWING | POSITION.CONTAINED_BY;100}101return POSITION.FOLLOWING;102} else {103if (sharedParent === nodeA) {104return POSITION.PRECEDING | POSITION.CONTAINS;105}106return POSITION.PRECEDING;107}108};109110// Sort an array of nodes based on their relative position in the document and111// remove any duplicate nodes. If the array contains nodes that do not belong112// to the same document, sort order is unspecified.113//114// @argument {Array} nodes Array of DOM nodes115//116// @returns {Array} collection of unique nodes, sorted in document order117exports.uniqueSort = function(nodes) {118var idx = nodes.length, node, position;119120nodes = nodes.slice();121122while (--idx > -1) {123node = nodes[idx];124position = nodes.indexOf(node);125if (position > -1 && position < idx) {126nodes.splice(idx, 1);127}128}129nodes.sort(function(a, b) {130var relative = comparePos(a, b);131if (relative & POSITION.PRECEDING) {132return -1;133} else if (relative & POSITION.FOLLOWING) {134return 1;135}136return 0;137});138139return nodes;140};141142143