"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 |