forked from nbubna/HTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.js
More file actions
101 lines (98 loc) · 3.61 KB
/
stringify.js
File metadata and controls
101 lines (98 loc) · 3.61 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(function(window, document, HTML) {
"use strict";
var fn = HTML._.fn.stringify = function(markup, indent) {
var s = '';
this.each(function(el) {
s += _.print(el, markup||false, indent||'');
});
return s;
},
_ = fn._ = {
map: Array.prototype.map,
specialPrefix: '_',
markup: {
'\n': '<br>',
'<': '<span class="markup"><</span>',
'>': '<span class="markup">></span>',
'</': '<span class="markup"></</span>',
'\t': ' '
},
plain: {
'\n': '\n',
'<': '<',
'>': '>',
'</': '</',
'/>': '/>',
'\t': ' '
},
type: {
attr: 'attr',
string: 'string',
tag: 'tag',
},
print: function(el, markup, indent) {
var tag = el.tagName.toLowerCase(),
code = markup ? _.markup : _.plain,
line = _.isInline(el) ? '' : code['\n'],
content = _.content(el, markup, indent+code['\t'], line),
attrs = _.attrs(el, markup),
special = markup ? _.special(el) : [];
if (markup) {
tag = _.mark(tag, _.type.tag);
}
var open = _.mark(code['<'] + tag + (attrs ? ' '+attrs : '') + code['>'], special),
close = _.mark(code['</'] + tag + code['>'], special);
if (content && line) {
content = line + content + line + indent;
}
return indent + open + content + close;
},
isInline: function(el) {
return (el.currentStyle || window.getComputedStyle(el,'')).display === 'inline' ||
el.tagName.match(/^(H\d|LI)$/i);
},
content: function(el, markup, indent, line) {
var s = [];
for (var i=0, m= el.childNodes.length; i<m; i++) {
var node = el.childNodes[i];
if (node.tagName) {
s.push(_.print(node, markup, line ? indent : ''));
} else if (node.nodeType === 3) {
var text = node.textContent.replace(/^\s+|\s+$/g, ' ');
if (text.match(/[^\s]/)) {
s.push(text);
}
}
}
return s.join(line);
},
attrs: function(el, markup) {
return _.map.call(el.attributes, function(attr) {
var name = attr.nodeName,
value = attr.nodeValue;
if (!markup || name.indexOf(_.specialPrefix) !== 0) {
return markup ? _.mark(name+'=', _.type.attr) + _.mark('"'+value+'"', _.type.string)
: name+'="'+value+'"';
}
}).filter(_.notEmpty).join(' ');
},
special: function(el) {
return _.map.call(el.attributes, function(attr) {
var name = attr.nodeName;
if (name.indexOf(_.specialPrefix) === 0) {
return name.substr(1)+'="'+attr.nodeValue+'"';
}
}).filter(_.notEmpty);
},
mark: function(value, attrs) {
if (attrs.length) {
if (typeof attrs === "string"){ attrs = ['class="'+attrs+'"']; }
return '<span '+attrs.join(' ')+'>'+value+'</span>';
}
return value;
},
notEmpty: function(s) {
return s !== undefined && s !== null && s !== '';
}
};
})(window, document, HTML);