-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (179 loc) · 5.47 KB
/
index.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module.exports = function xReplace (ignoreTagNamesRegex, str, values) {
if ( values === undefined ) {
// Only two arguments were given; shift them to the right
values = str
str = ignoreTagNamesRegex
ignoreTagNamesRegex = /^template$/
}
var pieces = []
var lastCut = 0
for (var x=0; x < str.length; x++) {
if ( str[x] === '{' ) {
if ( str[x+1] !== '{' ) {
// Failed to match interpolation brackets
x++ // Skip next character
continue
}
//
// Normalize quotes
//
var cutPoint = x
var shouldInsertQuote = false
if ( values.__inHtmlAttr__ ) {
if ( str[x-1] === '=' ) {
// No quote; insert our own
shouldInsertQuote = true
}
else if ( str[x-2] === '=' && str[x-1] === '"' ) {
// Double quote; cut it out and insert our own
cutPoint--
shouldInsertQuote = true
}
}
pieces.push( str.substring(lastCut, cutPoint) )
if ( shouldInsertQuote ) pieces.push("'")
lastCut = x + 1 + 2 // extra two for skipping {{
var key = ''
var shouldEscape = true
var currentSource = values
var y = x + 2 // extra two for skipping {{
if ( str[x+2] === '=' ) {
shouldEscape = false
lastCut++ // skip the =
y++ // skip the =
}
// Skip leading whitespace
while (str[y] === ' ' || str[y] === '\n') { y++ }
// Calculate key
while (y < str.length) {
var c = str[y]
if ( c === ' ' || c === '\n' ) {
// Do nothing
}
else if ( c === '}' ) {
// We've reached the end
if ( str[y+1] !== '}' ) {
throw new Error('Invalid syntax (missing closing curly bracket)')
}
var val = currentSource[key]
if ( Array.isArray(val) || Object.prototype.toString.call(val) === '[object Object]' ) {
val = JSON.stringify(val)
}
else {
val = String(val || '')
}
pieces.push(
values.__inHtmlAttr__
? encodeHtmlAttr(val)
: ( shouldEscape ? escapeHTML(val) : val )
)
if ( shouldInsertQuote ) pieces.push("'")
lastCut = y + 2 // extra two for skipping }}
if ( shouldInsertQuote && str[lastCut] === '"' ) {
lastCut++ // Skip doublequote
}
break
}
else if ( c === '.' ) {
// TODO: SUBKEY
currentSource = currentSource[key]
key = ''
}
else {
key += c
}
y++
}
}
else if ( str[x] === '<' ) {
var tagNameEnd = x
while ( str[tagNameEnd+1].match(/[a-zA-Z0-9\-]/) ) { tagNameEnd++ }
if ( tagNameEnd === x ) {
// Not a tag name after all. Nothing to see here; move along!
continue
}
tagNameEnd++
var tagName = str.substring(x+1, tagNameEnd)
pieces.push( str.substring(lastCut, tagNameEnd) )
lastCut = tagNameEnd
//
// HTML attribute-aware string replacing;
// Insert quotes when appropriate.
//
// First, find the end of the opening tag
var openTagEnd = tagNameEnd
while ( str[openTagEnd] !== '>' ) { openTagEnd++ }
openTagEnd++
// Next, x-replace string with quote flag
var attrString = str.substring(tagNameEnd, openTagEnd)
pieces.push(
xReplace(ignoreTagNamesRegex, attrString, Object.assign({ __inHtmlAttr__: true }, values))
)
lastCut = openTagEnd
//
// Attributes are done; only continue if we need to.
//
if ( ! tagName.match(ignoreTagNamesRegex) ) {
// Not an ignored tag name. Parse tag body as normal.
x = openTagEnd - 1 // subtract one to accommodate for the `for` loop's x++
continue
}
//
// At this point we've encountered a tag to ignore.
// Skip to its closing tag.
//
var closeTagEnd = openTagEnd
searchClosingTag:
while ( closeTagEnd < str.length ) {
if (
str[closeTagEnd] === '<' &&
str[closeTagEnd+1] === '/' &&
str.substr(closeTagEnd+2, tagName.length) === tagName
) {
// Search for that last character!
closeTagEnd = closeTagEnd + 2 + tagName.length
while ( closeTagEnd < str.length ) {
if ( str[closeTagEnd] === '>' ) {
closeTagEnd++
if ( closeTagEnd === str.length ) {
// We've reached the end of the string, but that's ok.
// Change it up to skip error handling condition.
closeTagEnd++
}
break searchClosingTag
}
closeTagEnd++
}
}
closeTagEnd++
}
if ( closeTagEnd === str.length ) {
throw new Error('Could not find valid closing tag for `' + tagName + '`')
}
x = closeTagEnd
}
}
// Append any trailing string
if ( lastCut < str.length ) {
pieces.push( str.substring(lastCut) )
}
return pieces.join('')
}
exports.encodeHtmlAttr = encodeHtmlAttr
exports.decodeHtmlAttr = decodeHtmlAttr
var ESC_MAP = {
'&': '&',
'<': '<',
'>': '>',
};
function encodeHtmlAttr(json) {
return json.replace(/'/g, ''')
}
function decodeHtmlAttr(json) {
return json.replace(/'/g, "'")
}
function escapeHTML(s) {
return s.replace(/[&<>]/g, function(c) {
return ESC_MAP[c];
});
}