Skip to content
This repository was archived by the owner on Oct 14, 2026. It is now read-only.

Commit 7eb90e5

Browse files
anuj-adobeCopilot
andcommitted
test(html-analyzer): harden map root handling
Process only outermost Google Maps runtime roots, assert preserved InfoWindow ordering, and document the browser shim and current header/body DOM assumptions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 460fe0b commit 7eb90e5

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎packages/spacecat-shared-html-analyzer/src/html-filter.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ const MAPS_CONTROL_SELECTORS = [
119119
* @param {Element} element - DOM element to filter
120120
*/
121121
function removeMapWidgets(element) {
122-
element.querySelectorAll(MAPS_RUNTIME_ROOT_SELECTOR).forEach((mapRoot) => {
122+
const mapRoots = Array.from(element.querySelectorAll(MAPS_RUNTIME_ROOT_SELECTOR));
123+
const outermostMapRoots = mapRoots.filter((mapRoot) => !mapRoots.some(
124+
(candidate) => candidate !== mapRoot && candidate.contains(mapRoot),
125+
));
126+
127+
outermostMapRoots.forEach((mapRoot) => {
123128
mapRoot.querySelectorAll('.gm-style-iw').forEach((infoWindow) => {
129+
// Current Maps DOM uses separate header/body nodes; keep their document order.
124130
const contentParts = infoWindow.querySelectorAll('.gm-style-iw-ch, .gm-style-iw-d');
125131
const preservedNodes = contentParts.length ? contentParts : [infoWindow];
126132

@@ -138,7 +144,12 @@ function removeMapWidgets(element) {
138144
* @param {CheerioAPI} $ - Cheerio instance
139145
*/
140146
function removeMapWidgetsCheerio($) {
141-
$(MAPS_RUNTIME_ROOT_SELECTOR).each((i, mapRoot) => {
147+
const mapRoots = $(MAPS_RUNTIME_ROOT_SELECTOR).toArray();
148+
const outermostMapRoots = mapRoots.filter((mapRoot) => !mapRoots.some(
149+
(candidate) => candidate !== mapRoot && $.contains(candidate, mapRoot),
150+
));
151+
152+
outermostMapRoots.forEach((mapRoot) => {
142153
const $mapRoot = $(mapRoot);
143154
$mapRoot.find('.gm-style-iw').each((j, infoWindow) => {
144155
const $infoWindow = $(infoWindow);

‎packages/spacecat-shared-html-analyzer/test/index.test.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
} from '../src/index.js';
2424

2525
function installBrowserDomParserShim() {
26-
// This minimal shim supports selector/removal behavior only; fixtures must not include scripts.
26+
// Cheerio-backed shim verifies call order only, not native NodeList or mutation semantics.
27+
// It supports selector/removal behavior only; fixtures must not include scripts.
2728
const hadWindow = Object.hasOwn(globalThis, 'window');
2829
const hadDocument = Object.hasOwn(globalThis, 'document');
2930
const hadDOMParser = Object.hasOwn(globalThis, 'DOMParser');
@@ -39,6 +40,9 @@ function installBrowserDomParserShim() {
3940
const root = element ? $(element) : $.root();
4041
return root.find(selector).toArray().map((child) => createCheerioElement($, child));
4142
},
43+
contains(child) {
44+
return $.contains(element, child.cheerioElement);
45+
},
4246
remove() {
4347
if (element) {
4448
$(element).remove();
@@ -425,6 +429,10 @@ describe('HTML Visibility Analyzer', () => {
425429
</div>
426430
</div>
427431
<div class="gm-style-iw">Legacy InfoWindow: Sevilla office.</div>
432+
<div class="gm-style">
433+
<div>Nested map runtime noise.</div>
434+
<div class="gm-style-iw">Nested Valencia office.</div>
435+
</div>
428436
<div class="gm-bundled-control">
429437
<div class="gmnoprint">
430438
<div class="gm-svpc">Pegman</div>
@@ -446,12 +454,15 @@ describe('HTML Visibility Analyzer', () => {
446454
expect(text).to.include('Estación Repsol Centro');
447455
expect(text).to.include('Calle Mayor 1, Madrid');
448456
expect(text).to.include('Legacy InfoWindow: Sevilla office.');
457+
expect(text).to.include('Nested Valencia office.');
449458
expect(text).to.not.include('Close window');
459+
expect(text).to.not.include('Nested map runtime noise.');
450460
expect(text).to.not.include('Map data');
451461
expect(text).to.not.include('Pegman');
452462
expect(text).to.not.include('40.4168');
453463
expect(text).to.not.include('Zoom in');
454464
expect(text).to.not.include('Terms of Use');
465+
expect(text.indexOf('Calle Mayor 1, Madrid')).to.be.lessThan(text.indexOf('Horario'));
455466
});
456467

457468
it('should remove Google Maps tile fallback and keyboard shortcut text while preserving location content', async () => {

0 commit comments

Comments
 (0)