vomebook commited on
Commit ·
3fed1fd
1
Parent(s): 81c84ae
Measure shared chapter TOC targets once per marker pass
Browse filesFiles changed (2)
120 unmodified lines121122123124125126127128129120 unmodified linescurrentPosition = -Infinity,first = -1,firstPosition = Infinity;for (const [index, entry] of readerContext.navigationState.tocEntries.entries()) {if (!entry.target?.isConnected) continue;const position = entry.target.getBoundingClientRect().top;if (position <= marker && (position > currentPosition || (position === currentPosition && index > current))) {current = index;currentPosition = position;120 unmodified lines121122123124125126127128129130131132133134135136137138120 unmodified linescurrentPosition = -Infinity,first = -1,firstPosition = Infinity;// CHM fallbacks and fragment aliases can share a target. Reuse its top// only in this read pass: layout and scroll can move it before the next./** @type {Map<Element, number>} */const targetTops = new Map();for (const [index, entry] of readerContext.navigationState.tocEntries.entries()) {const target = entry.target;if (!target?.isConnected) continue;let position = targetTops.get(target);if (position === undefined) {position = target.getBoundingClientRect().top;targetTops.set(target, position);}if (position <= marker && (position > currentPosition || (position === currentPosition && index > current))) {current = index;currentPosition = position;
42 unmodified lines43444546474849440 unmodified lines49049149249349449542 unmodified linesroute.fulfill(response=response, body=response.text() + '''window.__replaceTocForSearch=setToc;window.__toc={nav:()=>navigationState, jump:i=>navigateTocEntry(i),set:setToc, filter:()=>filterPanel(document.querySelector('#toc-panel')),setPageCount:count=>updateDocumentState({pageCount:count}),setPage:page=>{updateDocumentState({page});updateTocCurrentMark()},setCurrent:index=>{updateNavigationState({currentChapterIndex:index});updateTocCurrentMark()},440 unmodified linesself.assertEqual(result['unsafe'],0)self.assertIn('chapter head',result['head'])def test_chapter_target_updates_only_visit_the_owned_toc_once(self):self.expose()# Drive demand loads and the real virtualizer explicitly, without ambient prefetch.42 unmodified lines43444546474849440 unmodified lines49049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057142 unmodified linesroute.fulfill(response=response, body=response.text() + '''window.__replaceTocForSearch=setToc;window.__toc={nav:()=>navigationState, jump:i=>navigateTocEntry(i),set:setToc, sync:()=>syncHeadingLocation(), filter:()=>filterPanel(document.querySelector('#toc-panel')),setPageCount:count=>updateDocumentState({pageCount:count}),setPage:page=>{updateDocumentState({page});updateTocCurrentMark()},setCurrent:index=>{updateNavigationState({currentChapterIndex:index});updateTocCurrentMark()},440 unmodified linesself.assertEqual(result['unsafe'],0)self.assertIn('chapter head',result['head'])def test_chapter_scroll_geometry_census_with_shared_toc_targets(self):self.expose()def expose_window(route):response = route.fetch()script = response.text()needle = ' function scheduleChapterWindow() {'self.assertIn(needle, script)route.fulfill(response=response, body=script.replace(needle,' window.__refreshChapterWindow = refreshChapterWindow;\n' + needle, 1))self.page.route('**/static/reader/formats/reader-chapter-format.mjs*', expose_window)prose = '<p>A line in the converted chapter.</p>' * 120body = ('<p id="lead" style="height:120px"><a href="#chm-page-1">First page</a> ''<a href="#chm-page-2">Second page</a></p>'f'<section id="chm-page-1"><h1>One</h1>{prose}</section>'f'<section id="chm-page-2"><h1>Two</h1>{prose}</section>')# A merged CHM can have many entries with no recoverable fragment. Each# still participates in tie-breaking even though they share the article.entries = [(f'Unresolved entry {i}', '') for i in range(120)] + [('First page', ''), ('Second page', ''), ('Second page alias', 'chm-page-2'), ('Missing', 'missing')]manifest = {'version': 1, 'kind': 'epub-chapters', 'chapters': [{'index': 1, 'path': 'chapters/chapter-0001.xhtml', 'source_path': 'chm-fallback.html', 'bytes': len(body)}], 'toc': [{'title': label, 'chapter': 1, 'fragment': fragment, 'depth': 0} for label, fragment in entries]}self.page.route('**/api/reader-bucket-resource**', lambda route: route.fulfill(json=manifest)if 'chapter-manifest' in route.request.url else route.fulfill(content_type='text/html', body=body))source = ('https://voiceofml-search.hf.space/api/reader-bucket-resource?path=chapters/ebook/chm/'+ 'a' * 64 + '/1234567890abcdef/chapter-manifest.json')self.page.goto(self.origin + support.PREFIX + '/static/reader.html?' +urlencode(dict(url=source, ext='epub-chapters')), wait_until='domcontentloaded')self.page.wait_for_function("()=>document.documentElement.dataset.readerPhase==='ready'")result = self.page.evaluate('''() => {const viewport = document.querySelector('#viewport');const article = viewport.querySelector('.reader-epub-chapter');const second = article.querySelector('#chm-page-2');const placeholder = document.createElement('button');placeholder.className = 'reader-chapter-sentinel reader-chapter-placeholder';placeholder.dataset.chapter = '0';placeholder.style.height = '120px';article.before(placeholder);viewport.scrollTop += second.getBoundingClientRect().top - viewport.getBoundingClientRect().top - 4;const entries = __toc.nav().tocEntries;const connected = entries.map(entry => entry.target).filter(target => target?.isConnected);const rect = Element.prototype.getBoundingClientRect;const census = run => {const nodes = new Set();let calls = 0;Element.prototype.getBoundingClientRect = function () {nodes.add(this);calls++;return rect.call(this);};try { run(); } finally { Element.prototype.getBoundingClientRect = rect; }return [nodes.size, calls];};const current = () => [__toc.nav().currentChapterIndex,Number(document.querySelector('#toc-list .is-current')?.dataset.tocIndex)];// Sample the real synchronous scroll listener and the two independently// scheduled passes in isolation, before any queued frame can run.const remember = census(() => viewport.dispatchEvent(new Event('scroll')));const windowRows = census(__refreshChapterWindow);const toc = census(__toc.sync);const before = current();const scroll = viewport.scrollTop;article.querySelector('#lead').style.height = '240px';viewport.scrollTop = scroll;const lateToc = census(__toc.sync);return {entries: [entries.length, connected.length, new Set(connected).size],remember, windowRows, toc, lateToc, before, after: current()};}''')# Counts are [unique measured nodes, rectangle reads]. The unmodified# reader reported [4, 124] for each TOC pass in both builds. Remember# (244 candidates + viewport) and window rows are unchanged.self.assertEqual(result, dict(entries=[124, 123, 3], remember=[245, 245], windowRows=[3, 3],toc=[4, 4], lateToc=[4, 4], before=[122, 122], after=[120, 120]))def test_chapter_target_updates_only_visit_the_owned_toc_once(self):self.expose()# Drive demand loads and the real virtualizer explicitly, without ambient prefetch.
static/reader/ui/reader-toc.mjs+11-2
tests/reader/browser/test_reader_toc_navigation.py+77-1