Repository navigation
Sanitize block container IDs used in selectors and anchors - #3180
Open
faisalahammad wants to merge 1 commit into
Open
faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3085.
Summary
The HTML Anchor field takes any text, and stored block IDs are used as-is, so IDs like
my.anchor:1end 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: newsanitizeBlockId()helper, applied to generated IDs and to stored IDs adopted byaddBlockId. 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_selectorin 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 FormoptionNamekeys 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 cleanupnpm run test:unit:php: 824 passed, including the newtests/test-css-utility-block-id.phpcomposer run lint,composer run phpstan,npm run lint: cleanManual, on a local install of the built zip:
my.anchor:1 testin the HTML Anchor field. The dot, colon and spaces never appear, and the saved markup uses the cleaned ID in bothidand the class.old.id:xthrough the code editor and reload the visual editor. The ID heals tooldidxwith no block validation error, and the frontend uses it after saving.<style>block contains only valid selectors.sección_1) keep working: the styling applies,page#sección_1jumps to the heading, and a Form block keeps its submissions linked after re-saving.Checklist before the final review