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

[Bug]: After pasting HTML content with <a> tags link marks are created in wrong locations #4769

Closed
1 of 2 tasks
romansp opened this issue Jan 10, 2024 · 8 comments
Closed
1 of 2 tasks
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@romansp
Copy link
Contributor

romansp commented Jan 10, 2024

Which packages did you experience the bug in?

extension-link

What Tiptap version are you using?

2.2.1

What’s the bug you are facing?

I'm trying to paste HTML content from another web page into tiptap.

  1. Go to https://templates.tiptap.dev/
  2. Don't clear the template document and paste the following HTML content with links in the middle of the doc.

--- start copying below ---

Some search engines:

What is a search engine: https://en.wikipedia.org/wiki/Search_engine

--- stop copying above ---

  1. Check pasted content and inspect browser console logs.

What browser are you using?

Chrome

Code example

https://stackblitz.com/edit/vue3-vite-starter-pj5vfd

What did you expect to happen?

Content should be pasted with links parsed correctly and no console errors should be thrown.

Anything to add? (optional)

Stack trace

index.js:87 
 Uncaught TypeError: Cannot read properties of undefined (reading 'nodeSize')
    at _Fragment.nodesBetween (index.js:87:60)
    at _Node.nodesBetween (index.js:1212:22)
    at getMarksBetween (getMarksBetween.ts:28:9)
    at PasteRule.handler (markPasteRule.ts:42:31)
    at PasteRule.ts:137:28
    at Array.forEach (<anonymous>)
    at PasteRule.ts:125:13
    at _Fragment.nodesBetween (index.js:88:31)
    at _Node.nodesBetween (index.js:1212:22)
    at run (PasteRule.ts:114:13)
nodesBetween	@	index.js:87
nodesBetween	@	index.js:1212
getMarksBetween	@	getMarksBetween.ts:28
handler	@	markPasteRule.ts:42
(anonymous)	@	PasteRule.ts:137
(anonymous)	@	PasteRule.ts:125
nodesBetween	@	index.js:88
nodesBetween	@	index.js:1212
run	@	PasteRule.ts:114
appendTransaction	@	PasteRule.ts:235
applyTransaction	@	index.js:808
apply	@	index.js:773
dispatchTransaction	@	Editor.ts:349
dispatch	@	index.js:5406
doPaste	@	index.js:3437
editHandlers.paste	@	index.js:3449
view.dom.addEventListener.view.input.eventHandlers.<computed>	@	index.js:2900

Introduced in tiptap v2.1.14, most likely with this PR #4700.

I'm not sure if parsing HTML content from PasteEvent even required here at all, index values from html won't match locations in text. I assume that's why exception is thrown.

if (html) {
const dom = new DOMParser().parseFromString(html, 'text/html')
const anchors = dom.querySelectorAll('a')
if (anchors.length) {
[...anchors].forEach(anchor => (foundLinks.push({
text: anchor.innerText,
data: {
href: anchor.getAttribute('href'),
},
// get the index of the anchor inside the text
// and add the length of the anchor text
index: dom.body.innerText.indexOf(anchor.innerText) + anchor.innerText.length,
})))
}
}

By that point HTML content and anchor tags were already parsed by prosemirror via parseHTML and schema attributes.

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@romansp romansp added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Jan 10, 2024
@galdeguer-ut
Copy link

Same problem here, it seems related with #4700

@davidliudev
Copy link

Same issue here.

@romansp
Copy link
Contributor Author

romansp commented Jan 31, 2024

This is now reproducible on https://templates.tiptap.dev/ with the HTML content example from reproduction steps.

If HTML is pasted in the middle of the document link marks are mismatched and set in incorrect locations.

@romansp romansp changed the title [Bug]: Pasting HTML content with links produces console errors, sometimes failing to paste any content at all [Bug]: After pasting HTML content with <a> tags link marks are created in wrong locations Jan 31, 2024
@galdeguer-ut
Copy link

Same here, I still see the nodeSize error pasting content with links:

Captura de pantalla 2024-02-01 a las 9 45 16

It throws the exception in the prosemirror-model package, here.

@nk11dev
Copy link

nk11dev commented Feb 4, 2024

Same problem: Cannot read properties of undefined (reading 'nodeSize').

I noticed that this error occurs when pasting content that has lists with links. Otherwise, when only the link is pasting, this error does not appear to me.

@jakebarshickf1nx
Copy link

I'm still experiencing many issues with pasting content with links in anything after version 2.1.10

@HananoshikaYomaru
Copy link

this issue is not fixed

CleanShot 2024-02-10 at 07 38 46@2x

using tiptap 2.2.2

@romansp
Copy link
Contributor Author

romansp commented Feb 21, 2024

After some initial testing this appears fixed in 2.2.3. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
Status: Done
Development

No branches or pull requests

6 participants