All files / src/cms/utils node-script-replace.js

92.86% Statements 13/14
100% Branches 2/2
100% Functions 3/3
92.31% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  24x 2x     22x 22x 22x 20x       24x     24x     2x 2x 2x     2x  
export default function nodeScriptReplace(node) {
    if ( nodeScriptIs(node) === true ) {
            node.parentNode.replaceChild( nodeScriptClone(node) , node );
    }
    else {
            var i        = 0;
            var children = node.childNodes;
            while ( i < children.length ) {
                    nodeScriptReplace( children[i++] );
            }
    }
 
    return node;
}
function nodeScriptIs(node) {
    return node.tagName === 'SCRIPT';
}
function nodeScriptClone(node){
    var script  = document.createElement("script");
    script.text = node.innerHTML;
    for( var i = node.attributes.length-1; i >= 0; i-- ) {
            script.setAttribute( node.attributes[i].name, node.attributes[i].value );
    }
    return script;
}