Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update voby-via-demo
  • Loading branch information
wongchichong committed Mar 13, 2023
commit 2a69170c48d26870f57a31f3d6ff2c61e8f6eda0
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"amdName": "vhtml",
"version": "2.2.1",
"description": "Hyperscript reviver that constructs a sanitized HTML string.",
"main": "dist/vhtml.js",
"minified:main": "dist/vhtml.min.js",
"jsnext:main": "src/vhtml.js",
"main": "./dist/vhtml.js",
"minified:main": "./dist/vhtml.umd.js",
"jsnext:main": "./src/vhtml.ts",
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "vite build && tsc --declaration && pnpm size",
Expand Down
30 changes: 12 additions & 18 deletions src/vhtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,26 @@ export default function h(name: string | Function | null, attrs: any, ..._args:
// return name(attrs, stack.reverse());
}

const loopAttr = () => {
if (attrs) for (let i in attrs) {
if (attrs[i] !== false && attrs[i] != null && i !== setInnerHTMLAttr) {
//@ts-ignore
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`
}
}
}

if (name) {
if (name === '!') {
s += '<!-- '
if (attrs) for (let i in attrs) {
if (attrs[i] !== false && attrs[i] != null && i !== setInnerHTMLAttr) {
//@ts-ignore
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`
}
}
loopAttr()
}
else if (name === 'text') {
if (attrs) for (let i in attrs) {
if (attrs[i] !== false && attrs[i] != null && i !== setInnerHTMLAttr) {
//@ts-ignore
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`
}
}
loopAttr()
}
else {
s += '<' + name
if (attrs) for (let i in attrs) {
if (attrs[i] !== false && attrs[i] != null && i !== setInnerHTMLAttr) {
//@ts-ignore
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`
}
}
loopAttr()
s += '>'
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/vhtml.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('vhtml', () => {
)
})

// it('should sanitize function', () => {
// expect(
// <div onClick={() => alert('hello')} />
// ).to.equal(
// `<div onclick="&amp;&lt;&gt;&quot;&apos;"></div>`
// )
// })

it('should not sanitize the "dangerouslySetInnerHTML" attribute, and directly set its `__html` property as innerHTML', () => {
expect(
<div dangerouslySetInnerHTML={{ __html: "<span>Injected HTML</span>" }} />
Expand Down