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

Resolves #4089 #5515

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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": minor
---

Replaced zeed-dom with happy-dom
74 changes: 45 additions & 29 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions packages/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
"@tiptap/core": "^2.6.4",
"@tiptap/pm": "^2.6.4"
},
"dependencies": {
"zeed-dom": "^0.10.11"
},
"repository": {
"type": "git",
"url": "https://github.com/ueberdosis/tiptap",
Expand All @@ -47,5 +44,8 @@
"scripts": {
"clean": "rm -rf dist",
"build": "npm run clean && rollup -c"
},
"dependencies": {
"happy-dom": "^14.12.3"
}
}
12 changes: 9 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 } from 'happy-dom'

/**
* Generates a JSON object from the given HTML string and converts it into a Prosemirror node with content.
Expand All @@ -16,7 +16,13 @@ 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
const window = new Window()
const dom = new HappyDomParser(window).parseFromString(
html,
'text/html',
).body

return DOMParser.fromSchema(schema).parse(dom, options).toJSON()
return DOMParser.fromSchema(schema)
.parse(dom as never, options)
.toJSON()
}
19 changes: 13 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'

/**
* Returns the HTML string representation of a given document node.
Expand All @@ -23,10 +23,17 @@ 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 window = new Window()

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

const serializer = new window.XMLSerializer()

return serializer.serializeToString(fragment as never)
}
14 changes: 13 additions & 1 deletion tests/cypress/integration/html/generateHTML.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { generateHTML } from '@tiptap/html'
import Youtube from '@tiptap/extension-youtube'
import { generateHTML, generateJSON } from '@tiptap/html'

describe('generateHTML', () => {
it('generate HTML from JSON without an editor instance', () => {
Expand All @@ -26,4 +27,15 @@ describe('generateHTML', () => {

expect(html).to.eq('<p>Example Text</p>')
})

it('can convert from & to html', async () => {
const extensions = [Document, Text, Youtube]
const html = `<p>Tiptap now supports YouTube embeds! Awesome!</p>
<div data-youtube-video>
<iframe src="https://www.youtube.com/watch?v=cqHqLQgVCgY"></iframe>
</div>`
const json = generateJSON(html, extensions)

expect(generateHTML(json, extensions)).to.equal(html)
})
Comment on lines +30 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we had more tests to give more confidence here 😓

})