-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// (C)opyright 2021-07-20 Dirk Holtwick, holtwick.it. All rights reserved. | ||
|
||
import { serializeSafeHTML } from './serialize-safehtml' | ||
import { parseHTML } from './vdomparser' | ||
|
||
describe('serialize safe html', () => { | ||
it('should create safe html', () => { | ||
const document = parseHTML(` | ||
<html> | ||
<head> | ||
<title>Hello World</title> | ||
</head> | ||
<body> | ||
<h1>Hello</h1> | ||
<p> | ||
This is a | ||
<b>sample</b> | ||
. > And a link | ||
<a href="https://example.com?x=a%20b&y=1" title="Will this be stripped?">example</a> | ||
. | ||
</p> | ||
<p> | ||
Some & lines | ||
<br /> | ||
line | ||
<br /> | ||
line | ||
</p> | ||
<ol> | ||
<li>One</li> | ||
<li>Two</li> | ||
</ol> | ||
<pre> | ||
<p>Do nothing</p> | ||
</pre> | ||
</body> | ||
`) | ||
|
||
expect(document.render()).toMatchInlineSnapshot(` | ||
" | ||
<html> | ||
<head> | ||
<title>Hello World</title> | ||
</head> | ||
<body> | ||
<h1>Hello</h1> | ||
<p> | ||
This is a | ||
<b>sample</b> | ||
. > And a link | ||
<a href="https://example.com?x=a%20b&y=1" title="Will this be stripped?">example</a> | ||
. | ||
</p> | ||
<p> | ||
Some & lines | ||
<br> | ||
line | ||
<br> | ||
line | ||
</p> | ||
<ol> | ||
<li>One</li> | ||
<li>Two</li> | ||
</ol> | ||
<pre> | ||
<p>Do nothing</p> | ||
</pre> | ||
</body> | ||
</html>" | ||
`) | ||
|
||
const md = serializeSafeHTML(document) | ||
expect(md).toMatchInlineSnapshot(` | ||
"<h1>Hello</h1> | ||
<p>This is a | ||
sample | ||
. > And a link | ||
<a href="https://example.com?x=a%20b&y=1">example</a> | ||
.</p> | ||
<p>Some & lines | ||
<br> | ||
line | ||
<br> | ||
line</p> | ||
<ol><li>One</li> | ||
<li>Two</li></ol> | ||
<p>Do nothing</p>" | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { escapeHTML } from './encoding' | ||
import { VNode, isVElement } from './vdom' | ||
|
||
export const SELECTOR_BLOCK_ELEMENTS = 'p,h1,h2,h3,h4,h5,h6,blockquote,div,ul,ol,li,article,section,footer,nav,hr,form' | ||
|
||
interface SerializeContext { | ||
level: number | ||
count: 0 | ||
mode?: 'ol' | 'ul' | ||
} | ||
|
||
function serialize(node: VNode, context: SerializeContext = { | ||
level: 0, | ||
count: 0, | ||
}): string { | ||
if (node.nodeType === VNode.DOCUMENT_FRAGMENT_NODE) { | ||
return node.children.map(c => serialize(c, { ...context })).join('') | ||
} | ||
|
||
else if (isVElement(node)) { | ||
const tag: string = node.tagName?.toLowerCase() | ||
const handleChildren = (ctx?: Partial<SerializeContext>): string => node.children.map(c => serialize(c, { ...context, ...ctx })).join('') | ||
|
||
const rules: Record<string, () => string> = { | ||
a: () => `<a href="${escapeHTML(node.getAttribute('href') ?? '')}">${handleChildren()}</a>`, | ||
br: () => `<br>`, | ||
title: () => '', | ||
script: () => '', | ||
style: () => '', | ||
head: () => '', | ||
} | ||
|
||
SELECTOR_BLOCK_ELEMENTS.split(',').forEach((tag) => { | ||
rules[tag] = () => `<${tag}>${handleChildren().trim()}</${tag}>` | ||
}) | ||
|
||
const fn = rules[tag] | ||
|
||
if (fn) | ||
return fn() | ||
|
||
return handleChildren() | ||
} | ||
return escapeHTML(node.textContent ?? '') | ||
} | ||
|
||
export function serializeSafeHTML(node: VNode): string { | ||
return serialize(node).trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters