Advertisement
NurUddinShohan

Untitled

Mar 25th, 2021
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Browser.DOM = function (html, scope) {
  2.    // Creates empty node and injects html string using .innerHTML
  3.    // in case the variable isn't a string we assume is already a node
  4.    var node;
  5.    if (html.constructor === String) {
  6.        var node = document.createElement('div');
  7.        node.innerHTML = html;
  8.    } else {
  9.        node = html;
  10.    }
  11.  
  12.    // Creates of uses and object to which we will create variables
  13.    // that will point to the created nodes
  14.  
  15.    var _scope = scope || {};
  16.  
  17.    // Recursive function that will read every node and when a node
  18.    // contains the var attribute add a reference in the scope object
  19.  
  20.    function toScope(node, scope) {
  21.        var children = node.children;
  22.        for (var iChild = 0; iChild < children.length; iChild++) {
  23.            if (children[iChild].getAttribute('var')) {
  24.                var names = children[iChild].getAttribute('var').split('.');
  25.                var obj = scope;
  26.                while (names.length > 0)
  27.                {
  28.                    var _property = names.shift();
  29.                    if (names.length == 0)
  30.                    {
  31.                        obj[_property] = children[iChild];
  32.                    }
  33.                    else
  34.                    {
  35.                        if (!obj.hasOwnProperty(_property)){
  36.                            obj[_property] = {};
  37.                        }
  38.                        obj = obj[_property];
  39.                    }
  40.                }
  41.            }
  42.            toScope(children[iChild], scope);
  43.        }
  44.    }
  45.  
  46.    toScope(node, _scope);
  47.  
  48.    if (html.constructor != String) {
  49.        return html;
  50.    }
  51.    // If the node in the highest hierarchy is one return it
  52.  
  53.    if (node.childNodes.length == 1) {
  54.     // if a scope to add node variables is not set
  55.     // attach the object we created into the highest hierarchy node
  56.    
  57.        // by adding the nodes property.
  58.        if (!scope) {
  59.            node.childNodes[0].nodes = _scope;
  60.        }
  61.        return node.childNodes[0];
  62.    }
  63.  
  64.    // if the node in highest hierarchy is more than one return a fragment
  65.    var fragment = document.createDocumentFragment();
  66.    var children = node.childNodes;
  67.    
  68.    // add notes into DocumentFragment
  69.    while (children.length > 0) {
  70.        if (fragment.append){
  71.            fragment.append(children[0]);
  72.        }else{
  73.           fragment.appendChild(children[0]);
  74.        }
  75.    }
  76.  
  77.    fragment.nodes = _scope;
  78.    return fragment;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement