Skip to content

Commit 3667ca1

Browse files
authored
Merge pull request #22 from luikore/master
fix(index): escape double quotes in html attributes
2 parents e7093ae + cd277a5 commit 3667ca1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,10 @@ function attrs (obj) {
127127
for (var key in obj) {
128128
if (typeof obj[key] === 'boolean' && obj[key]) {
129129
attr += ' ' + key
130-
} else if (
131-
typeof obj[key] === 'string' ||
132-
typeof obj[key] === 'number'
133-
) {
130+
} else if (typeof obj[key] === 'number') {
134131
attr += ' ' + key + '="' + obj[key] + '"'
132+
} else if (typeof obj[key] === 'string') {
133+
attr += ' ' + key + '="' + obj[key].replace(/"/g, '"') + '"'
135134
}
136135
}
137136

test/render.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ describe('PostHTML Render', function () {
103103

104104
expect(render(fixture)).to.eql(expected)
105105
})
106+
107+
it('{String} (double quotes)', function () {
108+
var fixture = {
109+
attrs: {
110+
onclick: 'alert("hello world")'
111+
}
112+
}
113+
var expected = '<div onclick="alert(&quot;hello world&quot;)"></div>'
114+
115+
expect(render(fixture)).to.eql(expected)
116+
})
106117
})
107118

108119
describe('Content', function () {

0 commit comments

Comments
 (0)