-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 htmlToDocumentFragment,htmlToElement
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`htmlToDom htmlToDocumentFragment 正常 1`] = ` | ||
<DocumentFragment> | ||
test | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`htmlToDom htmlToDocumentFragment 正常 2`] = ` | ||
<DocumentFragment> | ||
test | ||
<a> | ||
hello | ||
</a> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`htmlToDom htmlToDocumentFragment 正常 3`] = ` | ||
<DocumentFragment> | ||
<a> | ||
hello | ||
</a> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`htmlToDom htmlToDocumentFragment 正常 4`] = `"hello"`; | ||
|
||
exports[`htmlToDom htmlToElement 正常 1`] = `undefined`; | ||
|
||
exports[`htmlToDom htmlToElement 正常 2`] = ` | ||
<a> | ||
hello | ||
</a> | ||
`; | ||
|
||
exports[`htmlToDom htmlToElement 正常 3`] = ` | ||
<a> | ||
hello | ||
</a> | ||
`; |
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,20 @@ | ||
import { htmlToDocumentFragment, htmlToElement } from './htmlToDom' | ||
|
||
describe('htmlToDom', () => { | ||
test('htmlToDocumentFragment 正常', () => { | ||
expect(htmlToDocumentFragment('test')).toMatchSnapshot() | ||
expect(htmlToDocumentFragment('test <a>hello</a>')).toMatchSnapshot() | ||
expect(htmlToDocumentFragment('<a>hello</a>')).toMatchSnapshot() | ||
expect( | ||
htmlToDocumentFragment('<a>hello</a>') | ||
.querySelector('a') | ||
?.textContent?.trim(), | ||
).toMatchSnapshot() | ||
}) | ||
|
||
test('htmlToElement 正常', () => { | ||
expect(htmlToElement('test')).toMatchSnapshot() | ||
expect(htmlToElement('test <a>hello</a>')).toMatchSnapshot() | ||
expect(htmlToElement('<a>hello</a>')).toMatchSnapshot() | ||
}) | ||
}) |
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,27 @@ | ||
// https://github.com/fregante/doma/blob/master/index.ts | ||
|
||
/** | ||
* 将 HTML 字符串转为 DocumentFragment。 | ||
* | ||
* @param html HTML 字符串 | ||
*/ | ||
export function htmlToDocumentFragment(html: string): DocumentFragment { | ||
if (html == null) { | ||
return new DocumentFragment() | ||
} | ||
|
||
const template = document.createElement('template') | ||
template.innerHTML = html | ||
return template.content | ||
} | ||
|
||
/** | ||
* 将 HTML 字符串转为 Element。 | ||
* | ||
* @param html HTML 字符串 | ||
*/ | ||
export function htmlToElement<T extends Element = Element>( | ||
html: string, | ||
): T | undefined { | ||
return (htmlToDocumentFragment(html).firstElementChild as T) ?? undefined | ||
} |
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