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- authored and backportbot[bot] committed Dec 17, 2024
1 parent cb01720 commit ae65f9a
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 = '#'
}
return ['a', {
...mark.attrs,
href,
Expand Down

0 comments on commit ae65f9a

Please sign in to comment.