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 ffa9b6b commit 005b208
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)
: '#'

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

View check run for this annotation

Codecov / codecov/patch

src/marks/Link.js#L68

Added line #L68 was not covered by tests
} catch (error) {
href = '#'

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

View check run for this annotation

Codecov / codecov/patch

src/marks/Link.js#L70

Added line #L70 was not covered by tests
}
return ['a', {
...mark.attrs,
href,
Expand Down

0 comments on commit 005b208

Please sign in to comment.