Skip to content

Stop adding nofollow and target to every WYSIWYG link - #130

Closed
Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:fix/wysiwyg-link-rel
Closed

Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:fix/wysiwyg-link-rel

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Problem

Every link inserted through the WYSIWYG toolbar is stored with Tiptap's default link attributes, internal links included. Measured in the block editor with the 7.7.2 code, for an internal URL and for https://wordpress.org/:

Field config "Open in new tab" Stored value
"link": { "opensInNewTab": true } off <a target="_self" rel="noopener noreferrer nofollow" href="...">
"link": { "opensInNewTab": true } on <a target="_blank" rel="noopener noreferrer nofollow" href="...">
"attributes": { "rel": "nofollow" } (test block type-wysiwyg) off <a target="_self" rel="nofollow" href="...">
"attributes": { "rel": "nofollow" } on <a target="_blank" rel="nofollow" href="...">

A stored link without target or rel (written in code mode, pasted, or saved through the API) is rewritten to <a target="_blank" rel="noopener noreferrer nofollow" href="..."> as soon as any text in that field is edited.

nofollow on internal links tells search engines not to follow the site's own links, target="_self" is noise, and the last case silently turns plain links into new tab links.

Cause

  • @tiptap/extension-link defaults HTMLAttributes to { target: '_blank', rel: 'noopener noreferrer nofollow', class: null } (addOptions in src/link.ts), and the target, rel and class mark attributes default to these values (addAttributes).
  • configure() merges options with mergeDeep (Extendable.configure in @tiptap/core), so toolbar.formats.link.attributes only replaces the keys it sets.
  • The link modal calls setLink({ href, target: opensInNewTab ? '_blank' : '_self' }) without rel, so rel keeps its default.
  • When HTML is parsed, a missing attribute falls back to the mark default (injectExtensionAttributesToParseRule), which is why plain stored links pick up target and rel on the next edit.

The Tiptap docs describe resetting these with rel: null and target: null in HTMLAttributes ("Removing and overriding existing html attributes", https://tiptap.dev/docs/editor/extensions/marks/link).

Fix

  • getWysiwygLinkHTMLAttributes() sets target: null and rel: null before the configured toolbar.formats.link.attributes, so configured attributes still win and still apply to every link.
  • getWysiwygLinkAttributes() builds the setLink() attributes from what LinkModal already returns: normalizeLinkChange() sets target="_blank" and rel="noreferrer noopener" for a new tab only, as for the link field. A configured rel is kept and the new tab tokens are added to it. Turning "Open in new tab" off removes target and rel again.

Result in the block editor:

Field config "Open in new tab" Stored value
no attributes off <a href="...">
no attributes on <a target="_blank" rel="noreferrer noopener" href="...">
"attributes": { "rel": "nofollow" } off <a rel="nofollow" href="...">
"attributes": { "rel": "nofollow" } on <a target="_blank" rel="nofollow noreferrer noopener" href="...">
no attributes, stored <a href="...">, one character typed <a href="...">

Existing content is not migrated: links already stored with target="_self" or rel="noopener noreferrer nofollow" keep these attributes until the link is changed in the link modal.

Tests

  • New client test tests/client/wysiwyg-link.test.ts: a headless Tiptap editor without a DOM and the same Link configuration, links inserted the way the modal does, rendered attributes checked. All 5 tests fail with the 7.7.2 behavior and pass with the fix.
  • npm run check passes.
  • Browser check in WordPress Playground with a local build of the editor bundle (not committed): the cases above for an internal and an external URL, for a field with and without attributes. The existing E2E files types/wysiwyg/default.ts, types/wysiwyg/switch.ts and types/repeater/wysiwyg.ts pass before and after (55 of 55).

Relation to #125

#125 moves the extension list into src/blocks/utils/wysiwyg-extensions.ts. Both PRs are based on main and touch the same Link.configure() call, so whichever lands second needs a small rebase: in wysiwyg-extensions.ts the line becomes HTMLAttributes: getWysiwygLinkHTMLAttributes(item), and wysiwyg.tsx only imports getWysiwygLinkAttributes. I checked that combination (lint, types, client tests) and can rebase either branch.

Notes

  • WordPress has since dropped noreferrer for new tab links (Stop adding “noreferrer” to external links. WordPress/gutenberg#26968, rel="noopener" only). This PR keeps the value LinkModal already uses (NEW_TAB_REL in fields/components/link.tsx), so the link field and the WYSIWYG field stay consistent. Changing it there would be a separate change.
  • toolbar.formats.link (opensInNewTab, attributes) is not described in schemas/ or the field docs yet, so there was no documentation to update.

Tiptap's Link extension defaults HTMLAttributes to target="_blank" and
rel="noopener noreferrer nofollow", and configure() merges the field's
toolbar.formats.link.attributes into these defaults key by key. The link
modal then called setLink({ href, target }) with target "_self" when
"Open in new tab" was off and never passed rel. As a result every link
inserted through the toolbar was stored as

  <a target="_self" rel="noopener noreferrer nofollow" href="...">

internal links included, and with "Open in new tab" on as
target="_blank" with the same rel. Stored links without target or rel
(written in code mode or pasted) picked up target="_blank" and
rel="noopener noreferrer nofollow" on the next edit of the field,
because Tiptap falls back to the attribute defaults when parsing.

Reset target and rel to null in HTMLAttributes, as the Tiptap Link docs
describe, and take target and rel from the link modal, which already
sets target="_blank" and rel="noreferrer noopener" for a new tab only.
Attributes configured in toolbar.formats.link.attributes still apply to
every link; a configured rel is kept and the new tab tokens are added
to it.
@dnnsjsk

dnnsjsk commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.4.

The WYSIWYG link-attribute fix has shipped: unwanted target/nofollow attributes are no longer added, and explicit configuration is preserved.

Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution.

@dnnsjsk dnnsjsk closed this Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants