Download pool/shard009/JavaScript/0056291_findClonedNode.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 731 Bytes
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard009/JavaScript/0056291_findClonedNode.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard009/JavaScript/0056291_findClonedNode.js
-
curl -L -o 0056291_findClonedNode.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard009/JavaScript/0056291_findClonedNode.js
731 Bytes
| //Given a node from a DOM tree find the node in the same position from an identical DOM tree. See diagram below for clarity. | |
| // A B | |
| // | |
| // O O | |
| // |\ |\ | |
| // O O O O | |
| // /|\ /|\ | |
| // O O O O O O | |
| // \ \ | |
| // x y | |
| //signature required. y = find(A, B, x) | |
| export const getPath = (root, x, memo = []) => { | |
| if (!x.isEqualNode(root)) { | |
| let index = [].indexOf.call(x.parentNode.children, x); | |
| if (index !== -1) { | |
| memo.push(index); | |
| return getPath(root, x.parentNode, memo); | |
| } | |
| } | |
| return memo.reverse(); | |
| }; | |
| const find = (A, B, x) => { | |
| const path = getPath(A, x); | |
| return path.reduce((memo, position) => memo.children[position], B); | |
| }; | |
| export default find; | |