yoastseo
Version:
Yoast client-side content analysis
96 lines (87 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _traverse = require("../traverse");
var _SourceCodeLocation = _interopRequireDefault(require("./SourceCodeLocation"));
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* @typedef {import(".").Text} Text
*/
/**
* A node in the tree.
*/
class Node {
/**
* Creates a new node.
*
* @param {string} name The node's name or tag.
* @param {Object} attributes This node's attributes.
* @param {(Node|Text)[]} childNodes This node's child nodes.
* @param {Object} sourceCodeLocationInfo This node's location in the source code, from parse5.
*/
constructor(name, attributes = {}, childNodes = [], sourceCodeLocationInfo = {}) {
/**
* This node's name or tag.
* @type {string}
*/
this.name = name;
/**
* This node's attributes.
* @type {Object}
*/
this.attributes = attributes;
/**
* This node's child nodes.
* @type {(Node|Text)[]}
*/
this.childNodes = childNodes;
// Don't add the source code location when unavailable.
if (!(0, _lodash.isEmpty)(sourceCodeLocationInfo)) {
/**
* The location of this node inside the HTML.
* @type {SourceCodeLocation}
*/
this.sourceCodeLocation = new _SourceCodeLocation.default(sourceCodeLocationInfo);
}
}
/**
* Finds all nodes in the tree that satisfies the given condition.
*
* @param {function} condition The condition that a node should satisfy to end up in the list.
* @param {boolean} recurseFoundNodes=false Whether to recurse into found nodes to see if the condition
* also applies to sub-nodes of the found node.
*
* @returns {(Node|Text)[]} The list of nodes that satisfy the condition.
*/
findAll(condition, recurseFoundNodes = false) {
return (0, _traverse.findAllInTree)(this, condition, recurseFoundNodes);
}
/**
* Retrieves the parent node for the current node.
* @param {Node} tree The full tree for this node.
* @returns {Node} The parent node.
*/
getParentNode(tree) {
return (0, _traverse.getParentNode)(tree, this);
}
/**
* Returns the inner text (text without any markup) from this node.
*
* @returns {string} The inner text from this node.
*/
innerText() {
return (0, _traverse.innerText)(this);
}
/**
* Retrieves the start offset for this node.
* @returns {number} The start offset.
*/
getStartOffset() {
return this.sourceCodeLocation?.startTag?.endOffset || this.sourceCodeLocation?.startOffset || 0;
}
}
var _default = exports.default = Node;
//# sourceMappingURL=Node.js.map