-
Notifications
You must be signed in to change notification settings - Fork 638
/
StringWriter.js
67 lines (58 loc) · 1.52 KB
/
StringWriter.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
"use strict";
var escapeDoubleQuotes = require("./helpers/escape-quotes")
.___escapeDoubleQuotes;
function StringWriter() {
this._content = "";
this._scripts = "";
this._data = null;
}
StringWriter.prototype = {
write: function(str) {
this._content += str;
},
script: function(str) {
if (str) {
this._scripts += (this._scripts ? ";" : "") + str;
}
},
get: function(key) {
const extra = (this._data = this._data || {});
return (extra[key] = extra[key] || []);
},
merge: function(otherWriter) {
this._content += otherWriter._content;
this._scripts += otherWriter._scripts;
if (otherWriter._data) {
if (this._data) {
for (const key in otherWriter._data) {
if (this._data[key]) {
this._data[key].push.apply(this._data[key], otherWriter._data[key]);
} else {
this._data[key] = this._writer[key];
}
}
} else {
this._data = otherWriter._data;
}
}
},
clear: function() {
this._content = "";
this._scripts = "";
this._data = null;
},
toString: function() {
this.state.events.emit("___toString", this);
let str = this._content;
if (this._scripts) {
const outGlobal = this.state.root.global;
const cspNonce = outGlobal.cspNonce;
const nonceAttr = cspNonce
? ' nonce="' + escapeDoubleQuotes(cspNonce) + '"'
: "";
str += `<script${nonceAttr}>${this._scripts}</script>`;
}
return str;
}
};
module.exports = StringWriter;