@@ -18,7 +18,7 @@ var allowedTags = [
18
18
'math' , 'maction' , 'menclose' , 'merror' , 'mfenced' , 'mfrac' , 'mglyph' , 'mi' , 'mlabeledtr' , 'mmultiscripts' , 'mn' , 'mo' , 'mover' , 'mpadded' , 'mphantom' , 'mroot' ,
19
19
'mrow' , 'ms' , 'mspace' , 'msqrt' , 'mstyle' , 'msub' , 'msup' , 'msubsup' , 'mtable' , 'mtd' , 'mtext' , 'mtr' , 'munder' , 'munderover' , 'msgroup' , 'mlongdiv' , 'mscarries' ,
20
20
'mscarry' , 'mstack' , 'semantics'
21
- // TODO ?
21
+ // TODO ?
22
22
// ,'form', 'button'
23
23
24
24
// TODO svg support ?
@@ -38,12 +38,12 @@ var tmpIdsToNewCssSTRING = {};
38
38
var supportedCss = [
39
39
'background-color' ,
40
40
'border' ,
41
- 'color' ,
41
+ 'color' ,
42
42
'font' ,
43
43
'line-height' ,
44
44
'list-style' ,
45
45
'padding' ,
46
- 'text-align' ,
46
+ 'text-align' ,
47
47
] ;
48
48
//////
49
49
@@ -73,7 +73,7 @@ function getImageSrc(srcTxt) {
73
73
filename : newImgFileName , // TODO name
74
74
data : getBase64ImgData ( srcTxt )
75
75
} ) ;
76
- } else {
76
+ } else {
77
77
allImages . push ( {
78
78
originalUrl : getImgDownloadUrl ( srcTxt ) ,
79
79
filename : newImgFileName , // TODO name
@@ -118,28 +118,31 @@ function extractSvgToImg($htmlObject) {
118
118
119
119
// replaces all iframes by divs with the same innerHTML content
120
120
function extractIFrames ( ) {
121
- let allIframes = document . getElementsByTagName ( 'iframe' )
122
- let changeIFrames = [ ]
123
- let newDivs = [ ]
124
- for ( let iFrame of allIframes ) {
125
- if ( ! iFrame . contentDocument || ! iFrame . contentDocument . body ) {
126
- continue
127
- }
128
- let bodyContent = iFrame . contentDocument . body . innerHTML
129
- let bbox = iFrame . getBoundingClientRect ( )
130
- let newDiv = document . createElement ( 'div' )
131
- newDiv . style . width = bbox . width
132
- newDiv . style . height = bbox . height
133
- newDiv . innerHTML = bodyContent
134
- changeIFrames . push ( iFrame )
135
- newDivs . push ( newDiv )
136
- }
137
- for ( let i = 0 ; i < newDivs . length ; i ++ ) {
138
- let newDiv = newDivs [ i ]
139
- let iFrame = changeIFrames [ i ]
140
- let iframeParent = iFrame . parentNode
141
- iframeParent . replaceChild ( newDiv , iFrame )
121
+ function editStyle ( style , id ) {
122
+ return style . split ( "\n" ) . map ( function ( line ) {
123
+ if ( ! / \{ / . test ( line ) ) {
124
+ return line ;
125
+ }
126
+ return "#" + id + " " + line . replace ( "body" , "" ) ;
127
+ } ) . join ( "\n" ) ;
142
128
}
129
+ let iframes = Array . from ( document . querySelectorAll ( "iframe" ) ) ;
130
+ const divs = iframes . map ( function ( iframe , index ) {
131
+ const div = document . createElement ( "div" ) ;
132
+ div . id = "save-as-ebook-iframe-" + index ;
133
+ if ( ! iframe . contentDocument || ! iframe . contentDocument . body ) {
134
+ return div ;
135
+ }
136
+ let bbox = iframe . getBoundingClientRect ( ) ;
137
+ div . style . width = bbox . width ;
138
+ div . style . height = bbox . height ;
139
+ div . innerHTML = iframe . contentDocument . body . innerHTML ?? "" ;
140
+ Array . from ( div . querySelectorAll ( "style" ) ) . forEach ( function ( style ) {
141
+ style . innerHTML = editStyle ( style . innerHTML , div . id ) ;
142
+ } ) ;
143
+ return div ;
144
+ } ) ;
145
+ iframes . forEach ( ( iframe , i ) => iframe . parentNode . replaceChild ( divs [ i ] , iframe ) ) ;
143
146
}
144
147
145
148
function preProcess ( $htmlObject ) {
@@ -381,7 +384,7 @@ function extractCss(includeStyle, appliedStyles) {
381
384
382
385
function deferredAddZip ( url , filename ) {
383
386
let deferred = $ . Deferred ( ) ;
384
- JSZipUtils . getBinaryContent ( url , function ( err , data ) {
387
+ JSZipUtils . getBinaryContent ( url , function ( err , data ) {
385
388
if ( err ) {
386
389
// deferred.reject(err); TODO
387
390
console . log ( 'Error:' , err ) ;
@@ -408,13 +411,13 @@ function deferredAddZip(url, filename) {
408
411
}
409
412
tmpGlobalContent = tmpGlobalContent . replace ( oldFilename , filename )
410
413
}
411
-
414
+
412
415
extractedImages . push ( {
413
416
filename : filename ,
414
417
// TODO - must be JSON serializable
415
418
data : base64ArrayBuffer ( data )
416
419
} ) ;
417
-
420
+
418
421
deferred . resolve ( ) ;
419
422
}
420
423
} ) ;
0 commit comments