Skip to content

Commit

Permalink
fix(html): better server-side handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Jan 11, 2025
1 parent b69f500 commit cd76bb5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/html/src/generateJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { DOMParser as HappyDOMParser, Window as HappyDOMWindow } from 'happy-dom
export function generateJSON(html: string, extensions: Extensions, options?: ParseOptions): Record<string, any> {
const schema = getSchema(extensions)

const parseInstance = window ? new window.DOMParser() : new HappyDOMParser(new HappyDOMWindow())
const parseInstance =
typeof window !== 'undefined' ? new window.DOMParser() : new HappyDOMParser(new HappyDOMWindow())

return DOMParser.fromSchema(schema)
.parse(parseInstance.parseFromString(html, 'text/html').body as Node, options)
Expand Down
2 changes: 1 addition & 1 deletion packages/html/src/getHTMLFromFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getHTMLFromFragment(doc: Node, schema: Schema, options?: { docum
}

// Use happy-dom for serialization.
const browserWindow = window || new Window()
const browserWindow = typeof window === 'undefined' ? new Window() : window

const fragment = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, {
document: browserWindow.document as unknown as Document,
Expand Down

0 comments on commit cd76bb5

Please sign in to comment.