Skip to content

Commit 469ea3e

Browse files
committed
Warn on unsupported selector
1 parent a5b2768 commit 469ea3e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

html-api-debugger/html-api-integration.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,23 @@ function get_normalized_html( string $html, array $options ): ?string {
7070
* @param array $options The options.
7171
*/
7272
function get_tree( string $html, array $options ): array {
73+
/**
74+
* Messages generated during parse.
75+
*
76+
* @var string[]
77+
*/
78+
$warnings = array();
7379
$selector = null;
74-
if ( isset( $options['selector'] ) && class_exists( '\WP_CSS_Selector' ) ) {
75-
$selector = \WP_CSS_Selector::from_selectors( $options['selector'] );
80+
if ( isset( $options['selector'] ) && class_exists( '\WP_CSS_Complex_Selector_List' ) ) {
81+
$selector = \WP_CSS_Complex_Selector_List::from_selectors( $options['selector'] );
82+
if ( null === $selector ) {
83+
$warnings[] = 'The provided selector is invalid or unsupported.';
84+
}
85+
} elseif ( isset( $options['selector'] ) && class_exists( '\WP_CSS_Compound_Selector_List' ) ) {
86+
$selector = \WP_CSS_Compound_Selector_List::from_selectors( $options['selector'] );
87+
if ( null === $selector ) {
88+
$warnings[] = 'The provided selector is invalid or unsupported.';
89+
}
7690
}
7791

7892
$processor_state = new ReflectionProperty( WP_HTML_Processor::class, 'state' );
@@ -448,6 +462,7 @@ function get_tree( string $html, array $options ): array {
448462
'doctypeSystemId' => $doctype_system_identifier,
449463

450464
'contextNode' => $context_node,
465+
'warnings' => $warnings,
451466
);
452467
}
453468

html-api-debugger/interactivity.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ class="context-html"
152152
<span><label>Show virtual <input type="checkbox" data-wp-bind--checked="state.showVirtual" data-wp-on-async--input="handleShowVirtualClick"></label></span>
153153
<div>
154154
<label>CSS Selectors <textarea placeholder="CSS selector: .my-class" data-wp-on-async--input="handleSelectorChange"></textarea></label>
155-
<p data-wp-bind--hidden="!state.selectorErrorMessage" data-wp-text="state.selectorErrorMessage" class="error-holder"></p>
156155
</div>
157156
</div>
158157
<div>
@@ -166,6 +165,13 @@ class="context-html"
166165
</div>
167166
</div>
168167

168+
<div data-wp-bind--hidden="!state.htmlapiResponse.result.warnings.0">
169+
<template data-wp-each="state.htmlapiResponse.result.warnings">
170+
<p data-wp-text="context.item" class="error-holder"></p>
171+
</template>
172+
</div>
173+
<p data-wp-bind--hidden="!state.selectorErrorMessage" data-wp-text="state.selectorErrorMessage" class="error-holder"></p>
174+
169175
<div>
170176
<h2>Processed HTML</h2>
171177
<div data-wp-bind--hidden="!state.htmlapiResponse.result.playback">

0 commit comments

Comments
 (0)