// fixes <script> tags in inserted HTML (e.g. via innerHTML)
// TODO: Do we handle <script src=""> correctly?

sS js_nodeScriptReplace() {
  ret [[
    function nodeScriptReplace(node) {
      if (nodeScriptIs(node)) {
        node.parentNode.replaceChild(nodeScriptClone(node), node);
        console.log("nodeScriptReplace finished replacement");
      } else {
        var i = -1, children = node.childNodes;
        while (++i < children.length)
          nodeScriptReplace(children[i]);
      }
    
      return node;
    }
    
    function nodeScriptClone(node){
      var script  = document.createElement("script");
      if (node.src)
        console.log("nodeScriptReplace including " + node.src);
      else {
        console.log("nodeScriptReplace inline script");
        script.text = node.innerHTML;
      }

      var i = -1, attrs = node.attributes, attr;
      while ( ++i < attrs.length )
        script.setAttribute( (attr = attrs[i]).name, attr.value );
      return script;
    }
    
    function nodeScriptIs(node) {
      return node.tagName === 'SCRIPT';
    }
  ]];
}