serializer.js
5.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
var DefaultTreeAdapter = require('../tree_adapters/default'),
Doctype = require('../common/doctype'),
Utils = require('../common/utils'),
HTML = require('../common/html');
//Aliases
var $ = HTML.TAG_NAMES,
NS = HTML.NAMESPACES;
//Default serializer options
var DEFAULT_OPTIONS = {
encodeHtmlEntities: true
};
//Escaping regexes
var AMP_REGEX = /&/g,
NBSP_REGEX = /\u00a0/g,
DOUBLE_QUOTE_REGEX = /"/g,
LT_REGEX = /</g,
GT_REGEX = />/g;
//Escape string
function escapeString(str, attrMode) {
str = str
.replace(AMP_REGEX, '&')
.replace(NBSP_REGEX, ' ');
if (attrMode)
str = str.replace(DOUBLE_QUOTE_REGEX, '"');
else {
str = str
.replace(LT_REGEX, '<')
.replace(GT_REGEX, '>');
}
return str;
}
//Enquote doctype ID
//Serializer
var Serializer = module.exports = function (treeAdapter, options) {
this.treeAdapter = treeAdapter || DefaultTreeAdapter;
this.options = Utils.mergeOptions(DEFAULT_OPTIONS, options);
};
//API
Serializer.prototype.serialize = function (node) {
this.html = '';
this._serializeChildNodes(node);
return this.html;
};
//Internals
Serializer.prototype._serializeChildNodes = function (parentNode) {
var childNodes = this.treeAdapter.getChildNodes(parentNode);
if (childNodes) {
for (var i = 0, cnLength = childNodes.length; i < cnLength; i++) {
var currentNode = childNodes[i];
if (this.treeAdapter.isElementNode(currentNode))
this._serializeElement(currentNode);
else if (this.treeAdapter.isTextNode(currentNode))
this._serializeTextNode(currentNode);
else if (this.treeAdapter.isCommentNode(currentNode))
this._serializeCommentNode(currentNode);
else if (this.treeAdapter.isDocumentTypeNode(currentNode))
this._serializeDocumentTypeNode(currentNode);
}
}
};
Serializer.prototype._serializeElement = function (node) {
var tn = this.treeAdapter.getTagName(node),
ns = this.treeAdapter.getNamespaceURI(node);
this.html += '<' + tn;
this._serializeAttributes(node);
this.html += '>';
if (tn !== $.AREA && tn !== $.BASE && tn !== $.BASEFONT && tn !== $.BGSOUND && tn !== $.BR && tn !== $.BR &&
tn !== $.COL && tn !== $.EMBED && tn !== $.FRAME && tn !== $.HR && tn !== $.IMG && tn !== $.INPUT &&
tn !== $.KEYGEN && tn !== $.LINK && tn !== $.MENUITEM && tn !== $.META && tn !== $.PARAM && tn !== $.SOURCE &&
tn !== $.TRACK && tn !== $.WBR) {
if (tn === $.PRE || tn === $.TEXTAREA || tn === $.LISTING) {
var firstChild = this.treeAdapter.getFirstChild(node);
if (firstChild && this.treeAdapter.isTextNode(firstChild)) {
var content = this.treeAdapter.getTextNodeContent(firstChild);
if (content[0] === '\n')
this.html += '\n';
}
}
var childNodesHolder = tn === $.TEMPLATE && ns === NS.HTML ?
this.treeAdapter.getChildNodes(node)[0] :
node;
this._serializeChildNodes(childNodesHolder);
this.html += '</' + tn + '>';
}
};
Serializer.prototype._serializeAttributes = function (node) {
var attrs = this.treeAdapter.getAttrList(node);
for (var i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
var attr = attrs[i],
value = this.options.encodeHtmlEntities ? escapeString(attr.value, true) : attr.value;
this.html += ' ';
if (!attr.namespace)
this.html += attr.name;
else if (attr.namespace === NS.XML)
this.html += 'xml:' + attr.name;
else if (attr.namespace === NS.XMLNS) {
if (attr.name !== 'xmlns')
this.html += 'xmlns:';
this.html += attr.name;
}
else if (attr.namespace === NS.XLINK)
this.html += 'xlink:' + attr.name;
else
this.html += attr.namespace + ':' + attr.name;
this.html += '="' + value + '"';
}
};
Serializer.prototype._serializeTextNode = function (node) {
var content = this.treeAdapter.getTextNodeContent(node),
parent = this.treeAdapter.getParentNode(node),
parentTn = void 0;
if (parent && this.treeAdapter.isElementNode(parent))
parentTn = this.treeAdapter.getTagName(parent);
if (parentTn === $.STYLE || parentTn === $.SCRIPT || parentTn === $.XMP || parentTn === $.IFRAME ||
parentTn === $.NOEMBED || parentTn === $.NOFRAMES || parentTn === $.PLAINTEXT || parentTn === $.NOSCRIPT) {
this.html += content;
}
else
this.html += this.options.encodeHtmlEntities ? escapeString(content, false) : content;
};
Serializer.prototype._serializeCommentNode = function (node) {
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
};
Serializer.prototype._serializeDocumentTypeNode = function (node) {
var name = this.treeAdapter.getDocumentTypeNodeName(node),
publicId = this.treeAdapter.getDocumentTypeNodePublicId(node),
systemId = this.treeAdapter.getDocumentTypeNodeSystemId(node);
this.html += '<' + Doctype.serializeContent(name, publicId, systemId) + '>';
};