@@ -70,27 +70,30 @@ const BODY = {
7070 'section' : [ 'section' , SECTION ] ,
7171}
7272
73- const getImageSrc = el => {
74- const href = el . getAttributeNS ( NS . XLINK , 'href' )
75- if ( ! href ) return 'data:,'
76- const [ , id ] = href . split ( '#' )
77- if ( ! id ) return href
78- const bin = el . getRootNode ( ) . getElementById ( id )
79- return bin
80- ? `data:${ bin . getAttribute ( 'content-type' ) } ;base64,${ bin . textContent } `
81- : href
82- }
83-
8473class FB2Converter {
8574 constructor ( fb2 ) {
8675 this . fb2 = fb2
8776 this . doc = document . implementation . createDocument ( NS . XHTML , 'html' )
77+ // use this instead of `getElementById` to allow images like
78+ // `<image l:href="#img1.jpg" id="img1.jpg" />`
79+ this . bins = new Map ( Array . from ( this . fb2 . getElementsByTagName ( 'binary' ) ,
80+ el => [ el . id , el ] ) )
81+ }
82+ getImageSrc ( el ) {
83+ const href = el . getAttributeNS ( NS . XLINK , 'href' )
84+ if ( ! href ) return 'data:,'
85+ const [ , id ] = href . split ( '#' )
86+ if ( ! id ) return href
87+ const bin = this . bins . get ( id )
88+ return bin
89+ ? `data:${ bin . getAttribute ( 'content-type' ) } ;base64,${ bin . textContent } `
90+ : href
8891 }
8992 image ( node ) {
9093 const el = this . doc . createElement ( 'img' )
9194 el . alt = node . getAttribute ( 'alt' )
9295 el . title = node . getAttribute ( 'title' )
93- el . setAttribute ( 'src' , getImageSrc ( node ) )
96+ el . setAttribute ( 'src' , this . getImageSrc ( node ) )
9497 return el
9598 }
9699 anchor ( node ) {
@@ -267,7 +270,7 @@ export const makeFB2 = async blob => {
267270 subject : $$ ( 'title-info genre' ) . map ( getElementText ) ,
268271 }
269272 if ( $ ( 'coverpage image' ) ) {
270- const src = getImageSrc ( $ ( 'coverpage image' ) )
273+ const src = converter . getImageSrc ( $ ( 'coverpage image' ) )
271274 book . getCover = ( ) => fetch ( src ) . then ( res => res . blob ( ) )
272275 } else book . getCover = ( ) => null
273276
0 commit comments