Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5474,6 +5474,17 @@ public function get_attribute_names_with_prefix( $prefix ): ?array {
/**
* Adds a new class name to the currently matched tag.
*
* Whitespace in `$class_name` is preserved verbatim. This may result
* in multiple class names being added to the element's class list.
*
* Examples:
*
* $p->add_class( 'wp-block' );
* // Adds one class: "wp-block".
*
* $p->add_class( 'wp-block alignwide' );
* // Adds two classes: "wp-block" and "alignwide".
*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we started calling _doing_it_wrong() here and tried to push code away, so that some day we can reject multiple class name updates?

* @since 6.6.0 Subclassed for the HTML Processor.
*
* @param string $class_name The class name to add.
Expand All @@ -5487,6 +5498,7 @@ public function add_class( $class_name ): bool {
* Removes a class name from the currently matched tag.
*
* @since 6.6.0 Subclassed for the HTML Processor.
* @since 7.1.0 Returns false when `$class_name` contains ASCII whitespace.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this new in 7.1.0? I don’t see any change in behavior that would make it start returning false now

*
* @param string $class_name The class name to remove.
* @return bool Whether the class was set to be removed.
Expand Down
30 changes: 30 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,15 @@ public function has_class( $wanted_class ): ?bool {
return null;
}

if ( false !== strpbrk( $wanted_class, " \t\f\r\n" ) ) {
_doing_it_wrong(
__METHOD__,
__( 'A class name cannot contain ASCII whitespace.' ),
'7.1.0'
);
return false;
}

$case_insensitive = self::QUIRKS_MODE === $this->compat_mode;

$wanted_length = strlen( $wanted_class );
Expand Down Expand Up @@ -4537,6 +4546,17 @@ public function remove_attribute( $name ): bool {
/**
* Adds a new class name to the currently matched tag.
*
* Whitespace in `$class_name` is preserved verbatim. This may result
* in multiple class names being added to the element's class list.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another thought is that maybe we should not preserve this, and especially not declare that we are preserving it. a problem here is adding duplicate class names.

I’d really like the interface to be raw PHP strings that aren’t decoded, added as a class name with appropriate encoding.

we can trivially add add_classes( ...$class_names ) or even extend add_class() to support varargs.

as a stepping stone, maybe we break on appropriate whitespace just to we can deduplicate tags.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good opportunity to use my PHP extension in normal cases to explore what happens if someone sends a value like add_class( '—experimental' ) and then flag it. that should be sent as —experimental otherwise it would/should be encoded as &amp\; mdash\; experimental or something like that

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I saw something adding multiple class names this way, which prompted my investigation. I'd have to dig around.

*
* Examples:
*
* $p->add_class( 'wp-block' );
* // Adds one class: "wp-block".
*
* $p->add_class( 'wp-block alignwide' );
* // Adds two classes: "wp-block" and "alignwide".
*
* @since 6.2.0
*
* @param string $class_name The class name to add.
Expand Down Expand Up @@ -4580,6 +4600,7 @@ public function add_class( $class_name ): bool {
* Removes a class name from the currently matched tag.
*
* @since 6.2.0
* @since 7.1.0 Returns false when `$class_name` contains ASCII whitespace.
*
* @param string $class_name The class name to remove.
* @return bool Whether the class was set to be removed.
Expand All @@ -4592,6 +4613,15 @@ public function remove_class( $class_name ): bool {
return false;
}

if ( false !== strpbrk( $class_name, " \t\f\r\n" ) ) {
_doing_it_wrong(
__METHOD__,
__( 'A class name cannot contain ASCII whitespace.' ),
'7.1.0'
);
return false;
}

if ( self::QUIRKS_MODE !== $this->compat_mode ) {
$this->classname_updates[ $class_name ] = self::REMOVE_CLASS;
return true;
Expand Down
Loading