Skip to content

Commit 5f8ea49

Browse files
swissspidyclaude
andcommitted
Only match block styles in HTML class attributes
The innerHTML fallback matched the style class anywhere in the markup, so `<p>Use is-style-rounded here.</p>` counted as a rounded paragraph. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent f7ac0ee commit 5f8ea49

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎features/block-search.feature‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,26 @@ Feature: Search posts by block usage
108108
Scenario: Style search ignores plain text false positives
109109
When I run `wp post create --post_type=post --post_title='Style Token Plain Text Post' --post_status=publish --post_content='This post mentions is-style-rounded in plain text only.' --porcelain`
110110
Then STDOUT should be a number
111-
And save STDOUT as {STYLE_TOKEN_TEXT_POST_ID}
111+
112+
Given a style-token-block-text-post.html file:
113+
"""
114+
<!-- wp:paragraph --><p>Use is-style-rounded here.</p><!-- /wp:paragraph -->
115+
"""
116+
When I run `wp post create style-token-block-text-post.html --post_type=post --post_title='Style Token Block Text Post' --post_status=publish --porcelain`
117+
Then STDOUT should be a number
118+
119+
Given a style-class-only-post.html file:
120+
"""
121+
<!-- wp:paragraph --><p class="is-style-rounded">Styled</p><!-- /wp:paragraph -->
122+
"""
123+
When I run `wp post create style-class-only-post.html --post_type=post --post_title='Style Class Only Post' --post_status=publish --porcelain`
124+
Then STDOUT should be a number
125+
And save STDOUT as {STYLE_CLASS_ONLY_POST_ID}
112126

113127
When I run `wp block search --style=rounded --field=ID --format=ids`
114-
Then STDOUT should not contain:
128+
Then STDOUT should be:
115129
"""
116-
{STYLE_TOKEN_TEXT_POST_ID}
130+
{STYLE_CLASS_ONLY_POST_ID}
117131
"""
118132

119133
Scenario: Search block usage count

‎src/Block_Search_Command.php‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,23 @@ private function matches_style_filter( array $block, $style_name ) {
926926
return true;
927927
}
928928

929-
return ! empty( $block['innerHTML'] ) && 1 === preg_match(
930-
"/(^|[\\s\"'])" . preg_quote( $needle, '/' ) . "([\\s\"']|$)/",
931-
$block['innerHTML']
932-
);
929+
if ( empty( $block['innerHTML'] ) || ! is_string( $block['innerHTML'] ) ) {
930+
return false;
931+
}
932+
933+
// Only class attributes count, not text that happens to mention the class.
934+
if ( ! preg_match_all( '/\sclass\s*=\s*(?:"([^"]*)"|\'([^\']*)\')/i', $block['innerHTML'], $class_attributes, PREG_SET_ORDER ) ) {
935+
return false;
936+
}
937+
938+
foreach ( $class_attributes as $class_attribute ) {
939+
$classes = $class_attribute[1] . ( isset( $class_attribute[2] ) ? $class_attribute[2] : '' );
940+
941+
if ( 1 === preg_match( '/(^|\s)' . preg_quote( $needle, '/' ) . '(\s|$)/', $classes ) ) {
942+
return true;
943+
}
944+
}
945+
946+
return false;
933947
}
934948
}

0 commit comments

Comments
 (0)