Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#544] Added uliStore data structure, getAllTextNodes() and locateSlur() functions #597

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions browser-extension/plugin/src/transform-general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};



Loading