Skip to content

Commit 11dd0db

Browse files
authored
Merge pull request #1039 from XMOJ-Script-dev/codex/fix-navbar-cjk-font
fix: navbar font mismatch and overlap before Bootstrap loads
2 parents b46e5c2 + c730563 commit 11dd0db

7 files changed

Lines changed: 203 additions & 4 deletions

File tree

‎Update.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,6 +3828,17 @@
38283828
}
38293829
],
38303830
"Notes": "<p>修复新版比赛页面和旧版界面顶部导航栏持续增加窗口缩放监听器的问题,减少长时间打开页面后的内存占用和缩放卡顿;页面导航栏被替换时会清理旧监听器。修复下载页面的卡片被插入导航栏占位元素、导致首行被顶部导航栏遮挡的问题。新版 /web 页面与旧版页面共享黑白主题字体加载,避免菜单栏使用不同字体。</p>"
3831+
},
3832+
"3.8.2": {
3833+
"UpdateDate": 1791075550636,
3834+
"Prerelease": true,
3835+
"UpdateContents": [
3836+
{
3837+
"PR": 1039,
3838+
"Description": "fix: match Chinese navbar fonts between web and classic UI"
3839+
}
3840+
],
3841+
"Notes": "<p>修复新版比赛页面与旧版页面导航栏中文字体不一致的问题。</p>"
38313842
}
38323843
}
38333844
}

‎XMOJ.user.js‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name XMOJ
3-
// @version 3.8.1
3+
// @version 3.8.2
44
// @description XMOJ增强脚本
55
// @author @XMOJ-Script-dev, @langningchen and the community
66
// @namespace https://github/langningchen
@@ -143,6 +143,9 @@ const MonochromeSkinCSS = `
143143
--mono-border-thin: 1px solid var(--mono-gray-300);
144144
--mono-font-heading: 'Playfair Display', Georgia, serif;
145145
--mono-font-body: 'Source Serif 4', 'Source Serif Pro', Georgia, serif;
146+
/* Source Serif has no Chinese glyphs. An explicit navbar fallback
147+
avoids different fonts under legacy lang=en and /web lang=zh-CN. */
148+
--mono-font-navbar: 'Source Serif 4', 'Source Serif Pro', Georgia, 'PingFang SC', 'Microsoft YaHei', 'Noto Sans CJK SC', serif;
146149
--mono-font-mono: 'JetBrains Mono', 'Consolas', monospace;
147150
--mono-transition: 100ms ease;
148151
}
@@ -197,14 +200,15 @@ const MonochromeSkinCSS = `
197200

198201
/* Navbar */
199202
.navbar, nav.navbar {
203+
font-family: var(--mono-font-navbar) !important;
200204
border-bottom: 4px solid var(--mono-black) !important;
201205
background-color: var(--mono-white) !important;
202206
opacity: 1 !important;
203207
}
204208
.navbar .nav-link {
205209
color: var(--mono-black) !important;
206210
text-decoration: none !important;
207-
font-family: var(--mono-font-body) !important;
211+
font-family: var(--mono-font-navbar) !important;
208212
text-transform: uppercase !important;
209213
letter-spacing: 0.05em !important;
210214
font-size: 0.85rem !important;
@@ -2823,7 +2827,7 @@ class NavbarStyler {
28232827
this.navbar = navbar;
28242828
this.addedClasses = ['fixed-top', 'container', 'ml-auto'].filter(name => !navbar.classList.contains(name));
28252829
this.restoreNavbarStyles = this.preserveStyles(navbar, [
2826-
'position', 'border-top-left-radius', 'border-top-right-radius',
2830+
'position', 'top', 'border-top-left-radius', 'border-top-right-radius',
28272831
'border-bottom-left-radius', 'border-bottom-right-radius', 'box-shadow',
28282832
'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
28292833
'max-width', 'background-color', 'opacity', 'z-index'
@@ -2882,6 +2886,9 @@ class NavbarStyler {
28822886
try {
28832887
let n = this.navbar;
28842888
n.classList.add('fixed-top', 'container', 'ml-auto');
2889+
// The CDN fallback may still be loading when the top bar starts. Without
2890+
// Bootstrap's .fixed-top rule, top:auto moves it down with the spacer.
2891+
n.style.top = '0';
28852892
if (UtilityEnabled("MonochromeUI")) {
28862893
Object.assign(n.style, {
28872894
position: 'fixed',

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xmoj-script",
3-
"version": "3.8.1",
3+
"version": "3.8.2",
44
"description": "an improvement script for xmoj.tech",
55
"main": "AddonScript.js",
66
"scripts": {

‎tests/fixtures/fonts/OFL.txt‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2014 The Source Serif 4 Project Authors (https://github.com/adobe-fonts/source-serif)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.

‎tests/fixtures/fonts/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This is the unmodified Latin subset of Source Serif 4 (regular, v15) served by
2+
Google Fonts, the same font used by the monochrome theme. It is distributed under
3+
the SIL Open Font License in OFL.txt.
4+
5+
Source: https://fonts.gstatic.com/s/sourceserif4/v15/vEFy2_tTDB4M7-auWDN0ahZJW3IX2ih5nk3AucvUHf6OAVIJmeUDygwjihdqnhtdDs4DrjTMXw.woff2
6+
License: https://github.com/google/fonts/blob/main/ofl/sourceserif4/OFL.txt
7+
8+
The navbar rendering regression embeds this 20 KB font offline. A local font or
9+
an unloaded font does not reproduce Chromium's language-dependent Chinese
10+
fallback for this webfont; checking computed font-family alone also misses it.
19.6 KB
Binary file not shown.

‎tests/navbar-styler.test.cjs‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,81 @@ test('downloads stay in the page container below the fixed navbar', {timeout: 60
186186
await browser.close();
187187
}
188188
});
189+
190+
test('Chinese navbar glyphs use the same fonts under legacy and web document languages', {timeout: 60000}, async () => {
191+
const skin = source.match(/const MonochromeSkinCSS = `([\s\S]*?)`;/)[1];
192+
const font = fs.readFileSync(path.join(__dirname, "fixtures/fonts/source-serif-4-latin.woff2")).toString("base64");
193+
const browser = await chromium.launch({executablePath: process.env.XMOJ_CHROMIUM || undefined});
194+
try {
195+
const rendered = [];
196+
for (const language of ['en', 'zh-CN']) {
197+
const page = await browser.newPage();
198+
// The public legacy page declares en, while /web declares zh-CN. Source
199+
// Serif 4 has no Chinese glyphs: compare the fonts Chromium actually uses,
200+
// not just getComputedStyle().fontFamily, which matched before the fix.
201+
const html = `<html lang="${language}"><head><style>@font-face { font-family: "Source Serif 4"; src: url(data:font/woff2;base64,${font}) format("woff2"); } ${skin}</style></head><body><nav class="navbar"><a class="navbar-brand">小明的OJ</a><ul class="navbar-nav"><li><a class="nav-link">竞赛&amp;作业</a></li></ul><a class="nav-link dropdown-toggle"><span>测试用户</span></a><ul class="dropdown-menu"><li><a class="dropdown-item">我的作业</a></li></ul></nav></body></html>`;
202+
await page.route("https://navbar-font.test/**", route => route.fulfill({contentType: "text/html; charset=utf-8", body: html}));
203+
await page.goto("https://navbar-font.test/" + language);
204+
await page.evaluate(() => document.fonts.ready);
205+
const session = await page.context().newCDPSession(page);
206+
await session.send('DOM.enable');
207+
await session.send('CSS.enable');
208+
const {root} = await session.send('DOM.getDocument');
209+
const fonts = {};
210+
for (const selector of ['.navbar-brand', '.navbar-nav .nav-link', '.dropdown-toggle span', '.dropdown-item']) {
211+
const {nodeId} = await session.send('DOM.querySelector', {nodeId: root.nodeId, selector});
212+
const result = await session.send('CSS.getPlatformFontsForNode', {nodeId});
213+
fonts[selector] = result.fonts.map(font => ({name: font.familyName, glyphs: font.glyphCount})).sort((a, b) => a.name.localeCompare(b.name));
214+
assert.ok(fonts[selector].length, 'must inspect rendered glyphs: ' + selector);
215+
}
216+
assert.ok(fonts[".navbar-brand"].some(font => font.name === "Source Serif 4"), "the Latin webfont must be loaded to exercise Chinese fallback");
217+
rendered.push(fonts);
218+
await session.detach();
219+
await page.close();
220+
}
221+
assert.deepEqual(rendered[1], rendered[0], 'document language must not change the navbar Chinese fallback font');
222+
} finally {
223+
await browser.close();
224+
}
225+
});
226+
227+
test('navbar reserves content space before Bootstrap CSS loads and restores its original top', {timeout: 60000}, async () => {
228+
const start = 'let navbarStyler = null;';
229+
const end = '// Wrapped in an async IIFE';
230+
const navbarSource = source.slice(source.indexOf(start), source.indexOf(end));
231+
const browser = await chromium.launch({executablePath: process.env.XMOJ_CHROMIUM || undefined});
232+
try {
233+
for (const width of [375, 1440]) {
234+
const page = await browser.newPage({viewport: {width, height: 800}});
235+
for (const mono of [false, true]) {
236+
// The resource cache can be empty and the CDN fallback can be slow
237+
// or fail. No .fixed-top rule is available yet in this fixture.
238+
await page.setContent('<div class="container"><nav class="navbar navbar-expand-lg bg-body-tertiary" style="height:64px">Navbar</nav><main id="content">Page content</main></div>');
239+
await page.evaluate(mono => { window.UtilityEnabled = name => name === 'MonochromeUI' ? mono : name === 'NewTopBar'; }, mono);
240+
await page.addScriptTag({content: '{\n' + navbarSource + '\nUpdateNavbarStyler(); window.testStyler = navbarStyler;\n}'});
241+
const Measure = () => page.evaluate(() => ({
242+
navbarTop: document.querySelector('nav').getBoundingClientRect().top,
243+
navbarBottom: document.querySelector('nav').getBoundingClientRect().bottom,
244+
contentTop: document.getElementById('content').getBoundingClientRect().top
245+
}));
246+
const before = await Measure();
247+
assert.equal(before.navbarTop, mono ? 0 : 16, 'the navbar must anchor to the viewport without Bootstrap');
248+
assert.ok(before.contentTop >= before.navbarBottom, 'the navbar must not cover content while CSS is unavailable');
249+
// When Bootstrap finally arrives, the reserved space still works.
250+
await page.addStyleTag({content: '.fixed-top { position: fixed; top: 0; right: 0; left: 0; }'});
251+
const after = await Measure();
252+
assert.ok(after.contentTop >= after.navbarBottom);
253+
await page.evaluate(() => window.testStyler.destroy());
254+
assert.equal(await page.locator('nav').evaluate(node => node.style.top), '', 'remove the owned offset on teardown');
255+
}
256+
await page.close();
257+
}
258+
const page = await browser.newPage();
259+
await page.setContent('<nav class="navbar navbar-expand-lg bg-body-tertiary" style="height:64px;top:7px!important">Navbar</nav>');
260+
await page.evaluate(() => { window.UtilityEnabled = () => true; });
261+
await page.addScriptTag({content: navbarSource + '\nUpdateNavbarStyler(); navbarStyler.destroy();'});
262+
assert.deepEqual(await page.locator('nav').evaluate(node => [node.style.top, node.style.getPropertyPriority('top')]), ['7px', 'important']);
263+
} finally {
264+
await browser.close();
265+
}
266+
});

0 commit comments

Comments
 (0)