Repository navigation
Conversation
The IMG background path found an existing wrapper position with a
regular expression and built both style attributes by concatenating
text. The regular expression matched "position:" inside a string or a
comment and missed a declaration written in uppercase or after a
comment:
color:red;/* note */position:absolute
=> color:red;/* note */position:absolute;position:relative;
The IMG object-position was the block's backgroundPosition attribute
escaped for HTML only, so a value with a top-level semicolon added
declarations to the IMG style.
Use WP_HTML_Style_Attribute_Processor for both. The wrapper gets
position: relative unless next_declaration( 'position' ) finds a
declaration. The IMG style appends object-fit and object-position with
append_declaration(), which rejects a value that is not one valid
declaration value; a rejected position is left out of the style and
of data-object-position.
The processor writes appended declarations as "name: value;", so the
existing assertions look for "position: relative".
Cover wrapper styles with "position" inside a string or a comment, in uppercase, and after a comment, and IMG object-position values that are not one CSS declaration value.
…kground-image-element
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.
Caution
This is a work in progress draft and is not ready for any serious review.
WordPress#11050 (https://core.trac.wordpress.org/ticket/64725) renders media library background images as an
<img>inside the block wrapper. It reads and writes two style attributes as text: it looks for an existing wrapperpositionwithpreg_match( '/(?:^|;)\s*position\s*:/' ), and it builds the IMGstyleby concatenatingobject-fitandobject-positionvalues that are escaped for HTML only. This PR replaces both withWP_HTML_Style_Attribute_Processor.The base branch is WordPress#14782 merged with WordPress#11050 (head
6dc1e7f0c8), so this diff shows only the change to that PR.Wrapper position
The wrapper gets
position: relativeunlessnext_declaration( 'position' )finds a declaration. The processor matches names ASCII-case-insensitively and ignores text inside strings and comments.stylecolor:red;position:absolutecolor:red;position:absolute;color:red;position:absolutecolor:red;/* note */position:absolutecolor:red;/* note */position:absolute;position:relative;color:red;/* note */position:absolutePOSITION:absolutePOSITION:absolute;position:relative;POSITION:absolutefont-family:"a;position:b"font-family:"a;position:b";font-family:"a;position:b"; position: relative;color:red;/*;position:absolute*/color:red;/*;position:absolute*/;color:red;/*;position:absolute*/ position: relative;IMG style
The fixed IMG declarations stay as a literal.
object-fitandobject-positionare added withappend_declaration(), which accepts a value only when it is one valid declaration value: no top-level;or!important, no unclosed block, string or function. A rejectedbackgroundPositionis left out of the style and ofdata-object-position; the image then uses the defaultobject-position,50% 50%.object-fitis alwayscoverorcontain, because the IMG path is only taken for those sizes.This checks syntax only. It does not check that the value is a CSS
<position>, and it does not runsafecss_filter_attr(), which the style engine applies on the CSSbackground-imagepath. A browser ignores anobject-positiondeclaration whose value has the wrong type.backgroundPositionright 10% bottom 20pxobject-fit:cover;object-position:right 10% bottom 20px;object-fit: cover; object-position: right 10% bottom 20px;;object-fit:cover;object-position:followed by the whole value, which adds declarationsobject-fit: cover;calc(50% + 1pxobject-fit:cover;object-position:calc(50% + 1px;object-fit: cover;The processor writes appended declarations as
name: value;. The two existing tests from WordPress#11050 look forposition: relativeinstead ofposition:relative; that is their only change, and they pass. The new tests fail on the base branch in 12 cases: the 7 wrapper cases and 3 IMG cases above, which differ in format in each case and in behavior in 6, and the two existing tests, which differ in format.Overlap with WordPress#14785
That PR changes the CSS
background-imagepath of the same function,wp_render_background_support(), to append each style engine declaration to the wrapper withappend_declaration(). This PR uses the same pattern on the IMG path. The two diffs touch different lines.Known limitations
background.gradientandbackground.backgroundClipstyles. This PR does not change that.>of the block content.Depends on WordPress#14782 and WordPress#11050.
Trac ticket: TBD
Use of AI Tools
AI assistance: Yes
Tools: Claude Code
Models: Claude Opus 5.5
Used for: implementation, tests and this description.
This pull request is for code review only. Keep other discussion in the Trac ticket. Do not merge. See GitHub Pull Requests for Code Review in the Core Handbook.