Files
Garfield c737db3a0d ✅ (linguoland): 补上协议和进度测试
登录、书架和 xpointer 换 CFI 都有离线断言。
2026-11-27 00:51:41 +08:00

74 lines
3.0 KiB
Lua

--[[--
xpointer 换算成官网阅读器的 EPUB CFI。换算失败必须是 nil,不能造一个看起来像的字符串。
@module tests.linguoland.cfi_spec
--]]
local Assert = require("support.assert")
local Cfi = require("moonsource_linguoland.cfi")
local HTML = "<html><head></head><body><p>abcdef</p></body></html>"
Assert.eq(
Cfi.fromHtml(HTML, "/body/DocFragment[1]/body/p/text().4"),
"epubcfi(/6/2!/4/2/1:4)")
Assert.eq(
Cfi.fromHtml(HTML, "/body/DocFragment/body/p/text().4"),
"epubcfi(/6/2!/4/2/1:4)")
local spaced = "<html>\n<head></head>\n<body>\n<p>abcdef</p>\n</body>\n</html>"
Assert.eq(
Cfi.fromHtml(spaced, "/body/DocFragment[1]/body/p/text().4"),
"epubcfi(/6/2!/4/2/1:4)")
Assert.eq(
Cfi.fromHtml(
'<html><head></head><body id="main"><p>ab</p><p>cdefghij</p></body></html>',
"/body/DocFragment[2]/body/p[2]/text().4"),
"epubcfi(/6/4!/4[main]/4/1:4)")
local mixed = "<html><head></head><body><p>Hello <em>x</em> tail</p></body></html>"
Assert.eq(Cfi.fromHtml(mixed, "/body/DocFragment[1]/body/p/text().3"), "epubcfi(/6/2!/4/2/1:3)")
Assert.eq(Cfi.fromHtml(mixed, "/body/DocFragment[1]/body/p/text()[2].1"), "epubcfi(/6/2!/4/2/3:1)")
Assert.eq(Cfi.fromHtml(mixed, "/body/DocFragment[1]/body/p/em/text().0"), "epubcfi(/6/2!/4/2/2/1:0)")
Assert.eq(
Cfi.fromHtml(
"<html><head></head><body><div><p>Hi</p></div></body></html>",
"/body/DocFragment[1]/body/div/p/text().0"),
"epubcfi(/6/2!/4/2/2/1:0)")
Assert.eq(
Cfi.fromHtml(
"<html><head></head><body><p>a&amp;b</p></body></html>",
"/body/DocFragment[1]/body/p/text().2"),
"epubcfi(/6/2!/4/2/1:2)")
Assert.eq(
Cfi.fromHtml(
'<html><head></head><body id="a,b"><p>ab</p></body></html>',
"/body/DocFragment[1]/body/p/text().0"),
"epubcfi(/6/2!/4[a^,b]/2/1:0)")
Assert.is_nil(Cfi.fromHtml(HTML, "/body/DocFragment[1]/body/p/text().99"))
Assert.is_nil(Cfi.fromHtml(HTML, "/body/DocFragment[1]/body/p"))
Assert.is_nil(Cfi.fromHtml(HTML, "/body/DocFragment[1]/body/p/text()[2].0"))
Assert.is_nil(Cfi.fromHtml(HTML, "epubcfi(/6/2!/4/2/1:4)"))
Assert.is_nil(Cfi.fromHtml("<html><p></html>", "/body/DocFragment[1]/body/p/text().0"))
local files = {
["META-INF/container.xml"] = '<container><rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>',
["OEBPS/content.opf"] = '<package><manifest><item id="c1" href="chap.xhtml"/><item id="c2" href="../Text/two.xhtml"/></manifest><spine><itemref idref="c1"/><itemref idref="c2"/></spine></package>',
["OEBPS/chap.xhtml"] = HTML,
["Text/two.xhtml"] = '<html><head></head><body id="main"><p>ab</p><p>cdefghij</p></body></html>',
}
local function read(name)
return files[name]
end
Assert.eq(
Cfi.fromArchive(read, "/body/DocFragment[1]/body/p/text().4"),
"epubcfi(/6/2!/4/2/1:4)")
Assert.eq(
Cfi.fromArchive(read, "/body/DocFragment[2]/body/p[2]/text().4"),
"epubcfi(/6/4!/4[main]/4/1:4)")
Assert.is_nil(Cfi.fromArchive(read, "/body/DocFragment[3]/body/p/text().0"))