UNPKG

js-draw

Version:

Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript.

24 lines (23 loc) 760 B
/** Returns the first node or element with `textContent` matching `expectedText`. */ const findNodeWithText = (expectedText, parent, options = {}) => { const { tag } = options; if (parent.textContent === expectedText) { const matchesTag = (() => { // No tag check necessary? if (!tag) return true; return parent instanceof Element && tag.toUpperCase() === parent.tagName; })(); if (matchesTag) { return parent; } } for (const child of parent.childNodes) { const results = findNodeWithText(expectedText, child, options); if (results) { return results; } } return null; }; export default findNodeWithText;