Debian Codegraph @

src/foliate-js/tests/epubcfi-tests.js

raw
1 import * as CFI from '../epubcfi.js'
2
3 const parser = new DOMParser()
4 const XML = str => parser.parseFromString(str, 'application/xml')
5 const XHTML = str => parser.parseFromString(str, 'application/xhtml+xml')
6
7 {
8 // example from EPUB CFI spec
9 const opf = XML(`<?xml version="1.0"?>
10
11 <package version="2.0"
12 unique-identifier="bookid"
13 xmlns="http://www.idpf.org/2007/opf"
14 xmlns:dc="http://purl.org/dc/elements/1.1/"
15 xmlns:opf="http://www.idpf.org/2007/opf">
16
17 <metadata>
18 <dc:title>…</dc:title>
19 <dc:identifier id="bookid">…</dc:identifier>
20 <dc:creator>…</dc:creator>
21 <dc:language>en</dc:language>
22 </metadata>
23
24 <manifest>
25 <item id="toc"
26 properties="nav"
27 href="toc.xhtml"
28 media-type="application/xhtml+xml"/>
29 <item id="titlepage"
30 href="titlepage.xhtml"
31 media-type="application/xhtml+xml"/>
32 <item id="chapter01"
33 href="chapter01.xhtml"
34 media-type="application/xhtml+xml"/>
35 <item id="chapter02"
36 href="chapter02.xhtml"
37 media-type="application/xhtml+xml"/>
38 <item id="chapter03"
39 href="chapter03.xhtml"
40 media-type="application/xhtml+xml"/>
41 <item id="chapter04"
42 href="chapter04.xhtml"
43 media-type="application/xhtml+xml"/>
44 </manifest>
45
46 <spine>
47 <itemref id="titleref" idref="titlepage"/>
48 <itemref id="chap01ref" idref="chapter01"/>
49 <itemref id="chap02ref" idref="chapter02"/>
50 <itemref id="chap03ref" idref="chapter03"/>
51 <itemref id="chap04ref" idref="chapter04"/>
52 </spine>
53
54 </package>`)
55
56 const a = opf.getElementById('chap01ref')
57 const b = CFI.toElement(opf, CFI.parse('/6/4[chap01ref]')[0])
58 const c = CFI.toElement(opf, CFI.parse('/6/4')[0])
59 console.assert(a === b)
60 console.assert(a === c)
61 }
62
63 {
64 // example from EPUB CFI spec
65 const page = XHTML(`<html xmlns="http://www.w3.org/1999/xhtml">
66 <head>
67 <title>…</title>
68 </head>
69
70 <body id="body01">
71 <p>…</p>
72 <p>…</p>
73 <p>…</p>
74 <p>…</p>
75 <p id="para05">xxx<em>yyy</em>0123456789</p>
76 <p>…</p>
77 <p>…</p>
78 <img id="svgimg" src="foo.svg" alt="…"/>
79 <p>…</p>
80 <p>…</p>
81 </body>
82 </html>`)
83
84 // the exact same page with some text nodes removed, CDATA & comment added,
85 // and characters changed to entities
86 const page2 = XHTML(`<html xmlns="http://www.w3.org/1999/xhtml">
87 <head>
88 <title>…</title>
89 </head>
90 <body id="body01">
91 <p>…</p><p>…</p><p>…</p><p>…</p>
92 <p id="para05">xxx<em>yyy</em><![CDATA[]]><!--comment1--><![CDATA[0123]]>4<!--comment2-->5<![CDATA[67]]>&#56;&#57;</p>
93 <p>…</p>
94 <p>…</p>
95 <img id="svgimg" src="foo.svg" alt="…"/>
96 <p>…</p>
97 <p>…</p>
98 </body>
99 </html>`)
100
101 // the exact same page with nodes are to be ignored
102 const page3 = XHTML(`<html xmlns="http://www.w3.org/1999/xhtml">
103 <head>
104 <title>…</title>
105 </head>
106 <body id="body01">
107 <h1 class="reject">This is ignored!</h1>
108 <section class="skip">
109 <p class="reject">Also ignored</p>
110 <p>…</p><p>…</p><p>…</p><p>…</p>
111 <p id="para05">xxx<em>yyy</em><span class="reject">Note: we put ignored text in this span but not the other ones because although the CFI library should ignore them, they won't be ignored by DOM Ranges, which will break the tests.</span><span class="skip">0<span class="skip"><span class="reject"><![CDATA[]]></span>123</span></span>45<span class="reject"><img src="icon.svg"/></span>6789</p>
112 <p>…</p>
113 <p>…</p>
114 <img id="svgimg" src="foo.svg" alt="…"/>
115 <p>…</p>
116 <p>…</p>
117 </section>
118 </body>
119 </html>`)
120
121 const filter = node => node.nodeType !== 1 ? NodeFilter.FILTER_ACCEPT
122 : node.matches('.reject') ? NodeFilter.FILTER_REJECT
123 : node.matches('.skip') ? NodeFilter.FILTER_SKIP
124 : NodeFilter.FILTER_ACCEPT
125
126 const test = (page, filter) => {
127 for (const cfi of [
128 '/4[body01]/10[para05]/3:10',
129 '/4[body01]/16[svgimg]',
130 '/4[body01]/10[para05]/1:0',
131 '/4[body01]/10[para05]/2/1:0',
132 '/4[body01]/10[para05]/2/1:3',
133 ]) {
134 const range = CFI.toRange(page, CFI.parse(cfi), filter)
135 const a = CFI.fromRange(range, filter)
136 const b = `epubcfi(${cfi})`
137 console.assert(a === b, `expected ${b}, got ${a}`)
138 }
139 for (let i = 0; i < 10; i++) {
140 const cfi = `/4/10,/3:${i},/3:${i+1}`
141 const range = CFI.toRange(page, CFI.parse(cfi), filter)
142 const n = `${i}`
143 console.assert(range.toString() === n, `expected ${n}, got ${range}`)
144 }
145 }
146 test(page)
147 test(page2)
148
149 test(page, filter)
150 test(page2, filter)
151 test(page3, filter)
152 }
153
154 {
155 // special characters in ID assertions
156 const opf = XML(`<?xml version="1.0"?>
157 <package version="2.0"
158 unique-identifier="bookid"
159 xmlns="http://www.idpf.org/2007/opf"
160 xmlns:dc="http://purl.org/dc/elements/1.1/"
161 xmlns:opf="http://www.idpf.org/2007/opf">
162 <metadata></metadata>
163 <manifest></manifest>
164 <spine>
165 <itemref id="titleref" idref="titlepage"/>
166 <itemref id="chap0]!/1ref^" idref="chapter01"/>
167 <itemref id="chap02ref" idref="chapter02"/>
168 <itemref id="chap03ref" idref="chapter03"/>
169 <itemref id="chap04ref" idref="chapter04"/>
170 </spine>
171 </package>`)
172
173 const a = opf.getElementById('chap0]!/1ref^')
174 const b = CFI.toElement(opf, CFI.parse('/6/4[chap0^]!/1ref^^]')[0])
175 console.assert(a === b)
176
177 const page = XHTML(`<html xmlns="http://www.w3.org/1999/xhtml">
178 <head>
179 <title>…</title>
180 </head>
181 <body id="body0]!/1^">
182 <p>…</p>
183 <p>…</p>
184 <p>…</p>
185 <p>…</p>
186 <p id="para]/0,/5">xxx<em>yyy</em>0123456789</p>
187 <p>…</p>
188 <p>…</p>
189 <img id="s][vgimg" src="foo.svg" alt="…"/>
190 <p>…</p>
191 <p>…</p>
192 </body>
193 </html>`)
194
195 for (const cfi of [
196 '/4[body0^]!/1^^]/10[para^]/0^,/5]/3:10',
197 '/4[body0^]!/1^^]/16[s^]^[vgimg]',
198 '/4[body0^]!/1^^]/10[para^]/0^,/5]/1:0',
199 '/4[body0^]!/1^^]/10[para^]/0^,/5]/2/1:0',
200 '/4[body0^]!/1^^]/10[para^]/0^,/5]/2/1:3',
201 ]) {
202 const range = CFI.toRange(page, CFI.parse(cfi))
203 const a = CFI.fromRange(range)
204 const b = `epubcfi(${cfi})`
205 console.assert(a === b, `expected ${b}, got ${a}`)
206 }
207 for (let i = 0; i < 10; i++) {
208 const cfi = `/4[body0^]!/1^^]/10[para^]/0^,^/5],/3:${i},/3:${i+1}`
209 const range = CFI.toRange(page, CFI.parse(cfi))
210 const n = `${i}`
211 console.assert(range.toString() === n, `expected ${n}, got ${range}`)
212 }
213 }
214
215 {
216 for (const [a, b, c] of [
217 ['/6/4!/10', '/6/4!/10', 0],
218 ['/6/4!/2/3:0', '/6/4!/2', 1],
219 ['/6/4!/2/4/6/8/10/3:0', '/6/4!/4', -1],
220 [
221 '/6/4[chap0^]!/1ref^^]!/4[body01^^]/10[para^]^,05^^]',
222 '/6/4!/4/10',
223 0,
224 ],
225 [
226 '/6/4[chap0^]!/1ref^^]!/4[body01^^],/10[para^]^,05^^],/15:10[foo^]]',
227 '/6/4!/4/12',
228 -1,
229 ],
230 ['/6/4', '/6/4!/2', -1],
231 ['/6/4!/2', '/6/4!/2!/2', -1],
232 ]) {
233 const x = CFI.compare(a, b)
234 console.assert(x === c, `compare ${a} and ${b}, expected ${c}, got ${x}`)
235 }
236 }