Skip to content

Commit

Permalink
feat(html): switch from zeed-dom to happy-dom-without-node
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Jan 8, 2025
1 parent 7d3b1a8 commit 32723bd
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-days-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/html": minor
---

Replace `zeed-dom` with `happy-dom` for broader compatibility of the HTML parser.
2 changes: 1 addition & 1 deletion packages/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@tiptap/pm": "^3.0.0-next.1"
},
"dependencies": {
"zeed-dom": "^0.15.1"
"happy-dom-without-node": "^14.12.3"
},
"repository": {
"type": "git",
Expand Down
9 changes: 6 additions & 3 deletions packages/html/src/generateJSON.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Extensions, getSchema } from '@tiptap/core'
import { DOMParser, ParseOptions } from '@tiptap/pm/model'
import { parseHTML } from 'zeed-dom'
import { DOMParser as HappyDOMParser, Window as HappyDOMWindow } from 'happy-dom-without-node'

/**
* Generates a JSON object from the given HTML string and converts it into a Prosemirror node with content.
Expand All @@ -16,7 +16,10 @@ import { parseHTML } from 'zeed-dom'
*/
export function generateJSON(html: string, extensions: Extensions, options?: ParseOptions): Record<string, any> {
const schema = getSchema(extensions)
const dom = parseHTML(html) as unknown as Node

return DOMParser.fromSchema(schema).parse(dom, options).toJSON()
const parseInstance = window ? new window.DOMParser() : new HappyDOMParser(new HappyDOMWindow())

return DOMParser.fromSchema(schema)
.parse(parseInstance.parseFromString(html, 'text/html').body as Node, options)
.toJSON()
}
16 changes: 10 additions & 6 deletions packages/html/src/getHTMLFromFragment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DOMSerializer, Node, Schema } from '@tiptap/pm/model'
import { createHTMLDocument, VHTMLDocument } from 'zeed-dom'
import { Window } from 'happy-dom-without-node'

/**
* Returns the HTML string representation of a given document node.
Expand All @@ -23,10 +23,14 @@ export function getHTMLFromFragment(doc: Node, schema: Schema, options?: { docum
return wrap.innerHTML
}

// Use zeed-dom for serialization.
const zeedDocument = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, {
document: createHTMLDocument() as unknown as Document,
}) as unknown as VHTMLDocument
// Use happy-dom for serialization.
const browserWindow = window || new Window()

return zeedDocument.render()
const fragment = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, {
document: browserWindow.document as unknown as Document,
})

const serializer = new browserWindow.XMLSerializer()

return serializer.serializeToString(fragment as any)
}
76 changes: 43 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 32723bd

Please sign in to comment.