Skip to content

Commit cfa1dd3

Browse files
committed
Display document title
1 parent 5340028 commit cfa1dd3

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function get_tree( string $html, array $options ): array {
7676
$is_fragment_processor = false;
7777

7878
$compat_mode = 'BackCompat';
79+
$document_title = null;
7980
$doctype_name = null;
8081
$doctype_public_identifier = null;
8182
$doctype_system_identifier = null;
@@ -99,10 +100,19 @@ function get_tree( string $html, array $options ): array {
99100
break;
100101

101102
case '#tag':
103+
if ( $document_title === null && $context_processor->get_tag() === 'TITLE' && $context_processor->get_namespace() === 'html' ) {
104+
$document_title = $context_processor->get_modifiable_text();
105+
}
106+
102107
$context_processor->set_bookmark( 'final_node' );
103108
break;
104109
}
105110
}
111+
112+
if ( $document_title === null ) {
113+
$document_title = '';
114+
}
115+
106116
if ( $context_processor->has_bookmark( 'final_node' ) ) {
107117
$context_processor->seek( 'final_node' );
108118
/**
@@ -205,6 +215,10 @@ function get_tree( string $html, array $options ): array {
205215
case '#tag':
206216
$tag_name = $processor->get_qualified_tag_name();
207217

218+
if ( $document_title === null && $tag_name === 'TITLE' && $processor->get_namespace() === 'html' ) {
219+
$document_title = $processor->get_modifiable_text();
220+
}
221+
208222
$attributes = array();
209223
$attribute_names = $processor->get_attribute_names_with_prefix( '' );
210224
if ( null !== $attribute_names ) {
@@ -399,13 +413,24 @@ function get_tree( string $html, array $options ): array {
399413
throw new Exception( 'Paused at incomplete token.' );
400414
}
401415

416+
/*
417+
* Strip and collapse ASCII whitespace.
418+
*
419+
* https://html.spec.whatwg.org/multipage/dom.html#document.title
420+
* https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
421+
*/
422+
$document_title = trim( preg_replace( "/[\t\n\f\r ]+/", ' ', $document_title ), "\t\n\f\r " );
423+
402424
return array(
403425
'tree' => $tree,
404426
'playback' => $playback,
405427
'compatMode' => $compat_mode,
428+
'documentTitle' => $document_title,
429+
406430
'doctypeName' => $doctype_name,
407431
'doctypePublicId' => $doctype_public_identifier,
408432
'doctypeSystemId' => $doctype_system_identifier,
433+
409434
'contextNode' => $context_node,
410435
);
411436
}

html-api-debugger/interactivity.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function generate_page( string $html, array $options ): string {
2424
array(
2525
'DOM' => array(
2626
'renderingMode' => '',
27+
'documentTitle' => '',
2728
'doctypeName' => '',
2829
'doctypeSystemId' => '',
2930
'doctypePublicId' => '',
@@ -42,9 +43,10 @@ function generate_page( string $html, array $options ): string {
4243
'hoverInsertion' => false,
4344
'checkingForPRPlaygroundLink' => false,
4445

45-
'htmlApiDoctypeName' => $htmlapi_response['result']['doctypeName'] ?? '[unknown]',
46-
'htmlApiDoctypePublicId' => $htmlapi_response['result']['doctypePublicId'] ?? '[unknown]',
47-
'htmlApiDoctypeSytemId' => $htmlapi_response['result']['doctypeSystemId'] ?? '[unknown]',
46+
'htmlApiDocumentTitle' => $htmlapi_response['result']['documentTitle'] ?? null,
47+
'htmlApiDoctypeName' => $htmlapi_response['result']['doctypeName'] ?? null,
48+
'htmlApiDoctypePublicId' => $htmlapi_response['result']['doctypePublicId'] ?? null,
49+
'htmlApiDoctypeSytemId' => $htmlapi_response['result']['doctypeSystemId'] ?? null,
4850
'normalizedHtml' => $htmlapi_response['normalizedHtml'] ?? '',
4951

5052
'playbackLength' => isset( $htmlapi_response['result']['playback'] )
@@ -90,13 +92,15 @@ class="html-api-debugger-container html-api-debugger--grid"
9092
<div class="html-api-debugger--grid">
9193
<div>
9294
Rendering mode:&nbsp;<code data-wp-text="state.htmlapiResponse.result.compatMode"></code><br>
95+
Document title:&nbsp;<code data-wp-text="state.htmlApiDocumentTitle"></code><br>
9396
Doctype name:&nbsp;<code data-wp-text="state.htmlApiDoctypeName"></code><br>
9497
Doctype publicId:&nbsp;<code data-wp-text="state.htmlApiDoctypePublicId"></code><br>
9598
Doctype systemId:&nbsp;<code data-wp-text="state.htmlApiDoctypeSystemId"></code><br>
9699
Context node:&nbsp;<code data-wp-text="state.htmlapiResponse.result.contextNode"></code>
97100
</div>
98101
<div>
99102
Rendering mode:&nbsp;<code data-wp-text="state.DOM.renderingMode"></code><br>
103+
Document title:&nbsp;<code data-wp-text="state.DOM.documentTitle"></code><br>
100104
Doctype name:&nbsp;<code data-wp-text="state.DOM.doctypeName"></code><br>
101105
Doctype publicId:&nbsp;<code data-wp-text="state.DOM.doctypePublicId"></code><br>
102106
Doctype systemId:&nbsp;<code data-wp-text="state.DOM.doctypeSystemId"></code><br>

html-api-debugger/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Please file issues and pull requests on the [GitHub repository](https://github.c
1616
== Changelog ==
1717

1818
= 2.4 =
19+
* Display document title.
1920

2021
= 2.3 =
2122
* Inline tree copy buttons.

html-api-debugger/view.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let mutationObserver = null;
3535
*
3636
* @typedef DOM
3737
* @property {string|undefined} renderingMode
38+
* @property {string|undefined} documentTitle
3839
* @property {string|undefined} doctypeName
3940
* @property {string|undefined} doctypeSystemId
4041
* @property {string|undefined} doctypePublicId
@@ -53,7 +54,7 @@ let mutationObserver = null;
5354
* @typedef HtmlApiResponse
5455
* @property {any} error
5556
* @property {Supports} supports
56-
* @property {{tree: any, compatMode:string, doctypeName:string, doctypePublicId:string, doctypeSystemId:string, playback: ReadonlyArray<[string,any]> }|null} result
57+
* @property {{tree: any, compatMode: string, documentTitle: string|null, doctypeName: string|null, doctypePublicId: string|null, doctypeSystemId: string|null, playback: ReadonlyArray<[string,any]> }|null} result
5758
* @property {string|null} normalizedHtml
5859
* @property {string} html
5960
*
@@ -74,6 +75,7 @@ let mutationObserver = null;
7475
* @property {string|undefined} playbackHTML
7576
* @property {number|null} playbackPoint
7677
* @property {number|null} playbackLength
78+
* @property {string|null} htmlApiDocumentTitle
7779
* @property {string|null} htmlApiDoctypeName
7880
* @property {string|null} htmlApiDoctypePublicId
7981
* @property {string|null} htmlApiDoctypeSystemId
@@ -194,12 +196,20 @@ const store = createStore(NS, {
194196
localStorage.getItem(`${NS}-hoverInfo`)
195197
),
196198

199+
get htmlApiDocumentTitle() {
200+
return store.state.showInvisible
201+
? store.state.htmlapiResponse.result?.documentTitle &&
202+
replaceInvisible(store.state.htmlapiResponse.result.documentTitle)
203+
: store.state.htmlapiResponse.result?.documentTitle;
204+
},
205+
197206
get htmlApiDoctypeName() {
198207
return store.state.showInvisible
199208
? store.state.htmlapiResponse.result?.doctypeName &&
200209
replaceInvisible(store.state.htmlapiResponse.result.doctypeName)
201210
: store.state.htmlapiResponse.result?.doctypeName;
202211
},
212+
203213
get htmlApiDoctypePublicId() {
204214
return store.state.showInvisible
205215
? store.state.htmlapiResponse.result?.doctypePublicId &&
@@ -377,6 +387,7 @@ const store = createStore(NS, {
377387
// @ts-expect-error It better be defined!
378388
const doc = RENDERED_IFRAME.contentWindow.document;
379389

390+
store.state.DOM.documentTitle = doc.title;
380391
store.state.DOM.renderingMode = doc.compatMode;
381392
store.state.DOM.doctypeName = doc.doctype?.name;
382393
store.state.DOM.doctypeSystemId = doc.doctype?.systemId;

0 commit comments

Comments
 (0)