lib/jyt.parser.js
function convertDomToJyt(dom) {
    var i, elems, tempElem;

    // if just one element in the array, bump it up a level
    if (utils.isArray(dom) && dom.length === 1) {
        dom = dom[0];
    }
    // if multiple, then we create a sibling array of elems and return it
    else if (utils.isArray(dom)) {
        elems = [];
        for (i = 0; i < dom.length; i++) {
            tempElem = convertDomToJyt(dom[i]);
            if (tempElem !== null) {
                elems.push(tempElem);
            }
        }

        return runtime.naked(elems, null);
    }

    // if type is string, then just return it
    if (dom.type === 'text') {
        return dom.data;
    }
    // if no dom name, then just return empty string since it is likely a comment
    if (!dom.name) {
        return null;
    }

    // if we get here then we have just one element
    var elem = runtime.elem(dom.name);
    var childCount = dom.children && dom.children.length;

    // attributes should be the same
    elem.attributes = dom.attribs;

    // recursively add children
    if (dom.children && dom.children.length) {
        elem.children = [];
        for (i = 0; i < childCount; i++) {
            tempElem = convertDomToJyt(dom.children[i]);
            if (tempElem !== null) {
                elem.children.push(tempElem);
            }
        }

        // if one child that is a string, bring it up a level
        if (elem.children.length === 1 && utils.isString(elem.children[0])) {
            elem.text = elem.children[0];
            delete elem.children;
        }
        // if no children, remove it
        else if (elem.children.length === 0) {
            delete elem.children;
        }
    }

    return elem;
}
function convertDomToJyt(dom) {
    var i, elems, tempElem;

    // if just one element in the array, bump it up a level
    if (utils.isArray(dom) && dom.length === 1) {
        dom = dom[0];
    }
    // if multiple, then we create a sibling array of elems and return it
    else if (utils.isArray(dom)) {
        elems = [];
        for (i = 0; i < dom.length; i++) {
            tempElem = convertDomToJyt(dom[i]);
            if (tempElem !== null) {
                elems.push(tempElem);
            }
        }

        return runtime.naked(elems, null);
    }

    // if type is string, then just return it
    if (dom.type === 'text') {
        return dom.data;
    }
    // if no dom name, then just return empty string since it is likely a comment
    if (!dom.name) {
        return null;
    }

    // if we get here then we have just one element
    var elem = runtime.elem(dom.name);
    var childCount = dom.children && dom.children.length;

    // attributes should be the same
    elem.attributes = dom.attribs;

    // recursively add children
    if (dom.children && dom.children.length) {
        elem.children = [];
        for (i = 0; i < childCount; i++) {
            tempElem = convertDomToJyt(dom.children[i]);
            if (tempElem !== null) {
                elem.children.push(tempElem);
            }
        }

        // if one child that is a string, bring it up a level
        if (elem.children.length === 1 && utils.isString(elem.children[0])) {
            elem.text = elem.children[0];
            delete elem.children;
        }
        // if no children, remove it
        else if (elem.children.length === 0) {
            delete elem.children;
        }
    }

    return elem;
}
function convertDomToJyt ( dom ) { var i , elems , tempElem ; // if just one element in the array, bump it up a level if ( utils . isArray ( dom ) && dom . length === 1 ) { dom = dom [ 0 ] ; } // if multiple, then we create a sibling array of elems and return it else if ( utils . isArray ( dom ) ) { elems = [ ] ; for ( i = 0 ; i < dom . length ; i ++ ) { tempElem = convertDomToJyt ( dom [ i ] ) ; if ( tempElem !== null ) { elems . push ( tempElem ) ; } } return runtime . naked ( elems , null ) ; } // if type is string, then just return it if ( dom . type === 'text' ) { return dom . data ; } // if no dom name, then just return empty string since it is likely a comment if ( ! dom . name ) { return null ; } // if we get here then we have just one element var elem = runtime . elem ( dom . name ) ; var childCount = dom . children && dom . children . length ; // attributes should be the same elem . attributes = dom . attribs ; // recursively add children if ( dom . children && dom . children . length ) { elem . children = [ ] ; for ( i = 0 ; i < childCount ; i ++ ) { tempElem = convertDomToJyt ( dom . children [ i ] ) ; if ( tempElem !== null ) { elem . children . push ( tempElem ) ; } } // if one child that is a string, bring it up a level if ( elem . children . length === 1 && utils . isString ( elem . children [ 0 ] ) ) { elem . text = elem . children [ 0 ] ; delete elem . children ; } // if no children, remove it else if ( elem . children . length === 0 ) { delete elem . children ; } } return elem ; }
Convert the HTML parser DOM format to Jyt format
@param dom
Convert the HTML parser DOM format to Jyt format