domutils
Advanced tools
Comparing version 1.7.0 to 2.0.0
@@ -1,141 +0,120 @@ | ||
// removeSubsets | ||
// Given an array of nodes, remove any member that is contained by another. | ||
exports.removeSubsets = function(nodes) { | ||
var idx = nodes.length, node, ancestor, replace; | ||
// Check if each node (or one of its ancestors) is already contained in the | ||
// array. | ||
while (--idx > -1) { | ||
node = ancestor = nodes[idx]; | ||
// Temporarily remove the node under consideration | ||
nodes[idx] = null; | ||
replace = true; | ||
while (ancestor) { | ||
if (nodes.indexOf(ancestor) > -1) { | ||
replace = false; | ||
nodes.splice(idx, 1); | ||
break; | ||
} | ||
ancestor = ancestor.parent; | ||
} | ||
// If the node has been found to be unique, re-insert it. | ||
if (replace) { | ||
nodes[idx] = node; | ||
} | ||
} | ||
return nodes; | ||
}; | ||
// Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition | ||
var POSITION = { | ||
DISCONNECTED: 1, | ||
PRECEDING: 2, | ||
FOLLOWING: 4, | ||
CONTAINS: 8, | ||
CONTAINED_BY: 16 | ||
}; | ||
// Compare the position of one node against another node in any other document. | ||
// The return value is a bitmask with the following values: | ||
// | ||
// document order: | ||
// > There is an ordering, document order, defined on all the nodes in the | ||
// > document corresponding to the order in which the first character of the | ||
// > XML representation of each node occurs in the XML representation of the | ||
// > document after expansion of general entities. Thus, the document element | ||
// > node will be the first node. Element nodes occur before their children. | ||
// > Thus, document order orders element nodes in order of the occurrence of | ||
// > their start-tag in the XML (after expansion of entities). The attribute | ||
// > nodes of an element occur after the element and before its children. The | ||
// > relative order of attribute nodes is implementation-dependent./ | ||
// Source: | ||
// http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order | ||
// | ||
// @argument {Node} nodaA The first node to use in the comparison | ||
// @argument {Node} nodeB The second node to use in the comparison | ||
// | ||
// @return {Number} A bitmask describing the input nodes' relative position. | ||
// See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for | ||
// a description of these values. | ||
var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) { | ||
var aParents = []; | ||
var bParents = []; | ||
var current, sharedParent, siblings, aSibling, bSibling, idx; | ||
if (nodeA === nodeB) { | ||
return 0; | ||
} | ||
current = nodeA; | ||
while (current) { | ||
aParents.unshift(current); | ||
current = current.parent; | ||
} | ||
current = nodeB; | ||
while (current) { | ||
bParents.unshift(current); | ||
current = current.parent; | ||
} | ||
idx = 0; | ||
while (aParents[idx] === bParents[idx]) { | ||
idx++; | ||
} | ||
if (idx === 0) { | ||
return POSITION.DISCONNECTED; | ||
} | ||
sharedParent = aParents[idx - 1]; | ||
siblings = sharedParent.children; | ||
aSibling = aParents[idx]; | ||
bSibling = bParents[idx]; | ||
if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) { | ||
if (sharedParent === nodeB) { | ||
return POSITION.FOLLOWING | POSITION.CONTAINED_BY; | ||
} | ||
return POSITION.FOLLOWING; | ||
} else { | ||
if (sharedParent === nodeA) { | ||
return POSITION.PRECEDING | POSITION.CONTAINS; | ||
} | ||
return POSITION.PRECEDING; | ||
} | ||
}; | ||
// Sort an array of nodes based on their relative position in the document and | ||
// remove any duplicate nodes. If the array contains nodes that do not belong | ||
// to the same document, sort order is unspecified. | ||
// | ||
// @argument {Array} nodes Array of DOM nodes | ||
// | ||
// @returns {Array} collection of unique nodes, sorted in document order | ||
exports.uniqueSort = function(nodes) { | ||
var idx = nodes.length, node, position; | ||
nodes = nodes.slice(); | ||
while (--idx > -1) { | ||
node = nodes[idx]; | ||
position = nodes.indexOf(node); | ||
if (position > -1 && position < idx) { | ||
nodes.splice(idx, 1); | ||
} | ||
} | ||
nodes.sort(function(a, b) { | ||
var relative = comparePos(a, b); | ||
if (relative & POSITION.PRECEDING) { | ||
return -1; | ||
} else if (relative & POSITION.FOLLOWING) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
return nodes; | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tagtypes_1 = require("./tagtypes"); | ||
/** | ||
* Given an array of nodes, remove any member that is contained by another. | ||
* | ||
* @param nodes — Nodes to filter. | ||
*/ | ||
function removeSubsets(nodes) { | ||
var idx = nodes.length; | ||
// Check if each node (or one of its ancestors) is already contained in the | ||
// array. | ||
while (--idx >= 0) { | ||
var node = nodes[idx]; | ||
// Remove the node if it is not unique. | ||
// We are going through the array from the end, so we only | ||
// have to check nodes that preceed the node under consideration in the array. | ||
if (idx > 0 && nodes.lastIndexOf(node, idx - 1) >= 0) { | ||
nodes.splice(idx, 1); | ||
continue; | ||
} | ||
for (var ancestor = node.parent; ancestor; ancestor = ancestor.parent) { | ||
if (nodes.indexOf(ancestor) > -1) { | ||
nodes.splice(idx, 1); | ||
break; | ||
} | ||
} | ||
} | ||
return nodes; | ||
} | ||
exports.removeSubsets = removeSubsets; | ||
/*** | ||
* Compare the position of one node against another node in any other document. | ||
* The return value is a bitmask with the following values: | ||
* | ||
* document order: | ||
* > There is an ordering, document order, defined on all the nodes in the | ||
* > document corresponding to the order in which the first character of the | ||
* > XML representation of each node occurs in the XML representation of the | ||
* > document after expansion of general entities. Thus, the document element | ||
* > node will be the first node. Element nodes occur before their children. | ||
* > Thus, document order orders element nodes in order of the occurrence of | ||
* > their start-tag in the XML (after expansion of entities). The attribute | ||
* > nodes of an element occur after the element and before its children. The | ||
* > relative order of attribute nodes is implementation-dependent./ | ||
* | ||
* Source: | ||
* http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order | ||
* @argument nodaA The first node to use in the comparison | ||
* @argument nodeB The second node to use in the comparison | ||
* | ||
* @return A bitmask describing the input nodes' relative position. | ||
* | ||
* See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for | ||
* a description of these values. | ||
*/ | ||
function compareDocumentPosition(nodeA, nodeB) { | ||
var aParents = []; | ||
var bParents = []; | ||
if (nodeA === nodeB) { | ||
return 0; | ||
} | ||
var current = tagtypes_1.hasChildren(nodeA) ? nodeA : nodeA.parent; | ||
while (current) { | ||
aParents.unshift(current); | ||
current = current.parent; | ||
} | ||
current = tagtypes_1.hasChildren(nodeB) ? nodeB : nodeB.parent; | ||
while (current) { | ||
bParents.unshift(current); | ||
current = current.parent; | ||
} | ||
var idx = 0; | ||
while (aParents[idx] === bParents[idx]) { | ||
idx++; | ||
} | ||
if (idx === 0) { | ||
return 1 /* DISCONNECTED */; | ||
} | ||
var sharedParent = aParents[idx - 1]; | ||
var siblings = sharedParent.children; | ||
var aSibling = aParents[idx]; | ||
var bSibling = bParents[idx]; | ||
if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) { | ||
if (sharedParent === nodeB) { | ||
return 4 /* FOLLOWING */ | 16 /* CONTAINED_BY */; | ||
} | ||
return 4 /* FOLLOWING */; | ||
} | ||
else { | ||
if (sharedParent === nodeA) { | ||
return 2 /* PRECEDING */ | 8 /* CONTAINS */; | ||
} | ||
return 2 /* PRECEDING */; | ||
} | ||
} | ||
exports.compareDocumentPosition = compareDocumentPosition; | ||
/*** | ||
* Sort an array of nodes based on their relative position in the document and | ||
* remove any duplicate nodes. If the array contains nodes that do not belong | ||
* to the same document, sort order is unspecified. | ||
* | ||
* @argument nodes Array of DOM nodes | ||
* @returns collection of unique nodes, sorted in document order | ||
*/ | ||
function uniqueSort(nodes) { | ||
nodes = nodes.filter(function (node, i, arr) { return !arr.includes(node, i + 1); }); | ||
nodes.sort(function (a, b) { | ||
var relative = compareDocumentPosition(a, b); | ||
if (relative & 2 /* PRECEDING */) { | ||
return -1; | ||
} | ||
else if (relative & 4 /* FOLLOWING */) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
return nodes; | ||
} | ||
exports.uniqueSort = uniqueSort; |
@@ -1,87 +0,86 @@ | ||
var ElementType = require("domelementtype"); | ||
var isTag = exports.isTag = ElementType.isTag; | ||
exports.testElement = function(options, element){ | ||
for(var key in options){ | ||
if(!options.hasOwnProperty(key)); | ||
else if(key === "tag_name"){ | ||
if(!isTag(element) || !options.tag_name(element.name)){ | ||
return false; | ||
} | ||
} else if(key === "tag_type"){ | ||
if(!options.tag_type(element.type)) return false; | ||
} else if(key === "tag_contains"){ | ||
if(isTag(element) || !options.tag_contains(element.data)){ | ||
return false; | ||
} | ||
} else if(!element.attribs || !options[key](element.attribs[key])){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var querying_1 = require("./querying"); | ||
var tagtypes_1 = require("./tagtypes"); | ||
function isTextNode(node) { | ||
return node.type === "text" /* Text */; | ||
} | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
var Checks = { | ||
tag_name: function(name){ | ||
if(typeof name === "function"){ | ||
return function(elem){ return isTag(elem) && name(elem.name); }; | ||
} else if(name === "*"){ | ||
return isTag; | ||
} else { | ||
return function(elem){ return isTag(elem) && elem.name === name; }; | ||
} | ||
}, | ||
tag_type: function(type){ | ||
if(typeof type === "function"){ | ||
return function(elem){ return type(elem.type); }; | ||
} else { | ||
return function(elem){ return elem.type === type; }; | ||
} | ||
}, | ||
tag_contains: function(data){ | ||
if(typeof data === "function"){ | ||
return function(elem){ return !isTag(elem) && data(elem.data); }; | ||
} else { | ||
return function(elem){ return !isTag(elem) && elem.data === data; }; | ||
} | ||
} | ||
tag_name: function (name) { | ||
if (typeof name === "function") { | ||
return function (elem) { return tagtypes_1.isTag(elem) && name(elem.name); }; | ||
} | ||
else if (name === "*") { | ||
return tagtypes_1.isTag; | ||
} | ||
else { | ||
return function (elem) { return tagtypes_1.isTag(elem) && elem.name === name; }; | ||
} | ||
}, | ||
tag_type: function (type) { | ||
if (typeof type === "function") { | ||
return function (elem) { return type(elem.type); }; | ||
} | ||
else { | ||
return function (elem) { return elem.type === type; }; | ||
} | ||
}, | ||
tag_contains: function (data) { | ||
if (typeof data === "function") { | ||
return function (elem) { return isTextNode(elem) && data(elem.data); }; | ||
} | ||
else { | ||
return function (elem) { return isTextNode(elem) && elem.data === data; }; | ||
} | ||
} | ||
}; | ||
function getAttribCheck(attrib, value){ | ||
if(typeof value === "function"){ | ||
return function(elem){ return elem.attribs && value(elem.attribs[attrib]); }; | ||
} else { | ||
return function(elem){ return elem.attribs && elem.attribs[attrib] === value; }; | ||
} | ||
/* eslint-enable @typescript-eslint/camelcase */ | ||
function getAttribCheck(attrib, value) { | ||
if (typeof value === "function") { | ||
return function (elem) { return tagtypes_1.isTag(elem) && value(elem.attribs[attrib]); }; | ||
} | ||
else { | ||
return function (elem) { return tagtypes_1.isTag(elem) && elem.attribs[attrib] === value; }; | ||
} | ||
} | ||
function combineFuncs(a, b){ | ||
return function(elem){ | ||
return a(elem) || b(elem); | ||
}; | ||
function combineFuncs(a, b) { | ||
return function (elem) { return a(elem) || b(elem); }; | ||
} | ||
exports.getElements = function(options, element, recurse, limit){ | ||
var funcs = Object.keys(options).map(function(key){ | ||
var value = options[key]; | ||
return key in Checks ? Checks[key](value) : getAttribCheck(key, value); | ||
}); | ||
return funcs.length === 0 ? [] : this.filter( | ||
funcs.reduce(combineFuncs), | ||
element, recurse, limit | ||
); | ||
}; | ||
exports.getElementById = function(id, element, recurse){ | ||
if(!Array.isArray(element)) element = [element]; | ||
return this.findOne(getAttribCheck("id", id), element, recurse !== false); | ||
}; | ||
exports.getElementsByTagName = function(name, element, recurse, limit){ | ||
return this.filter(Checks.tag_name(name), element, recurse, limit); | ||
}; | ||
exports.getElementsByTagType = function(type, element, recurse, limit){ | ||
return this.filter(Checks.tag_type(type), element, recurse, limit); | ||
}; | ||
function compileTest(options) { | ||
var funcs = Object.keys(options).map(function (key) { | ||
var value = options[key]; | ||
// @ts-ignore | ||
return key in Checks ? Checks[key](value) : getAttribCheck(key, value); | ||
}); | ||
return funcs.length === 0 ? null : funcs.reduce(combineFuncs); | ||
} | ||
function testElement(options, element) { | ||
var test = compileTest(options); | ||
return test ? test(element) : true; | ||
} | ||
exports.testElement = testElement; | ||
function getElements(options, element, recurse, limit) { | ||
if (limit === void 0) { limit = Infinity; } | ||
var test = compileTest(options); | ||
return test ? querying_1.filter(test, element, recurse, limit) : []; | ||
} | ||
exports.getElements = getElements; | ||
function getElementById(id, element, recurse) { | ||
if (recurse === void 0) { recurse = true; } | ||
if (!Array.isArray(element)) | ||
element = [element]; | ||
return querying_1.findOne(getAttribCheck("id", id), element, recurse); | ||
} | ||
exports.getElementById = getElementById; | ||
function getElementsByTagName(name, element, recurse, limit) { | ||
if (limit === void 0) { limit = Infinity; } | ||
return querying_1.filter(Checks.tag_name(name), element, recurse, limit); | ||
} | ||
exports.getElementsByTagName = getElementsByTagName; | ||
function getElementsByTagType(type, element, recurse, limit) { | ||
if (recurse === void 0) { recurse = true; } | ||
if (limit === void 0) { limit = Infinity; } | ||
return querying_1.filter(Checks.tag_type(type), element, recurse, limit); | ||
} | ||
exports.getElementsByTagType = getElementsByTagType; |
@@ -1,77 +0,101 @@ | ||
exports.removeElement = function(elem){ | ||
if(elem.prev) elem.prev.next = elem.next; | ||
if(elem.next) elem.next.prev = elem.prev; | ||
if(elem.parent){ | ||
var childs = elem.parent.children; | ||
childs.splice(childs.lastIndexOf(elem), 1); | ||
} | ||
}; | ||
exports.replaceElement = function(elem, replacement){ | ||
var prev = replacement.prev = elem.prev; | ||
if(prev){ | ||
prev.next = replacement; | ||
} | ||
var next = replacement.next = elem.next; | ||
if(next){ | ||
next.prev = replacement; | ||
} | ||
var parent = replacement.parent = elem.parent; | ||
if(parent){ | ||
var childs = parent.children; | ||
childs[childs.lastIndexOf(elem)] = replacement; | ||
} | ||
}; | ||
exports.appendChild = function(elem, child){ | ||
child.parent = elem; | ||
if(elem.children.push(child) !== 1){ | ||
var sibling = elem.children[elem.children.length - 2]; | ||
sibling.next = child; | ||
child.prev = sibling; | ||
child.next = null; | ||
} | ||
}; | ||
exports.append = function(elem, next){ | ||
var parent = elem.parent, | ||
currNext = elem.next; | ||
next.next = currNext; | ||
next.prev = elem; | ||
elem.next = next; | ||
next.parent = parent; | ||
if(currNext){ | ||
currNext.prev = next; | ||
if(parent){ | ||
var childs = parent.children; | ||
childs.splice(childs.lastIndexOf(currNext), 0, next); | ||
} | ||
} else if(parent){ | ||
parent.children.push(next); | ||
} | ||
}; | ||
exports.prepend = function(elem, prev){ | ||
var parent = elem.parent; | ||
if(parent){ | ||
var childs = parent.children; | ||
childs.splice(childs.lastIndexOf(elem), 0, prev); | ||
} | ||
if(elem.prev){ | ||
elem.prev.next = prev; | ||
} | ||
prev.parent = parent; | ||
prev.prev = elem.prev; | ||
prev.next = elem; | ||
elem.prev = prev; | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/*** | ||
* Remove an element from the dom | ||
* | ||
* @argument elem The element to be removed | ||
*/ | ||
function removeElement(elem) { | ||
if (elem.prev) | ||
elem.prev.next = elem.next; | ||
if (elem.next) | ||
elem.next.prev = elem.prev; | ||
if (elem.parent) { | ||
var childs = elem.parent.children; | ||
childs.splice(childs.lastIndexOf(elem), 1); | ||
} | ||
} | ||
exports.removeElement = removeElement; | ||
/*** | ||
* Replace an element in the dom | ||
* | ||
* @argument elem The element to be replaced | ||
* @argument replacement The element to be added | ||
*/ | ||
function replaceElement(elem, replacement) { | ||
var prev = (replacement.prev = elem.prev); | ||
if (prev) { | ||
prev.next = replacement; | ||
} | ||
var next = (replacement.next = elem.next); | ||
if (next) { | ||
next.prev = replacement; | ||
} | ||
var parent = (replacement.parent = elem.parent); | ||
if (parent) { | ||
var childs = parent.children; | ||
childs[childs.lastIndexOf(elem)] = replacement; | ||
} | ||
} | ||
exports.replaceElement = replaceElement; | ||
/*** | ||
* Append a child to an element | ||
* | ||
* @argument elem The element to append to | ||
* @argument child The element to be added as a child | ||
*/ | ||
function appendChild(elem, child) { | ||
child.parent = elem; | ||
if (elem.children.push(child) !== 1) { | ||
var sibling = elem.children[elem.children.length - 2]; | ||
sibling.next = child; | ||
child.prev = sibling; | ||
child.next = null; | ||
} | ||
} | ||
exports.appendChild = appendChild; | ||
/*** | ||
* Append an element after another | ||
* | ||
* @argument elem The element to append to | ||
* @argument next The element be added | ||
*/ | ||
function append(elem, next) { | ||
var parent = elem.parent, currNext = elem.next; | ||
next.next = currNext; | ||
next.prev = elem; | ||
elem.next = next; | ||
next.parent = parent; | ||
if (currNext) { | ||
currNext.prev = next; | ||
if (parent) { | ||
var childs = parent.children; | ||
childs.splice(childs.lastIndexOf(currNext), 0, next); | ||
} | ||
} | ||
else if (parent) { | ||
parent.children.push(next); | ||
} | ||
} | ||
exports.append = append; | ||
/*** | ||
* Prepend an element before another | ||
* | ||
* @argument elem The element to append to | ||
* @argument prev The element be added | ||
*/ | ||
function prepend(elem, prev) { | ||
var parent = elem.parent; | ||
if (parent) { | ||
var childs = parent.children; | ||
childs.splice(childs.lastIndexOf(elem), 0, prev); | ||
} | ||
if (elem.prev) { | ||
elem.prev.next = prev; | ||
} | ||
prev.parent = parent; | ||
prev.prev = elem.prev; | ||
prev.next = elem; | ||
elem.prev = prev; | ||
} | ||
exports.prepend = prepend; |
@@ -1,95 +0,86 @@ | ||
var isTag = require("domelementtype").isTag; | ||
module.exports = { | ||
filter: filter, | ||
find: find, | ||
findOneChild: findOneChild, | ||
findOne: findOne, | ||
existsOne: existsOne, | ||
findAll: findAll | ||
}; | ||
function filter(test, element, recurse, limit){ | ||
if(!Array.isArray(element)) element = [element]; | ||
if(typeof limit !== "number" || !isFinite(limit)){ | ||
limit = Infinity; | ||
} | ||
return find(test, element, recurse !== false, limit); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tagtypes_1 = require("./tagtypes"); | ||
function filter(test, element, recurse, limit) { | ||
if (recurse === void 0) { recurse = true; } | ||
if (limit === void 0) { limit = Infinity; } | ||
if (!Array.isArray(element)) | ||
element = [element]; | ||
return find(test, element, recurse, limit); | ||
} | ||
function find(test, elems, recurse, limit){ | ||
var result = [], childs; | ||
for(var i = 0, j = elems.length; i < j; i++){ | ||
if(test(elems[i])){ | ||
result.push(elems[i]); | ||
if(--limit <= 0) break; | ||
} | ||
childs = elems[i].children; | ||
if(recurse && childs && childs.length > 0){ | ||
childs = find(test, childs, recurse, limit); | ||
result = result.concat(childs); | ||
limit -= childs.length; | ||
if(limit <= 0) break; | ||
} | ||
} | ||
return result; | ||
exports.filter = filter; | ||
function find(test, elems, recurse, limit) { | ||
var result = []; | ||
for (var i = 0; i < elems.length; i++) { | ||
var elem = elems[i]; | ||
if (test(elem)) { | ||
result.push(elem); | ||
if (--limit <= 0) | ||
break; | ||
} | ||
if (recurse && tagtypes_1.hasChildren(elem) && elem.children.length > 0) { | ||
var children = find(test, elem.children, recurse, limit); | ||
result = result.concat(children); | ||
limit -= children.length; | ||
if (limit <= 0) | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
function findOneChild(test, elems){ | ||
for(var i = 0, l = elems.length; i < l; i++){ | ||
if(test(elems[i])) return elems[i]; | ||
} | ||
return null; | ||
exports.find = find; | ||
function findOneChild(test, elems) { | ||
for (var i = 0; i < elems.length; i++) { | ||
if (test(elems[i])) | ||
return elems[i]; | ||
} | ||
return null; | ||
} | ||
function findOne(test, elems){ | ||
var elem = null; | ||
for(var i = 0, l = elems.length; i < l && !elem; i++){ | ||
if(!isTag(elems[i])){ | ||
continue; | ||
} else if(test(elems[i])){ | ||
elem = elems[i]; | ||
} else if(elems[i].children.length > 0){ | ||
elem = findOne(test, elems[i].children); | ||
} | ||
} | ||
return elem; | ||
exports.findOneChild = findOneChild; | ||
function findOne(test, elems, recurse) { | ||
if (recurse === void 0) { recurse = true; } | ||
var elem = null; | ||
for (var i = 0; i < elems.length && !elem; i++) { | ||
var checked = elems[i]; | ||
if (!tagtypes_1.isTag(checked)) { | ||
continue; | ||
} | ||
else if (test(checked)) { | ||
elem = checked; | ||
} | ||
else if (recurse && checked.children.length > 0) { | ||
elem = findOne(test, checked.children); | ||
} | ||
} | ||
return elem; | ||
} | ||
function existsOne(test, elems){ | ||
for(var i = 0, l = elems.length; i < l; i++){ | ||
if( | ||
isTag(elems[i]) && ( | ||
test(elems[i]) || ( | ||
elems[i].children.length > 0 && | ||
existsOne(test, elems[i].children) | ||
) | ||
) | ||
){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
exports.findOne = findOne; | ||
function existsOne(test, elems) { | ||
for (var i = 0; i < elems.length; i++) { | ||
var checked = elems[i]; | ||
if (tagtypes_1.isTag(checked) && | ||
(test(checked) || | ||
(checked.children.length > 0 && | ||
existsOne(test, checked.children)))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function findAll(test, rootElems){ | ||
var result = []; | ||
var stack = rootElems.slice(); | ||
while(stack.length){ | ||
var elem = stack.shift(); | ||
if(!isTag(elem)) continue; | ||
if (elem.children && elem.children.length > 0) { | ||
stack.unshift.apply(stack, elem.children); | ||
} | ||
if(test(elem)) result.push(elem); | ||
} | ||
return result; | ||
exports.existsOne = existsOne; | ||
function findAll(test, rootElems) { | ||
var result = []; | ||
var stack = rootElems.slice(); | ||
while (stack.length) { | ||
var elem = stack.shift(); | ||
if (!elem || !tagtypes_1.isTag(elem)) | ||
continue; | ||
if (elem.children && elem.children.length > 0) { | ||
stack.unshift.apply(stack, elem.children); | ||
} | ||
if (test(elem)) | ||
result.push(elem); | ||
} | ||
return result; | ||
} | ||
exports.findAll = findAll; |
@@ -1,23 +0,26 @@ | ||
var ElementType = require("domelementtype"), | ||
getOuterHTML = require("dom-serializer"), | ||
isTag = ElementType.isTag; | ||
module.exports = { | ||
getInnerHTML: getInnerHTML, | ||
getOuterHTML: getOuterHTML, | ||
getText: getText | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
function getInnerHTML(elem, opts){ | ||
return elem.children ? elem.children.map(function(elem){ | ||
return getOuterHTML(elem, opts); | ||
}).join("") : ""; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tagtypes_1 = require("./tagtypes"); | ||
var dom_serializer_1 = __importDefault(require("dom-serializer")); | ||
exports.getOuterHTML = dom_serializer_1.default; | ||
function getInnerHTML(node, options) { | ||
return tagtypes_1.hasChildren(node) | ||
? node.children.map(function (node) { return exports.getOuterHTML(node, options); }).join("") | ||
: ""; | ||
} | ||
function getText(elem){ | ||
if(Array.isArray(elem)) return elem.map(getText).join(""); | ||
if(isTag(elem)) return elem.name === "br" ? "\n" : getText(elem.children); | ||
if(elem.type === ElementType.CDATA) return getText(elem.children); | ||
if(elem.type === ElementType.Text) return elem.data; | ||
return ""; | ||
exports.getInnerHTML = getInnerHTML; | ||
function getText(node) { | ||
if (Array.isArray(node)) | ||
return node.map(getText).join(""); | ||
if (tagtypes_1.isTag(node)) | ||
return node.name === "br" ? "\n" : getText(node.children); | ||
if (tagtypes_1.isCDATA(node)) | ||
return getText(node.children); | ||
if (tagtypes_1.isText(node)) | ||
return node.data; | ||
return ""; | ||
} | ||
exports.getText = getText; |
@@ -1,24 +0,33 @@ | ||
var getChildren = exports.getChildren = function(elem){ | ||
return elem.children; | ||
}; | ||
var getParent = exports.getParent = function(elem){ | ||
return elem.parent; | ||
}; | ||
exports.getSiblings = function(elem){ | ||
var parent = getParent(elem); | ||
return parent ? getChildren(parent) : [elem]; | ||
}; | ||
exports.getAttributeValue = function(elem, name){ | ||
return elem.attribs && elem.attribs[name]; | ||
}; | ||
exports.hasAttrib = function(elem, name){ | ||
return !!elem.attribs && hasOwnProperty.call(elem.attribs, name); | ||
}; | ||
exports.getName = function(elem){ | ||
return elem.name; | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function getChildren(elem) { | ||
// @ts-ignore | ||
return elem.children || null; | ||
} | ||
exports.getChildren = getChildren; | ||
function getParent(elem) { | ||
return elem.parent || null; | ||
} | ||
exports.getParent = getParent; | ||
function getSiblings(elem) { | ||
var parent = getParent(elem); | ||
return parent ? getChildren(parent) : [elem]; | ||
} | ||
exports.getSiblings = getSiblings; | ||
function getAttributeValue(elem, name) { | ||
return elem.attribs && elem.attribs[name]; | ||
} | ||
exports.getAttributeValue = getAttributeValue; | ||
function hasAttrib(elem, name) { | ||
return !!getAttributeValue(elem, name); | ||
} | ||
exports.hasAttrib = hasAttrib; | ||
/*** | ||
* Returns the name property of an element | ||
* | ||
* @argument elem The element to get the name for | ||
*/ | ||
function getName(elem) { | ||
return elem.name; | ||
} | ||
exports.getName = getName; |
{ | ||
"name": "domutils", | ||
"version": "1.7.0", | ||
"description": "utilities for working with htmlparser2's dom", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "tests" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/tests/**.js && jshint index.js test/**/*.js lib/*.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/FB55/domutils.git" | ||
}, | ||
"keywords": [ | ||
"dom", | ||
"htmlparser2" | ||
], | ||
"dependencies": { | ||
"dom-serializer": "0", | ||
"domelementtype": "1" | ||
}, | ||
"devDependencies": { | ||
"htmlparser2": "~3.9.2", | ||
"domhandler": "2", | ||
"jshint": "~2.9.4", | ||
"mocha": "~3.2.0" | ||
}, | ||
"author": "Felix Boehm <me@feedic.com>", | ||
"license": "BSD-2-Clause", | ||
"jshintConfig": { | ||
"proto": true, | ||
"unused": true, | ||
"eqnull": true, | ||
"undef": true, | ||
"quotmark": "double", | ||
"eqeqeq": true, | ||
"trailing": true, | ||
"node": true, | ||
"globals": { | ||
"describe": true, | ||
"it": true, | ||
"beforeEach": true | ||
"name": "domutils", | ||
"version": "2.0.0", | ||
"description": "Utilities for working with htmlparser2's dom", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"lib/**/*" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage && npm run lint", | ||
"coverage": "cat coverage/lcov.info | coveralls", | ||
"lint": "eslint src/**/*.ts", | ||
"format": "prettier --write '**/*.{ts,md,json}'", | ||
"build": "tsc", | ||
"prepare": "npm run build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/fb55/domutils.git" | ||
}, | ||
"keywords": [ | ||
"dom", | ||
"htmlparser2" | ||
], | ||
"dependencies": { | ||
"dom-serializer": "^0.2.1", | ||
"domelementtype": "^2.0.1", | ||
"domhandler": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.16", | ||
"@types/node": "^12.6.8", | ||
"@typescript-eslint/eslint-plugin": "^1.13.0", | ||
"@typescript-eslint/parser": "^1.13.0", | ||
"eslint": "^6.1.0", | ||
"eslint-config-prettier": "^6.0.0", | ||
"htmlparser2": "~3.10.0", | ||
"jest": "^24.8.0", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.3" | ||
}, | ||
"author": "Felix Boehm <me@feedic.com>", | ||
"license": "BSD-2-Clause", | ||
"jest": { | ||
"preset": "ts-jest", | ||
"testEnvironment": "node" | ||
}, | ||
"prettier": { | ||
"tabWidth": 4 | ||
} | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30479
27
622
15
3
11
1
+ Addeddomhandler@^3.0.0
+ Addeddomhandler@3.3.0(transitive)
- Removeddomelementtype@1.3.1(transitive)
Updateddom-serializer@^0.2.1
Updateddomelementtype@^2.0.1