Skip to content

Sanitize block container IDs used in selectors and anchors - #3180

Open
faisalahammad wants to merge 1 commit into
Codeinwp:masterfrom
faisalahammad:fix/3085-sanitize-block-ids
Open

faisalahammad wants to merge 1 commit into
Codeinwp:masterfrom
faisalahammad:fix/3085-sanitize-block-ids

Conversation

@faisalahammad

Copy link
Copy Markdown

Closes #3085.

Summary

The HTML Anchor field takes any text, and stored block IDs are used as-is, so IDs like my.anchor:1 end up in the markup and in the generated CSS selectors. Safari reports these as syntax errors in the console and the rules never apply.

This sanitizes IDs at every point where they enter the system:

  • src/blocks/helpers/block-utility.js: new sanitizeBlockId() helper, applied to generated IDs and to stored IDs adopted by addBlockId. An invalid stored ID is cleaned once when the block loads in the editor. If the cleaned form is empty or already used elsewhere on the page, a fresh ID is generated instead. The cleanup also removes the pre-sanitizing ID, so blocks that capture it once on mount (Popup, Modal) do not leak registry entries.
  • src/blocks/components/html-anchor-control/index.js: the anchor input strips invalid characters while typing.
  • inc/css/class-css-utility.php: generate() filters the ID before putting it into selectors, so posts that were saved with invalid IDs stop emitting broken rules without needing a re-save. IDs that cannot form a valid CSS identifier at all (empty after filtering, leading digit, --, -1) drop the rule instead of emitting a selector no browser would apply.
  • inc/plugins/class-abilities.php: block_selector in the forms ability goes through the same sanitizer, so it matches the frontend.

One deliberate deviation from the issue text: _ and non-ASCII characters (e.g. sección_1) are kept. They are valid in HTML IDs and in CSS selectors, and they never cause the reported warnings. Stripping them would rename working anchors, break styling on live sites that use them, and shift Form optionName keys derived from the ID. Generated IDs only use [a-z0-9-] and stay byte for byte the same.

The anchor input part covers #3086 as well, which can be closed as a duplicate.

Not normalized on load: reusable blocks and Query Loop inner blocks, same as before this PR. Their frontend CSS is still filtered by the PHP change.

Screenshots

Not applicable, no visual changes.


Test instructions

Automated, all green:

  • npm run test:unit: 378 passed, including new coverage for the sanitizer, stored ID adoption, collisions and cleanup
  • npm run test:unit:php: 824 passed, including the new tests/test-css-utility-block-id.php
  • composer run lint, composer run phpstan, npm run lint: clean

Manual, on a local install of the built zip:

  1. Add an Advanced Heading and type my.anchor:1 test in the HTML Anchor field. The dot, colon and spaces never appear, and the saved markup uses the cleaned ID in both id and the class.
  2. Give a heading a stored ID old.id:x through the code editor and reload the visual editor. The ID heals to oldidx with no block validation error, and the frontend uses it after saving.
  3. Open a post that still carries a legacy invalid ID together with custom styling in Safari. No selector syntax warnings, and the Otter <style> block contains only valid selectors.
  4. Anchors with underscores and accents (sección_1) keep working: the styling applies, page#sección_1 jumps to the heading, and a Form block keeps its submissions linked after re-saving.
  5. Popup with an invalid ID: deselect and reselect it several times, the ID stays stable and the popup still opens and closes.

Checklist before the final review

  • Included E2E or unit tests for the changes in this PR.
  • Visual elements are not affected by independent changes.
  • It is at least compatible with the minimum WordPress version.
  • It loads additional script in frontend only if it is required.
  • Does not impact the Core Web Vitals.
  • In case of deprecation, old blocks are safely migrated.
  • It is usable in Widgets and FSE.
  • Copy/Paste is working if the attributes are modified.
  • PR is following the best practices

Strip characters that break HTML ids and CSS selectors from block
container ids: in the anchor input, in the shared id helper when a
stored id is adopted, and in the PHP CSS generator so old posts stop
emitting invalid rules without a re-save. Underscores and non-ASCII
characters are kept because they are valid in ids and selectors.

Fixes Codeinwp#3085
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.

Sanitize Otter Advanced Heading block dynamically generated container IDs to alphanumeric characters and hyphens

1 participant