fix(content): sanitize hostile font-size:0 in server book css - #137
Conversation
WeRead's e_2 stylesheet shard occasionally ships 'html, body { ...
font-size: 0; }'. WeRead's own apps ignore root-element sizing, but
crengine honors it, and body text uses p { font-size: 1rem }, so the
whole book collapses to a near-zero font size on device.
Content.sanitize_book_css() now removes standalone zero font-size
declarations (bare or with any unit, optionally !important) while
keeping fractional sizes such as 0.5rem. It iterates to a fixpoint so
adjacent declarations are all removed, preserves block terminators,
and logs how many declarations were dropped.
|
请按模版提交 cr |
|
已按 有一项想说明:测试小节里的「KOReader integration」我留空了。本 PR 只改动 另外「非公开 WeRead API」勾的是不涉及:本 PR 没有新增或修改任何接口调用,只是在 |
|
修复方向没问题,不过这里直接删除所有 font-size: 0 会不会范围有点大?有些样式可能是有意这么设置的。 是不是只处理 html/body 上的 font-size: 0 会更安全一些? |
Review feedback: removing every font-size:0 declaration from the book css
is too broad -- outside the root elements the declaration can be
intentional (e.g. hiding whitespace between inline-block items).
The sanitizer now walks whole selector{block} units and only rewrites
rules whose selector list is exactly html and/or body (case- and
whitespace-insensitive). Compound selectors (body p, body, .wrapper),
:root and @media-wrapped rules pass through verbatim; an at-rule ahead
of the selector no longer blocks matching. The zero-value forms (bare 0,
any letter unit, %, optional !important), the adjacent-declaration
fixpoint and the fractional-size guard are unchanged.
已按这个方向收窄(62c2eb2):sanitizer 现在按 以下情况全部原样保留,不再动:
零值判定(裸 0 / 任意单位 / % / 可选 |
|
请修复一下ci问题 |
The two untouched-cases only checked the removal count, leaving the assigned `cleaned` unused until overwritten, which luacheck flags as a warning and CI treats as fatal. Compare the full returned css instead, which also makes the assertions stricter.
452619e to
3d63fca
Compare
变更说明
修复服务端返回的书籍 CSS 中
html, body { font-size: 0 }导致 KOReader 全书正文塌缩成极小字号的缺陷。新增Content.sanitize_book_css,在fetch_chapter_css解码样式分片后清洗独立的零字号声明。相关:#133(调试同一本书时发现的脚注定义污染问题)。
类型
Bugfix 要求
无关联 issue,提供复现步骤。
复现步骤(修复前):
e_2样式分片中含有html, body { ...; font-size: 0; }的书。实测书目:《天才的責任:維根斯坦傳》繁简版实测环境:KOReader v2026.07.1,Kindle。
根因:
服务端返回的
e_2样式分片偶尔包含:微信读书自家 App 与多数阅读器会忽略根元素的字号设置,但 crengine 会认真执行。正文使用
p { font-size: 1rem },一旦根字号归零,全书随之塌缩。修复:
新增
Content.sanitize_book_css(css),在fetch_chapter_css解码分片时执行,因此覆盖所有消费路径(整书构建、分章缓存、单章拉取):0、任意字母单位(px/em/rem/vh/...)、%,以及可选的!important。小数字号(如0.5rem)与非零值不受影响font-size:0;font-size:0)被全部移除;时保留}),不触碰-webkit-font-size这类带前缀的属性,并记录被移除的声明条数代码内已注明的已知限制:属性名匹配区分大小写(目前观察到的分片均为小写)。
Feature 要求
不适用,本 PR 为 bugfix,未新增特性。
测试
bash scripts/run_lua_specs.shbash scripts/check_lua_namespace.shluacheck main.lua _meta.lua weread specKOReader integration测试说明:
非公开 WeRead API
脚本路径:
复现命令与脱敏结果:
模块结构
未新增或移动任何模块。仅修改既有的
weread/lib/content.lua,并新增spec/content_css_sanitize_spec.lua。命名空间保持不变,未引入根级lib/、ui/模块,也未引入裸lib.*、ui.*模块键。截图
不适用。本 PR 是渲染缺陷修复,未新增或修改 UI、菜单、弹窗与交互。
Checklist
settings/weread.lua、API key、cookie、token、x-wrpa-*或私人书籍内容。weread/lib/、weread/ui/命名空间规范。(不适用:未新增或移动模块)weread/lib/i18n.lua。(不适用:未修改用户可见文本)scripts/中提交可独立运行、可复现的 Python 验证脚本,并填写了复现命令和脱敏结果。(不适用:不涉及)