Skip to content

Commit

Permalink
fix(link): Don't throw exception on invalid URL href
Browse files Browse the repository at this point in the history
When pasting strings with invalid URLs, `new URL()` in `renderHTML()` of
the Link extension threw an error, which made the paste parser choke.

We should catch this exception and handle it gracefully to not break
HTML parsing completely with invalid URLs.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Dec 17, 2024
1 parent 1353d69 commit 6b7e145
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ const Link = TipTapLink.extend({

renderHTML(options) {
const { mark } = options
const url = new URL(mark.attrs.href, window.location)
const href = PROTOCOLS_TO_LINK_TO.includes(url.protocol)
? domHref(mark, this.options.relativePath)
: '#'
let href
try {
const url = new URL(mark.attrs.href, window.location)
href = PROTOCOLS_TO_LINK_TO.includes(url.protocol)
? domHref(mark, this.options.relativePath)
: '#'
} catch (error) {
href = mark.attrs.href
}

Check warning on line 71 in src/marks/Link.js

View check run for this annotation

Codecov / codecov/patch

src/marks/Link.js#L70-L71

Added lines #L70 - L71 were not covered by tests
return ['a', {
...mark.attrs,
href,
Expand Down

0 comments on commit 6b7e145

Please sign in to comment.