-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.js
More file actions
82 lines (63 loc) · 2.32 KB
/
stringify.js
File metadata and controls
82 lines (63 loc) · 2.32 KB
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
function _stringifyAttrs(attrs) {
var result = '', _quote;
if( !attrs ) return result;
for( var key in attrs ) {
if( key !== '$' && key !== '_' ) {
_quote = /"/.test(attrs[key]) ? '\'' : '"';
result += ' ' + key;
result += attrs[key] ? ('=' + _quote + attrs[key] + _quote) : '';
}
}
return result;
}
function _renderAttrs () {
return _stringifyAttrs(this.attrs);
}
function _indentationSpaces (n, i) {
var spaces = n || i ? '\n' : '';
for( ; n > 0; n-- ) spaces += ' ';
return spaces;
}
function _processNode (_node, processor, options, i, indent_level) {
if( typeof _node === 'string' ) return _node;
var node = Object.create(_node), result;
if( processor instanceof Function ) {
for( var key in _node ) {
if( key !== '$' && key !== '_' ) node[key] = _node[key];
}
_node.__parent__ = _node;
result = processor(node, function _renderChildren () {
return this._ ? _stringifyNodes(this._, options, indent_level + 1) : '';
}, _renderAttrs.bind(node) );
if( typeof result === 'string' ) return result;
}
result = '';
if( node.$ ) {
if( options.prettify_markup ) result += _indentationSpaces(indent_level, i);
result += '<' + node.$ + _stringifyAttrs(node.attrs) + ( node.self_closed ? '/' : '' ) + '>' + (options.prettify_markup && node.warn ? '\n' : '');
if( '_' in node ) result += _stringifyNodes(node._, options, indent_level + (node.warn ? 0 : 1) );
if( !node.self_closed && !node.unclosed ) {
if( options.prettify_markup && node._ instanceof Array ) result += '\n';
result += '</' + node.$ + '>';
}
} else if( 'comments' in node ) {
if( options.remove_comments === false ) {
if( options.prettify_markup ) result += _indentationSpaces(indent_level, i);
result += '<!--' + node.comments + '-->';
}
} else {
result += node.text || '';
}
return result;
}
function _stringifyNodes (nodes, options, indent_level) {
if( typeof nodes === 'string' ) return nodes;
return nodes.reduce(function (html, node, i) {
return html + _processNode(node, options.processors[node.$], options, i, indent_level);
}, '');
}
module.exports = function stringifyNodes (nodes, options) {
options = options || {};
options.processors = options.processors ||{};
return _stringifyNodes(nodes, options, 0);
};