Skip to content

Commit 850ba28

Browse files
committed
Use selectors and matching in HTML API
1 parent 2637b72 commit 850ba28

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function () {
4545
$html = $request->get_json_params()['html'] ?: '';
4646
$options = array(
4747
'context_html' => $request->get_json_params()['contextHTML'] ?: null,
48+
'selector' => $request->get_json_params()['selector'] ?: null,
4849
);
4950
return prepare_html_result_object( $html, $options );
5051
},

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ function get_normalized_html( string $html, array $options ): ?string {
6767
* @param array $options The options.
6868
*/
6969
function get_tree( string $html, array $options ): array {
70+
$selector = null;
71+
if ( isset( $options['selector'] ) && class_exists( '\WP_CSS_Selector_List' ) ) {
72+
$selector = \WP_CSS_Selector_List::from_selectors( $options['selector'] );
73+
}
74+
7075
$processor_state = new ReflectionProperty( WP_HTML_Processor::class, 'state' );
7176
$processor_state->setAccessible( true );
7277

@@ -219,6 +224,8 @@ function get_tree( string $html, array $options ): array {
219224
$document_title = $processor->get_modifiable_text();
220225
}
221226

227+
$matches = $selector !== null && $selector->matches( $processor );
228+
222229
$attributes = array();
223230
$attribute_names = $processor->get_attribute_names_with_prefix( '' );
224231
if ( null !== $attribute_names ) {
@@ -255,6 +262,7 @@ function get_tree( string $html, array $options ): array {
255262
'_virtual' => $is_virtual(),
256263
'_depth' => $processor->get_current_depth(),
257264
'_namespace' => $namespace,
265+
'_matches' => $matches,
258266
);
259267

260268
// Self-contained tags contain their inner contents as modifiable text.

html-api-debugger/print-html-tree.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function printHtmlApiTree(node, ul, options = {}) {
2323
const li = document.createElement('li');
2424
li.className = `t${node.childNodes[i].nodeType}`;
2525

26-
if (options.selector && node.childNodes[i].matches?.(options.selector)) {
26+
if (
27+
node.childNodes[i]._matches ||
28+
(options.selector && node.childNodes[i].matches?.(options.selector))
29+
) {
2730
li.classList.add('matches-selector');
2831
}
2932

html-api-debugger/view.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ const store = createStore(NS, {
509509
body: JSON.stringify({
510510
html: store.state.html,
511511
contextHTML: store.state.contextHTMLForUse,
512+
selector: store.state.selector,
512513
}),
513514
headers: {
514515
'Content-Type': 'application/json',
@@ -693,14 +694,15 @@ const store = createStore(NS, {
693694
},
694695

695696
/** @param {InputEvent} e */
696-
handleSelectorChange(e) {
697+
handleSelectorChange: function* (e) {
697698
const val = /** @type {HTMLInputElement} */ (e.target).value.trim() || null;
698699
if (val) {
699700
try {
700701
// Test whether the selector is valid before setting it so it isn't applied.
701702
document.createDocumentFragment().querySelector(val);
702703
store.state.selector = val;
703704
store.state.selectorErrorMessage = null;
705+
yield store.callAPI();
704706
return;
705707
} catch (/** @type {unknown} */ e) {
706708
if (e instanceof DOMException && e.name === 'SyntaxError') {
@@ -732,6 +734,7 @@ const store = createStore(NS, {
732734
}
733735
}
734736
store.state.selector = null;
737+
yield store.callAPI();
735738
},
736739
});
737740

0 commit comments

Comments
 (0)