Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(html): switch from zeed-dom to happy-dom-without-node #5984

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": major
---

Replace `zeed-dom` with `happy-dom-without-node` for broader compatibility of the HTML parser. The only difference you should see is that `happy-dom-without-node` will output `xmlns="http://www.w3.org/1999/xhtml"` on root elements, which makes it compliant with the HTML5 specification.
2 changes: 1 addition & 1 deletion demos/src/GuideContent/GenerateHTML/React/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ context('/src/GuideContent/GenerateHTML/React/', () => {
it('should render the content as an HTML string', () => {
cy.get('pre code').should('exist')

cy.get('pre code').should('contain', '<p>Example <strong>Text</strong></p>')
cy.get('pre code').should('contain', '<p xmlns="http://www.w3.org/1999/xhtml">Example <strong>Text</strong></p>')
})
})
2 changes: 1 addition & 1 deletion demos/src/GuideContent/GenerateHTML/Vue/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ context('/src/GuideContent/GenerateHTML/Vue/', () => {
it('should render the content as an HTML string', () => {
cy.get('pre code').should('exist')

cy.get('pre code').should('contain', '<p>Example <strong>Text</strong></p>')
cy.get('pre code').should('contain', '<p xmlns="http://www.w3.org/1999/xhtml">Example <strong>Text</strong></p>')
})
})
4 changes: 2 additions & 2 deletions demos/src/GuideContent/StaticRenderHTML/Vue/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context('/src/GuideContent/GenerateHTML/Vue/', () => {
context('/src/GuideContent/StaticRenderHTML/Vue/', () => {
before(() => {
cy.visit('/src/GuideContent/GenerateHTML/Vue/')
cy.visit('/src/GuideContent/StaticRenderHTML/Vue/')
})

it('should render the content as an HTML string', () => {
Expand Down
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
Loading