--[[-- xpointer 换算成官网阅读器的 EPUB CFI。换算失败必须是 nil,不能造一个看起来像的字符串。 @module tests.linguoland.cfi_spec --]] local Assert = require("support.assert") local Cfi = require("moonsource_linguoland.cfi") local HTML = "

abcdef

" 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 = "\n\n\n

abcdef

\n\n" Assert.eq( Cfi.fromHtml(spaced, "/body/DocFragment[1]/body/p/text().4"), "epubcfi(/6/2!/4/2/1:4)") Assert.eq( Cfi.fromHtml( '

ab

cdefghij

', "/body/DocFragment[2]/body/p[2]/text().4"), "epubcfi(/6/4!/4[main]/4/1:4)") local mixed = "

Hello x tail

" 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( "

Hi

", "/body/DocFragment[1]/body/div/p/text().0"), "epubcfi(/6/2!/4/2/2/1:0)") Assert.eq( Cfi.fromHtml( "

a&b

", "/body/DocFragment[1]/body/p/text().2"), "epubcfi(/6/2!/4/2/1:2)") Assert.eq( Cfi.fromHtml( '

ab

', "/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("

", "/body/DocFragment[1]/body/p/text().0")) local files = { ["META-INF/container.xml"] = '', ["OEBPS/content.opf"] = '', ["OEBPS/chap.xhtml"] = HTML, ["Text/two.xhtml"] = '

ab

cdefghij

', } 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"))