Skip to content

Commit 9941965

Browse files
committed
Update DOM tree when mutating document
1 parent 732939f commit 9941965

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: HTML API Debugger
44
* Plugin URI: https://github.com/sirreal/html-api-debugger
55
* Description: Add a page to wp-admin for debugging the HTML API.
6-
* Version: 1.6
6+
* Version: 1.7
77
* Requires at least: 6.5
88
* Tested up to: 6.6
99
* Author: Jon Surrell
@@ -22,7 +22,7 @@
2222
require_once __DIR__ . '/html-api-integration.php';
2323

2424
const SLUG = 'html-api-debugger';
25-
const VERSION = '1.6';
25+
const VERSION = '1.7';
2626

2727
/** Set up the plugin. */
2828
function init() {

html-api-debugger/interactivity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function generate_page( string $html, array $options ): string {
4949
'doctypeSystemId' => '',
5050
'doctypePublicId' => '',
5151
),
52+
'hasMutatedDom' => false,
5253
'html' => $html,
5354
'htmlapiResponse' => $htmlapi_response,
5455
'span' => false,
@@ -140,7 +141,7 @@ class="html-api-debugger-container html-api-debugger--grid"
140141
</div>
141142
<div>
142143
<h2>Interpreted from DOM</h2>
143-
<ul id="dom_tree" data-wp-ignore></ul>
144+
<div data-wp-class--mutated="state.hasMutatedDom"><ul id="dom_tree" data-wp-ignore></ul></div>
144145
</div>
145146
</div>
146147

html-api-debugger/readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: jonsurrell
33
Tags: HTML API, development, debug
44
Requires at least: 6.5
55
Tested up to: 6.6
6-
Stable tag: 1.6
6+
Stable tag: 1.7
77
License: GPLv2 or later
88
License URI: https://www.gnu.org/licenses/gpl-2.0.html
99

@@ -80,3 +80,6 @@ Add a page to wp-admin for debugging the HTML API.
8080
* Fix a crash on meta tags with a content attribute.
8181
* Display namespace on tag closers.
8282
* Display normalized HTML when supported.
83+
84+
= 1.7 =
85+
* Update DOM tree when HTML document is mutated.

html-api-debugger/style.css

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
pre {
2323
background-color: #fff;
24-
border: inset thin;
24+
border: inset 1px;
2525
padding: 0.2em 0.4em;
2626
}
2727

@@ -63,6 +63,21 @@
6363
word-break: break-all;
6464
text-wrap: pretty;
6565
}
66+
67+
.mutated {
68+
position: relative;
69+
70+
&::after {
71+
position: absolute;
72+
top: 0;
73+
right: 1px;
74+
padding: 0.1em 0.2em;
75+
76+
content: "DOM MODIFIED";
77+
background: red;
78+
color: white;
79+
}
80+
}
6681
}
6782

6883
#rendered_iframe {
@@ -91,7 +106,7 @@
91106

92107
#html_api_result_holder,
93108
#dom_tree {
94-
border: inset thin;
109+
border: inset 1px;
95110
padding: 0.5em 0.5em 0.5em 1em;
96111
color: black;
97112
font-family: monospace;

html-api-debugger/view.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ let inFlightRequestAbortController = null;
1818
/** @type {AbortController|null} */
1919
let debounceInputAbortController = null;
2020

21+
/** @type {MutationObserver|null} */
22+
let mutationObserver = null;
23+
2124
/**
2225
* @typedef DOM
2326
* @property {string|undefined} renderingMode
@@ -70,6 +73,7 @@ let debounceInputAbortController = null;
7073
* @property {boolean} hoverInsertion
7174
*
7275
* @property {DOM} DOM
76+
* @property {boolean} hasMutatedDom
7377
* @property {HTMLAPISpan|false} span
7478
* @property {string} hoverSpan
7579
* @property {readonly []|readonly [string,string,string]} hoverSpanSplit
@@ -93,22 +97,25 @@ let debounceInputAbortController = null;
9397
* @property {()=>void} handleCopyClick
9498
* @property {()=>void} handleCopyPrInput
9599
* @property {()=>void} handleCopyPrClick
100+
*
101+
* @property {()=>void} onRenderedIframeLoad
96102
*/
97103

98-
/** @type {typeof I.store<Store>} */
99-
const createStore = I.store;
104+
const createStore = /** @type {typeof I.store<Store>} */ (I.store);
100105

101106
/** @type {Store} */
102-
// @ts-expect-error Server provided state is not included here.
103107
const store = createStore(NS, {
108+
// @ts-expect-error Server provided state is not included here.
104109
state: {
105110
showClosers: Boolean(localStorage.getItem(`${NS}-showClosers`)),
106111
showInvisible: Boolean(localStorage.getItem(`${NS}-showInvisible`)),
107112
showVirtual: Boolean(localStorage.getItem(`${NS}-showVirtual`)),
108113
quirksMode: Boolean(localStorage.getItem(`${NS}-quirksMode`)),
109114
fullParser: Boolean(localStorage.getItem(`${NS}-fullParser`)),
110115

111-
hoverInfo: localStorage.getItem(`${NS}-hoverInfo`),
116+
hoverInfo: /** @type {typeof store.state.hoverInfo} */ (
117+
localStorage.getItem(`${NS}-hoverInfo`)
118+
),
112119

113120
get htmlApiDoctypeName() {
114121
return store.state.showInvisible
@@ -238,6 +245,11 @@ const store = createStore(NS, {
238245
u.searchParams.delete('html');
239246
history.replaceState(null, '', u);
240247
}
248+
249+
mutationObserver = new MutationObserver(() => {
250+
store.state.hasMutatedDom = true;
251+
store.onRenderedIframeLoad();
252+
});
241253
},
242254

243255
onRenderedIframeLoad() {
@@ -260,7 +272,26 @@ const store = createStore(NS, {
260272
hoverInfo: store.state.hoverInfo,
261273
},
262274
);
275+
mutationObserver?.observe(doc, {
276+
subtree: true,
277+
childList: true,
278+
attributes: true,
279+
characterData: true,
280+
});
281+
Array.prototype.forEach.call(
282+
doc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'template'),
283+
/** @param {HTMLTemplateElement} template */
284+
(template) => {
285+
mutationObserver?.observe(template.content, {
286+
subtree: true,
287+
childList: true,
288+
attributes: true,
289+
characterData: true,
290+
});
291+
},
292+
);
263293
},
294+
264295
clearSpan() {
265296
store.state.span = false;
266297
},
@@ -441,6 +472,10 @@ const store = createStore(NS, {
441472
render() {
442473
// @ts-expect-error This should not be null.
443474
const iframeDocument = RENDERED_IFRAME.contentWindow.document;
475+
476+
mutationObserver?.disconnect();
477+
store.state.hasMutatedDom = false;
478+
444479
iframeDocument.open();
445480
iframeDocument.write(store.state.htmlForProcessing);
446481
iframeDocument.close();

0 commit comments

Comments
 (0)