-
Notifications
You must be signed in to change notification settings - Fork 3.2k
HTML API: Use Tag Processor when adding rel keywords to A elements.
#9252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dmsnell
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
dmsnell:html-api/refactor-wp-rel-nofollow
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−32
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| class WP_HTML_Attribute { | ||
| /** | ||
| * Parses and returns an unordered set of space-separated tokens. | ||
| * | ||
| * Tokens in the returned array appear in the same order as they are uniquely | ||
| * found in the given attribute value string. When case-insensitive, output | ||
| * tokens will all be ASCII lowercase. | ||
| * | ||
| * Example: | ||
| * | ||
| * array( 'a', 'b', 'c' ) === WP_HTML_Attribute::from_unordered_set_of_space_separated_tokens( "a b a\t\nc" ); | ||
| * | ||
| * > A set of space-separated tokens is a string containing zero or more | ||
| * > words (known as tokens) separated by one or more ASCII whitespace, | ||
| * > where words consist of any string of one or more characters, none | ||
| * > of which are ASCII whitespace. | ||
| * | ||
| * > An unordered set of unique space-separated tokens is a set of | ||
| * > space-separated tokens where none of the tokens are duplicated. | ||
| * | ||
| * > How tokens in a set of space-separated tokens are to be compared | ||
| * > (e.g. case-sensitively or not) is defined on a per-set basis. | ||
| * | ||
| * @see https://html.spec.whatwg.org/#unordered-set-of-unique-space-separated-tokens | ||
| * | ||
| * @since {WP_VERSION} | ||
| * | ||
| * @param string $attribute_value HTML-decoded attribute value to parse. | ||
| * @param string $case_sensitivity Optional. Constrain uniqueness with 'case-sensitive' | ||
| * or 'case-insensitive'. Default 'case-sensitive'. | ||
| * @return string[] Set of unique tokens parsed from attribute value. | ||
| */ | ||
| public static function from_unordered_set_of_space_separated_tokens( $attribute_value, $case_sensitivity = 'case-sensitive' ) { | ||
| if ( empty( $attribute_value ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| if ( 'case-insensitive' === $case_sensitivity ) { | ||
| $attribute_value = strtolower( $attribute_value ); | ||
| } | ||
|
|
||
| $tokens = array(); | ||
| $uniques = ' '; | ||
| $at = 0; | ||
| $end = strlen( $attribute_value ); | ||
| while ( $at < $end ) { | ||
| $at += strspn( $attribute_value, " \t\f\r\n", $at ); | ||
|
|
||
| $word_length = strcspn( $attribute_value, " \t\f\r\n", $at ); | ||
| $word = substr( $attribute_value, $at, $word_length ); | ||
|
|
||
| if ( 0 < $word_length && ! str_contains( $uniques, " {$word} " ) ) { | ||
| $uniques .= "{$word} "; | ||
| $tokens[] = $word; | ||
| } | ||
|
|
||
| $at += $word_length; | ||
| } | ||
|
|
||
| return $tokens; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why this is a string param and not a boolean?
If going with a string, then this could use
'case-sensitive'|'case-insensitive'as the type instead ofstring, in alignment with the proposal to adopt PHPStan. This would add static type checking for bad string values. If string values are used as well, maybe they should be added as constants to the class so that the literals aren't passed around everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are there for discoverability in the code, plus I believe that in a very inconsequential amount the checking of string equality is faster than even booleans, because PHP short-circuits casting
100% it’s there because I find boolean parameters opaque and string parameters are explicit.
happy to update to use the string values in the types, but I thought WPCS nags yapped at me in the past because I tried that and it wanted string instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, performance is not my concern here. I'm more concerned with typos, and the DX of having to type in an exact string and what happens if you get it wrong.
Alternatively, this could take an
$optionsarray which has acase_sensitivekey with a boolean value. This would seem more WordPressy and would allow for more flexibility in the future to add more options without adding additional positional params.