Last active
July 5, 2021 07:45
-
-
Save TinoDidriksen/c41c33ca5809ff297bf7b1608b3a41e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upper-case because we compare them to DOM nodeName | |
let text_nodes = {'ADDRESS': true, 'ARTICLE': true, 'ASIDE': true, 'AUDIO': true, 'BLOCKQUOTE': true, 'BODY': true, 'CANVAS': true, 'DD': true, 'DIV': true, 'DL': true, 'FIELDSET': true, 'FIGCAPTION': true, 'FIGURE': true, 'FOOTER': true, 'FORM': true, 'H1': true, 'H2': true, 'H3': true, 'H4': true, 'H5': true, 'H6': true, 'HEADER': true, 'HGROUP': true, 'HTML': true, 'HR': true, 'LI': true, 'MAIN': true, 'NAV': true, 'NOSCRIPT': true, 'OL': true, 'OUTPUT': true, 'P': true, 'PRE': true, 'SECTION': true, 'TABLE': true, 'TD': true, 'TH': true, 'UL': true, 'VIDEO': true}; | |
function findTextNodes(nodes, filter) { | |
let tns = [], wsx = /\S/; | |
if (!$.isArray(nodes)) { | |
nodes = [nodes]; | |
} | |
function _findTextNodes(node) { | |
if (node.nodeType == 3) { | |
if (wsx.test(node.nodeValue)) { | |
tns.push(node); | |
} | |
} | |
else { | |
for (let i=0 ; i < node.childNodes.length ; ++i) { | |
if (typeof filter !== 'function' || filter(node.childNodes[i])) { | |
_findTextNodes(node.childNodes[i]); | |
} | |
} | |
} | |
} | |
for (let i=0 ; i<nodes.length ; ++i) { | |
if (typeof filter !== 'function' || filter(nodes[i])) { | |
_findTextNodes(nodes[i]); | |
} | |
} | |
return tns; | |
} | |
///////////////// | |
let tns = findTextNodes(dom.body); | |
let ns = []; | |
for (let i=0 ; i<tns.length ; ++i) { | |
let n = tns[i]; | |
do { | |
n = n.parentNode; | |
} while(n && n.parentNode && !text_nodes.hasOwnProperty(n.nodeName)); | |
// Only add unseen parent nodes | |
if (ns.indexOf(n) === -1) { | |
ns.push(n); | |
} | |
} | |
// Deduplicate found parent nodes, and mark the unique ones for tracking | |
let elms = []; | |
for (let i=0 ; i<ns.length ; ++i) { | |
let p = ns[i]; | |
do { | |
p = p.parentNode; | |
if (ns.indexOf(p) !== -1) { | |
console.log(['Skipping node with a parent already in the set', ns[i]]); | |
ns[i] = null; | |
break; | |
} | |
} while (p && p.parentNode); | |
if (ns[i]) { | |
ns[i].setAttribute('data-gs-id', elms.length+1); | |
elms.push(ns[i]); | |
} | |
} | |
// elms now contains unique block nodes that we want to send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment