Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a491bef
CSS API: Add CSS token processor foundations
sirreal Jul 23, 2026
a80bd98
Harden CSS builder identifier serialization
sirreal Aug 26, 2026
db896db
Harden CSS token value updates
sirreal Aug 26, 2026
b2e6400
Point CSS API tests at Trac #65738 and set @since 7.2.0
sirreal Oct 6, 2026
6804231
Move string and ident serialization onto the token processor
sirreal Oct 7, 2026
c64ca47
Drop the token mutation API
sirreal Oct 7, 2026
59eece4
Drop untested token value getters
sirreal Oct 7, 2026
52576f1
Document corpus provenance
sirreal Oct 7, 2026
a9d4b5d
Report whether a token ended at its delimiter
sirreal Oct 7, 2026
051e8f5
Accept NUL in an unquoted url
sirreal Oct 9, 2026
27f45f1
Scan plain ASCII runs in idents and urls with strspn
sirreal Oct 9, 2026
dabd8c5
Decode token text with a static method
sirreal Oct 9, 2026
2dcba33
Add WP_CSS_Token and WP_CSS_Token_Processor::get_token()
sirreal Oct 9, 2026
892cd25
Move string and ident serialization into WP_CSS_Token_Serializer
sirreal Oct 9, 2026
8dbd398
Serialize tokens following CSS Syntax 3 §9
sirreal Oct 9, 2026
3458860
Document why comment tokens are kept
sirreal Oct 9, 2026
47206d9
Escape CR and FF in strings instead of normalizing them
sirreal Oct 9, 2026
2626801
Do not spell CDO or CDC from other tokens when serializing
sirreal Oct 9, 2026
08f3485
Make the token serializer internal
sirreal Oct 9, 2026
c177694
Build identifier escaping on name escaping
sirreal Oct 9, 2026
06c6e5d
Fill serializer test gaps
sirreal Oct 9, 2026
74f3310
Escape control characters in strings
sirreal Oct 13, 2026
52216bc
Write hex escapes in lowercase
sirreal Oct 13, 2026
5d86e10
Escape code points outside the non-ASCII ident code points
sirreal Oct 13, 2026
7900018
Merge remote-tracking branch 'upstream/trunk' into css-api/css-token-…
sirreal Oct 23, 2026
11e4165
Merge remote-tracking branch 'upstream/trunk' into css-api/css-token-…
sirreal Oct 27, 2026
c29e0c9
Set CSS API @since to 7.3.0
sirreal Oct 27, 2026
b34301b
Revise the CSS API inline documentation
sirreal Oct 27, 2026
dcf3b56
Give comment tokens their text as a value
sirreal Oct 27, 2026
394a9f1
Match the is_valid_escape() comments to its code
sirreal Oct 27, 2026
e270ce2
Do not decode escapes in comment tokens
sirreal Oct 27, 2026
f236c54
Remove the tokenizer cursor getters that WP_CSS_Token duplicates
sirreal Oct 27, 2026
ec1b005
Add WP_CSS_Builder to write PHP values as CSS text
sirreal Oct 27, 2026
f0020b5
Mark private CSS API declarations @ignore
sirreal Oct 27, 2026
5742ca9
Document token values on WP_CSS_Token instead of the removed getters
sirreal Oct 27, 2026
f3e7bff
Remove the encoding parameter from WP_CSS_Token_Processor::create()
sirreal Oct 28, 2026
52c773c
Merge remote-tracking branch 'upstream/trunk' into css-api/css-token-…
sirreal Oct 29, 2026
722e34c
Construct WP_CSS_Token_Processor with a public constructor
sirreal Oct 29, 2026
6e82fe8
Remove an always-true null check in consume_ident_start_codepoint()
sirreal Oct 29, 2026
567c5d0
Mark WP_CSS_Token_Processor::next_token() impure for PHPStan
sirreal Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/wp-includes/css-api/class-wp-css-builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* CSS API: WP_CSS_Builder class
*
* @package WordPress
* @subpackage CSS-API
* @since 7.3.0
*/

/**
* Writes PHP values as CSS text.
*
* Each method returns CSS text that reads back as the given value: a string
* token, a `url()` function holding a string token, or an identifier. Combine
* the results with literal CSS to write a value with parts that come from
* input.
*
* Example:
*
* ```php interactive
* $image_url = 'https://example.com/a (1) "b".jpg';
* echo 'background-image: ' . WP_CSS_Builder::url( $image_url );
* // Outputs: background-image: url("https://example.com/a (1) \22 b\22 .jpg")
* ```
*
* The characters escaped beyond those CSS requires may change; the value
* the text reads back as does not.
*
* @since 7.3.0
*
* @see WP_CSS_Token_Serializer
*/
class WP_CSS_Builder {
/**
* Writes a value as a quoted CSS string.
*
* Quotes, `\` and control characters are hex-escaped, as are `< > & , ;
* { }`, which may be problematic in HTML or for rudimentary CSS
* processors. CSS cannot represent NULL, so it reads back as U+FFFD.
* Invalid UTF-8 reads back as U+FFFD.
*
* Example:
*
* ```php interactive
* echo 'content: ' . WP_CSS_Builder::string( 'Say "hi"' );
* // Outputs: content: "Say \22 hi\22 "
* ```
*
* @since 7.3.0
*
* @param string $value Value the string should read back as.
* @return string Quoted CSS string.
*/
public static function string( string $value ): string {
return WP_CSS_Token_Serializer::escape_string( $value );
}

/**
* Writes a URL as a CSS `url()` function holding a quoted string.
*
* The URL is written as string() writes it. It is not validated or
* sanitized; pass it through esc_url() or sanitize_url() first when it
* comes from input.
*
* Example:
*
* ```php interactive
* echo WP_CSS_Builder::url( 'https://example.com/a(1).png' );
* // Outputs: url("https://example.com/a(1).png")
* ```
*
* @since 7.3.0
*
* @link https://www.w3.org/TR/css-values-4/#urls
*
* @param string $url URL the function should read back as.
* @return string CSS `url()` function.
*/
public static function url( string $url ): string {
return 'url(' . self::string( $url ) . ')';
}

/**
* Writes a value as a CSS identifier.
*
* Code points that cannot appear in an identifier at their position are
* hex-escaped, as are the characters string() escapes. Use it for a
* property name, a custom property name in `var()`, or a keyword or
* family name written without quotes.
*
* An empty value returns an empty string, which is not an identifier.
*
* Example:
*
* ```php interactive
* echo 'font-family: ' . WP_CSS_Builder::ident( '1st Font' );
* // Outputs: font-family: \31 st\20 Font
* ```
*
* @since 7.3.0
*
* @link https://www.w3.org/TR/css-syntax-3/#ident-token-diagram
*
* @param string $value Value the identifier should read back as.
* @return string CSS identifier, or an empty string for an empty value.
*/
public static function ident( string $value ): string {
return WP_CSS_Token_Serializer::escape_ident( $value );
}
}
Loading
Loading