All files / hast-util-from-webparser/dist index.js

100% Statements 50/50
85.19% Branches 23/27
100% Functions 9/9
100% Lines 50/50
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  1x 1x 1x 1x 1x   36x       62x   62x   36x 36x 21x   36x 11x     25x   36x 36x         26x 22x   4x 1x   3x 3x             62x 62x       21x 21x 21x 21x 51x   21x     11x       22x       1x     87x 19x   68x 68x       25x 25x 25x   25x 26x 26x     25x 25x   1x 11x 11x 11x         11x    
"use strict";
const webparser_1 = require("webparser");
let htmlSchema = require('property-information/html');
let svgSchema = require('property-information/svg');
let hastSvg = require('@starptech/prettyhtml-hastscript/svg');
let hast = require('@starptech/prettyhtml-hastscript');
function isFakeRoot(obj) {
    return obj.name === ':webparser:root';
}
/* Transform a node. */
function transform(ast, config) {
    let schema = config.schema;
    let node;
    if (ast instanceof webparser_1.Element) {
        let children;
        config.schema = getNameAndNS(ast.name).ns === 'svg' ? svgSchema : htmlSchema;
        if (ast.children && ast.children.length) {
            children = nodes(ast.children, config);
        }
        if (isFakeRoot(ast)) {
            node = root(ast, children);
        }
        else {
            node = element(ast, children, config);
        }
        node.data = node.data || {};
        node.data.selfClosing =
            ast.startSourceSpan === ast.endSourceSpan &&
                ast.startSourceSpan !== null &&
                ast.endSourceSpan !== null;
    }
    else if (ast instanceof webparser_1.Text) {
        node = text(ast);
    }
    else if (ast instanceof webparser_1.Comment) {
        node = comment(ast);
    }
    else Eif (ast instanceof webparser_1.Doctype) {
        node = {
            type: 'doctype',
            name: 'html',
            public: null,
            system: null
        };
    }
    config.schema = schema;
    return node;
}
/* Transform children. */
function nodes(children, config) {
    let length = children.length;
    let index = -1;
    let result = [];
    while (++index < length) {
        result[index] = transform(children[index], config);
    }
    return result;
}
function root(ast, children) {
    return { type: 'root', children, data: {} };
}
/* Transform a text. */
function text(ast) {
    return { type: 'text', value: ast.value };
}
/* Transform a comment. */
function comment(ast) {
    return { type: 'comment', value: ast.value };
}
function getNameAndNS(name) {
    if (name[0] === ':') {
        return { ns: null, name: name };
    }
    const info = webparser_1.splitNsName(name);
    return { ns: info[0], name: info[1] };
}
/* Transform an element. */
function element(ast, children, config) {
    let fn = config.schema.space === 'svg' ? hastSvg : hast;
    let name = getNameAndNS(ast.name).name;
    let props = {};
    let node;
    for (const attr of ast.attrs) {
        const attrInfo = getNameAndNS(attr.name);
        props[attrInfo.ns ? attrInfo.ns + ':' + attrInfo.name : attrInfo.name] =
            attr.value;
    }
    node = fn(name, props, children);
    return node;
}
module.exports = function from(rootNodes, options) {
    const sourceSpan = new webparser_1.ParseSourceSpan(null, null);
    const fakeRoot = new webparser_1.Element(':webparser:root', [], rootNodes, sourceSpan);
    const result = transform(fakeRoot, {
        schema: htmlSchema,
        file: options.file,
        verbose: options.verbose
    });
    return result;
};
//# sourceMappingURL=index.js.map