forked from squirrellyjs/squirrelly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
161 lines (141 loc) · 4.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Squirrelly</title>
<script src='./dist/squirrelly.min.js'></script>
</head>
<body>
<div class="leftTitle label" style="white-space:pre-line;">Template</div>
<div class="rightTitle label">Result</div>
<textarea id="template" oninput='render'>
Hi
{{log("Hope you like Squirrelly!")/}}
{{htmlstuff}}
{{foreach(options.obj)}}
Reversed value: {{@this|reverse}}, Key: {{@key}}
{{if(@key==="thirdchild")}}
{{each(options.obj[@key])}}
Salutations. Index: {{@index}}
Old key: {{@../key}}
{{/each}}
{{/if}}
{{/foreach}}
{{customhelper()}}
{{#cabbage}}
Cabbages taste good
{{#pineapple}}
As do pineapples
{{/customhelper}}
This is a partial: {{include("mypartial")/}}
{{tags(--,--)/}}
Custom delimeters!
--arr--
</textarea>
<div id="renderResult" style="white-space:pre-line;"></div>
<div class="leftTitle label">Options</div>
<div class="rightBottomTitle label">Returned Function</div>
<textarea id="options">
"htmlstuff": "<script>alert('hey')</script><p>alert('hey')</p><p>alert('hey')</p><p>alert('hey')</p>",
"arr": ["Hey", "<p>Malicious XSS</p>", "Hey", 3, 12],
"obj": {
"firstchild": "HI",
"secondchild": "HEY",
"thirdchild": [3, 6, 3, 2, 5, 4]
}
</textarea>
<div id="functionResult"></div>
<script>
Sqrl.defineFilter("reverse", function (str) {
var out = ''
for (var i = str.length - 1; i >= 0; i--) {
out += String(str).charAt(i)
}
return out || str
})
Sqrl.defineHelper("customhelper", function (args, content, blocks, options) {
var returnStr = ''
for (var key in blocks) {
if (typeof blocks[key] === 'function') {
returnStr += "Block found named " + key + ", with value: " + blocks[key]()
}
}
return returnStr
})
Sqrl.definePartial("mypartial", "Partial content: the value of arr is {{arr}}")
function escape(str) { //To handle escaping for the function result
var escMap = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/"
}
function replaceChar(s) {
return escMap[s]
}
var newStr = String(str)
if (/[&<>"'\/]/.test(newStr)) {
return newStr.replace(/[&<>"'\/]/g, replaceChar)
} else {
return newStr
}
}
function render() {
var options = JSON.parse("{" + document.getElementById('options').value + "}")
var template = document.getElementById('template').value
var functionResult = Sqrl.Compile(template).toString()
document.getElementById('functionResult').innerHTML = escape(functionResult)
document.getElementById('renderResult').innerHTML = Sqrl.Render(template, options)
}
render()
document.getElementById("template").addEventListener("input", render)
document.getElementById("options").addEventListener("input", render)
</script>
<style>
#template {
width: 48vw;
height: 42vh;
overflow-y: scroll;
}
#options {
width: 48vw;
height: 42vh;
overflow-y: scroll;
}
#renderResult {
width: 47vw;
float: right;
border: 1px solid black;
height: 42vh;
overflow-y: scroll;
}
#functionResult {
width: 47vw;
height: 42vh;
border: 1px solid black;
float: right;
margin: 0;
overflow-y: scroll;
}
.rightTitle {
right: 1em;
top: 1vh;
position: fixed;
display: inline-block;
}
.rightBottomTitle {
right: 1em;
top: 47.5vh;
position: fixed;
display: inline-block;
}
.leftTitle {
width: 10vw;
}
.label {
font-weight: bold;
}
</style>
</body>
</html>