diff --git a/browser-extension/plugin/src/transform-general.js b/browser-extension/plugin/src/transform-general.js index da192173..07c724ba 100644 --- a/browser-extension/plugin/src/transform-general.js +++ b/browser-extension/plugin/src/transform-general.js @@ -72,6 +72,115 @@ const processNewlyAddedNodesGeneral = function (firstBody) { observer.observe(firstBody, config); }; + +// Code inserted below is for uliStore + + +/* getAllTextNodes() STARTS HERE */ + +function checkFalseTextNode(text, actualLengthOfText) { + let n = text.length; + let totalNewlineAndWhitespaces = 0; + for (let i = 0; i < n; i++) { + if (text[i] === "\n") { + totalNewlineAndWhitespaces += 1; + } + + else if (text[i] === " ") { + totalNewlineAndWhitespaces += 1; + } + + } + if (totalNewlineAndWhitespaces === actualLengthOfText) { + //False Text Node Confirmed + return true; + } + else { + //True Text Node Confirmed + return false; + } +} + + +function getAllTextNodes(node) { + let uliStore = [] + if (node.nodeType === 3) { + //If node.data contains just whitespaces and \n, then its a false text node + + // let whitespaces = (node.data.split(" ").length - 1); + // console.log(node.data) ; + if (checkFalseTextNode(node.data, node.length) === false) { + uliStore.push({ node: node, parent: node.parentNode }); + } + // textNodes.push({ node: node, parent: node.parentNode }); + return; + } + + let children = Array.from(node.childNodes); + for (let i = 0; i < children.length; i++) { + getAllTextNodes(children[i]); + } + + return uliStore ; +} + + +/* getAllTextNodes() ENDS HERE */ + + +/* locateSlur() STARTS HERE */ + +function findPositions(word, text) { + let positions = {}; + + let len = word.length + let loc = [] + let index = text.toString().indexOf(word); + while (index !== -1) { + let obj = {} ; + loc.push([index , index + len]); + index = text.toString().indexOf(word, index + 1); + } + + + if(loc.length !== 0){ + positions.slurText = word + positions.slurLocation = loc ; + } + return positions; +} + + +function locateSlur(uliStore, targetWords){ + let n = uliStore.length ; + + for(let i = 0 ; i < n ; i++){ + let store = uliStore[i] ; //This will contain the textNode + let parentNode = store.parent + let text = store.node.textContent + //We have to look into this store for all the slurWords + let slurs = [] ; + + targetWords.forEach(targetWord => { + let slurWord = targetWord ; + let pos = findPositions(slurWord , text) ; + if(Object.keys(pos).length !== 0){ + slurs.push(pos) + } + }) + uliStore[i].slurs = slurs ; + } + return uliStore ; //This will return the final uliStore (after appending slurs) +} + + +/* locateSlur() ENDS HERE */ + + + export default { processNewlyAddedNodesGeneral }; + + +