Skip to content

Commit 4035a63

Browse files
committed
properly escape html
1 parent 0af5e42 commit 4035a63

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
var types = require("./codepen-data");
22
var languageHTML = /language-(\w+)/;
33

4+
var assign = Object.assign || function(d, s) {
5+
for(var prop in s) {
6+
d[prop] = s[prop];
7+
}
8+
return d;
9+
};
410

511
function cleanCodePenData(data) {
612
if(docObject.codepen) {
@@ -13,6 +19,13 @@ function cleanCodePenData(data) {
1319
}
1420

1521
function createCodePen(data) {
22+
if(data.html) {
23+
// HTML needs to be escaped because put this in the page
24+
data = assign({}, data);
25+
data.html = data.html.replace(/&/g,"&")
26+
.replace(/</g,"&lt;")
27+
.replace(/>/g,"&gt;");
28+
}
1629

1730
var JSONstring =
1831
JSON.stringify(data)
@@ -22,13 +35,12 @@ function createCodePen(data) {
2235

2336

2437
var form = '<form action="https://codepen.io/pen/define" method="POST" target="_blank">' +
25-
'<input type="hidden" name="data" value=\'' +
26-
JSONstring +
27-
'\'>' +
38+
'<input type="hidden" name="data">' +
2839
'</form>';
2940

3041
var div = document.createElement("div");
3142
div.innerHTML = form;
43+
div.firstChild.firstChild.value = JSONstring;
3244
document.body.appendChild(div);
3345
div.firstChild.submit();
3446
setTimeout(function(){

0 commit comments

Comments
 (0)