Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions XMOJ.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,7 @@ class NavbarStyler {
this.overlay?.remove();
this.overlayStyle?.remove();
this.spacer?.remove();
this.spacerStyle?.remove();
this.navbar = null;
}

Expand Down Expand Up @@ -3577,14 +3578,20 @@ class NavbarStyler {
try {
let spacer = document.getElementById('navbar-spacer');
let newHeight = this.navbar.offsetHeight + 24;
// Browser cosmetic filters can hide empty body children whose inline
// style starts with display:block; width:100%; height:. Keep static
// layout rules outside that attribute, including when Bootstrap is late.
if (!this.spacerStyle?.isConnected) {
this.spacerStyle = document.createElement('style');
this.spacerStyle.textContent = '#navbar-spacer { display: block; width: 100%; }';
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
(document.head || document.documentElement).appendChild(this.spacerStyle);
}
if (!spacer) {
// Legacy page handlers use body > div for the content container.
// A block span reserves space without becoming their first match.
spacer = document.createElement('span');
spacer.id = 'navbar-spacer';
spacer.setAttribute('aria-hidden', 'true');
spacer.style.display = 'block';
spacer.style.width = '100%';
document.body.insertBefore(spacer, document.body.firstChild);
this.spacer = spacer;
} else if (spacer !== this.spacer && spacer !== this.borrowedSpacer) {
Expand Down
49 changes: 49 additions & 0 deletions tests/navbar-styler.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,52 @@ test('navbar reserves content space before Bootstrap CSS loads and restores its
await browser.close();
}
});

test('browser cosmetic filtering does not hide the navbar spacer', {timeout: 60000}, async () => {
const start = 'let navbarStyler = null;';
const end = '// Wrapped in an async IIFE';
const navbarSource = source.slice(source.indexOf(start), source.indexOf(end));
const browser = await chromium.launch({executablePath: process.env.XMOJ_CHROMIUM || undefined});
const page = await browser.newPage();
try {
// The reporter's video shows this browser-injected cosmetic rule. A block
// spacer whose inline style begins with these declarations is hidden.
await page.setContent(`<style>
body > [style^="display: block; width: 100%; height:"]:empty { display: none !important; }
body { margin: 0; }
.navbar { height: 64px; }
.fixed-top { top: 0; left: 0; right: 0; }
</style><div id="content"><nav class="navbar navbar-expand-lg bg-body-tertiary">Navbar</nav><main>Page content</main></div>`);
await page.evaluate(() => { window.UtilityEnabled = () => true; });
await page.addScriptTag({content: navbarSource + '\nUpdateNavbarStyler();'});
const Measure = () => page.evaluate(() => {
const spacer = document.getElementById('navbar-spacer');
return {
display: getComputedStyle(spacer).display,
height: spacer.getBoundingClientRect().height,
filtered: spacer.matches('body > [style^="display: block; width: 100%; height:"]:empty'),
navbarBottom: document.querySelector('nav').getBoundingClientRect().bottom,
contentTop: document.querySelector('main').getBoundingClientRect().top
};
});
for (const height of [64, 96]) {
await page.evaluate(height => { document.querySelector('nav').style.height = height + 'px'; UpdateNavbarStyler(); }, height);
const result = await Measure();
assert.equal(result.filtered, false, 'our spacer must not match the recorded browser filter');
assert.equal(result.display, 'block');
assert.equal(result.height, height + 24);
assert.ok(result.contentTop >= result.navbarBottom, 'the first content must remain below the navbar');
}
// Switching navbars releases and recreates the spacer and its stylesheet.
await page.evaluate(() => {
document.querySelector('nav').outerHTML = '<nav class="navbar navbar-expand-lg bg-body-tertiary" style="height:48px">Replacement</nav>';
UpdateNavbarStyler();
});
assert.equal((await Measure()).height, 72);
await page.evaluate(() => { window.UtilityEnabled = () => false; UpdateNavbarStyler(); });
assert.equal(await page.locator('#navbar-spacer').count(), 0);
assert.equal(await page.locator('head style').count(), 1, 'remove the owned styles but keep the browser-rule fixture');
} finally {
await browser.close();
}
});
Loading