Skip to content

Commit 732939f

Browse files
committed
Add support for HTML normalize method
1 parent e488665 commit 732939f

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ function prepare_html_result_object( string $html, array $options = null ): arra
151151
'html' => $html,
152152
'error' => null,
153153
'result' => null,
154+
'normalizedHtml' => HTML_API_Integration\get_normalized_html( $html ),
154155
);
155156

156157
try {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,24 @@ function get_supports(): array {
2020
'full_parser' => method_exists( WP_HTML_Processor::class, 'create_full_parser' ),
2121
'quirks_mode' => $html_processor_state_rc->hasProperty( 'document_mode' ),
2222
'doctype' => method_exists( WP_HTML_Processor::class, 'get_doctype_info' ),
23+
'normalize' => method_exists( WP_HTML_Processor::class, 'normalize' ),
2324
);
2425
}
2526

27+
28+
/**
29+
* Get the normalized HTML.
30+
*
31+
* @param string $html The HTML.
32+
* @return string|null The normalized HTML or null if not supported.
33+
*/
34+
function get_normalized_html( string $html ): ?string {
35+
if ( ! method_exists( WP_HTML_Processor::class, 'normalize' ) ) {
36+
return null;
37+
}
38+
return WP_HTML_Processor::normalize( $html );
39+
}
40+
2641
/**
2742
* Build a DOM-like tree using the HTML API
2843
*

html-api-debugger/interactivity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function generate_page( string $html, array $options ): string {
6868
'htmlApiDoctypeName' => $htmlapi_response['result']['doctypeName'] ?? '[unknown]',
6969
'htmlApiDoctypePublicId' => $htmlapi_response['result']['doctypePublicId'] ?? '[unknown]',
7070
'htmlApiDoctypeSytemId' => $htmlapi_response['result']['doctypeSystemId'] ?? '[unknown]',
71+
'normalizedHtml' => $htmlapi_response['normalizedHtml'] ?? '',
7172
)
7273
);
7374
ob_start();
@@ -104,6 +105,10 @@ class="html-api-debugger-container html-api-debugger--grid"
104105
referrerpolicy="no-referrer"
105106
sandbox="allow-forms allow-modals allow-popups allow-scripts allow-same-origin"></iframe>
106107
</div>
108+
<details class="full-width" data-wp-bind--hidden="!state.htmlapiResponse.supports.normalize">
109+
<summary>HTML API Normalized HTML</summary>
110+
<pre class="html-text" data-wp-text="state.normalizedHtml"></pre>
111+
</details>
107112
<details class="full-width">
108113
<summary>Document info</summary>
109114

html-api-debugger/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ Add a page to wp-admin for debugging the HTML API.
7979
* Replace TABLE based layout with CSS grid.
8080
* Fix a crash on meta tags with a content attribute.
8181
* Display namespace on tag closers.
82+
* Display normalized HTML when supported.

html-api-debugger/view.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ let debounceInputAbortController = null;
3535
* @property {boolean} is_virtual
3636
* @property {boolean} quirks_mode
3737
* @property {boolean} full_parser
38+
* @property {boolean} normalize
3839
*
3940
*
4041
* @typedef HtmlApiResponse
4142
* @property {any} error
4243
* @property {Supports} supports
4344
* @property {{tree: any, compatMode:string, doctypeName:string, doctypePublicId:string, doctypeSystemId:string }|null} result
45+
* @property {string|null} normalizedHtml
4446
* @property {string} html
4547
*
4648
*
4749
* @typedef State
4850
* @property {string|null} htmlApiDoctypeName
4951
* @property {string|null} htmlApiDoctypePublicId
5052
* @property {string|null} htmlApiDoctypeSystemId
53+
* @property {string|null} normalizedHtml
5154
* @property {string} htmlPreambleForProcessing
5255
* @property {string} formattedHtmlapiResponse
5356
* @property {HtmlApiResponse} htmlapiResponse
@@ -126,6 +129,18 @@ const store = createStore(NS, {
126129
: store.state.htmlapiResponse.result?.doctypeSystemId;
127130
},
128131

132+
get normalizedHtml() {
133+
if (
134+
!store.state.htmlapiResponse.supports.normalize ||
135+
!store.state.htmlapiResponse.normalizedHtml
136+
) {
137+
return '';
138+
}
139+
return store.state.showInvisible
140+
? replaceInvisible(store.state.htmlapiResponse.normalizedHtml)
141+
: store.state.htmlapiResponse.normalizedHtml;
142+
},
143+
129144
get formattedHtmlapiResponse() {
130145
return JSON.stringify(store.state.htmlapiResponse, undefined, 2);
131146
},

0 commit comments

Comments
 (0)