Skip to content

Commit b26dac9

Browse files
committed
feat: 🎸 allow class inherits to access document, body, head
1 parent 5e92059 commit b26dac9

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

.changeset/dull-fans-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperse/html-webpack-plugin-loader": patch
3+
---
4+
5+
allow class inherits to access document, body, head

src/parser/TemplateParser.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { DefaultTreeAdapterTypes } from 'parse5';
2-
import { serialize } from 'parse5';
1+
import { type DefaultTreeAdapterTypes, parseFragment, serialize } from 'parse5';
32
import type {
43
ScriptInlineItem,
54
ScriptItem,
@@ -16,9 +15,9 @@ import { upsertScripts } from './upsertScripts.js';
1615
import { upsertTitle } from './upsertTitle.js';
1716

1817
export class TemplateParser {
19-
private document: DefaultTreeAdapterTypes.Document;
20-
private head: DefaultTreeAdapterTypes.Element;
21-
private body: DefaultTreeAdapterTypes.Element;
18+
protected readonly document: DefaultTreeAdapterTypes.Document;
19+
protected readonly head: DefaultTreeAdapterTypes.Element;
20+
protected readonly body: DefaultTreeAdapterTypes.Element;
2221

2322
constructor(htmlSource: string) {
2423
const { document, head, body } = parseDocument(htmlSource);
@@ -120,4 +119,13 @@ export class TemplateParser {
120119
public serialize(): string {
121120
return serialize(this.document);
122121
}
122+
123+
/**
124+
* Parse a fragment of html
125+
* @param html - The html fragment to parse
126+
* @returns The parsed document fragment
127+
*/
128+
public parseFragment(html: string): DefaultTreeAdapterTypes.DocumentFragment {
129+
return parseFragment(html);
130+
}
123131
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { type DefaultTreeAdapterTypes } from 'parse5';
2+
13
/**
24
* The position of the script
35
*/

0 commit comments

Comments
 (0)