@@ -45,99 +45,99 @@ Worker.prototype.toCanvas = function toCanvas() {
45
45
} ;
46
46
47
47
function onclone_pagebreak ( oncloneOrig , doc ) {
48
- // Setup root element and inner page height.
49
- var root = doc . body ;
50
- var pxPageHeight = this . prop . pageSize . inner . px . height ;
51
-
52
- // Check all requested modes.
53
- var modeSrc = [ ] . concat ( this . opt . pagebreak . mode ) ;
54
- var mode = {
55
- avoidAll : modeSrc . indexOf ( 'avoid-all' ) !== - 1 ,
56
- css : modeSrc . indexOf ( 'css' ) !== - 1 ,
57
- legacy : modeSrc . indexOf ( 'legacy' ) !== - 1
48
+ // Setup root element and inner page height.
49
+ var root = doc . body ;
50
+ var pxPageHeight = this . prop . pageSize . inner . px . height ;
51
+
52
+ // Check all requested modes.
53
+ var modeSrc = [ ] . concat ( this . opt . pagebreak . mode ) ;
54
+ var mode = {
55
+ avoidAll : modeSrc . indexOf ( 'avoid-all' ) !== - 1 ,
56
+ css : modeSrc . indexOf ( 'css' ) !== - 1 ,
57
+ legacy : modeSrc . indexOf ( 'legacy' ) !== - 1
58
+ } ;
59
+
60
+ // Get arrays of all explicitly requested elements.
61
+ var select = { } ;
62
+ var self = this ;
63
+ [ 'before' , 'after' , 'avoid' ] . forEach ( function ( key ) {
64
+ var all = mode . avoidAll && key === 'avoid' ;
65
+ select [ key ] = all ? [ ] : [ ] . concat ( self . opt . pagebreak [ key ] || [ ] ) ;
66
+ if ( select [ key ] . length > 0 ) {
67
+ select [ key ] = Array . prototype . slice . call (
68
+ root . querySelectorAll ( select [ key ] . join ( ', ' ) ) ) ;
69
+ }
70
+ } ) ;
71
+
72
+ // Get all legacy page-break elements.
73
+ var legacyEls = root . querySelectorAll ( '.html2pdf__page-break' ) ;
74
+ legacyEls = Array . prototype . slice . call ( legacyEls ) ;
75
+
76
+ // Loop through all elements.
77
+ var els = root . querySelectorAll ( '*' ) ;
78
+ Array . prototype . forEach . call ( els , function pagebreak_loop ( el ) {
79
+ // Setup pagebreak rules based on legacy and avoidAll modes.
80
+ var rules = {
81
+ before : false ,
82
+ after : mode . legacy && legacyEls . indexOf ( el ) !== - 1 ,
83
+ avoid : mode . avoidAll
58
84
} ;
59
85
60
- // Get arrays of all explicitly requested elements.
61
- var select = { } ;
62
- var self = this ;
63
- [ 'before' , 'after' , 'avoid' ] . forEach ( function ( key ) {
64
- var all = mode . avoidAll && key === 'avoid' ;
65
- select [ key ] = all ? [ ] : [ ] . concat ( self . opt . pagebreak [ key ] || [ ] ) ;
66
- if ( select [ key ] . length > 0 ) {
67
- select [ key ] = Array . prototype . slice . call (
68
- root . querySelectorAll ( select [ key ] . join ( ', ' ) ) ) ;
69
- }
70
- } ) ;
71
-
72
- // Get all legacy page-break elements.
73
- var legacyEls = root . querySelectorAll ( '.html2pdf__page-break' ) ;
74
- legacyEls = Array . prototype . slice . call ( legacyEls ) ;
75
-
76
- // Loop through all elements.
77
- var els = root . querySelectorAll ( '*' ) ;
78
- Array . prototype . forEach . call ( els , function pagebreak_loop ( el ) {
79
- // Setup pagebreak rules based on legacy and avoidAll modes.
80
- var rules = {
81
- before : false ,
82
- after : mode . legacy && legacyEls . indexOf ( el ) !== - 1 ,
83
- avoid : mode . avoidAll
86
+ // Add rules for css mode.
87
+ if ( mode . css ) {
88
+ // TODO: Check if this is valid with iFrames.
89
+ var style = window . getComputedStyle ( el ) ;
90
+ // TODO: Handle 'left' and 'right' correctly.
91
+ // TODO: Add support for 'avoid' on breakBefore/After.
92
+ var breakOpt = [ 'always' , 'page' , 'left' , 'right' ] ;
93
+ var avoidOpt = [ 'avoid' , 'avoid-page' ] ;
94
+ rules = {
95
+ before : rules . before || breakOpt . indexOf ( style . breakBefore || style . pageBreakBefore ) !== - 1 ,
96
+ after : rules . after || breakOpt . indexOf ( style . breakAfter || style . pageBreakAfter ) !== - 1 ,
97
+ avoid : rules . avoid || avoidOpt . indexOf ( style . breakInside || style . pageBreakInside ) !== - 1
84
98
} ;
99
+ }
85
100
86
- // Add rules for css mode.
87
- if ( mode . css ) {
88
- // TODO: Check if this is valid with iFrames.
89
- var style = window . getComputedStyle ( el ) ;
90
- // TODO: Handle 'left' and 'right' correctly.
91
- // TODO: Add support for 'avoid' on breakBefore/After.
92
- var breakOpt = [ 'always' , 'page' , 'left' , 'right' ] ;
93
- var avoidOpt = [ 'avoid' , 'avoid-page' ] ;
94
- rules = {
95
- before : rules . before || breakOpt . indexOf ( style . breakBefore || style . pageBreakBefore ) !== - 1 ,
96
- after : rules . after || breakOpt . indexOf ( style . breakAfter || style . pageBreakAfter ) !== - 1 ,
97
- avoid : rules . avoid || avoidOpt . indexOf ( style . breakInside || style . pageBreakInside ) !== - 1
98
- } ;
99
- }
101
+ // Add rules for explicit requests.
102
+ Object . keys ( rules ) . forEach ( function ( key ) {
103
+ rules [ key ] = rules [ key ] || select [ key ] . indexOf ( el ) !== - 1 ;
104
+ } ) ;
100
105
101
- // Add rules for explicit requests.
102
- Object . keys ( rules ) . forEach ( function ( key ) {
103
- rules [ key ] = rules [ key ] || select [ key ] . indexOf ( el ) !== - 1 ;
104
- } ) ;
105
-
106
- // Get element position on the screen.
107
- // TODO: Subtract the top of the container from clientRect.top/bottom?
108
- var clientRect = el . getBoundingClientRect ( ) ;
109
-
110
- // Avoid: Check if a break happens mid-element.
111
- if ( rules . avoid && ! rules . before ) {
112
- var startPage = Math . floor ( clientRect . top / pxPageHeight ) ;
113
- var endPage = Math . floor ( clientRect . bottom / pxPageHeight ) ;
114
- var nPages = Math . abs ( clientRect . bottom - clientRect . top ) / pxPageHeight ;
115
-
116
- // Turn on rules.before if the el is broken and is at most one page long.
117
- if ( endPage !== startPage && nPages <= 1 ) {
118
- rules . before = true ;
119
- }
120
- }
106
+ // Get element position on the screen.
107
+ // TODO: Subtract the top of the container from clientRect.top/bottom?
108
+ var clientRect = el . getBoundingClientRect ( ) ;
121
109
122
- // Before: Create a padding div to push the element to the next page.
123
- if ( rules . before ) {
124
- var pad = createElement ( 'div' , { style : {
125
- display : 'block' ,
126
- height : pxPageHeight - ( clientRect . top % pxPageHeight ) + 'px'
127
- } } ) ;
128
- el . parentNode . insertBefore ( pad , el ) ;
129
- }
110
+ // Avoid: Check if a break happens mid-element.
111
+ if ( rules . avoid && ! rules . before ) {
112
+ var startPage = Math . floor ( clientRect . top / pxPageHeight ) ;
113
+ var endPage = Math . floor ( clientRect . bottom / pxPageHeight ) ;
114
+ var nPages = Math . abs ( clientRect . bottom - clientRect . top ) / pxPageHeight ;
130
115
131
- // After: Create a padding div to fill the remaining page.
132
- if ( rules . after ) {
133
- var pad = createElement ( 'div' , { style : {
134
- display : 'block' ,
135
- height : pxPageHeight - ( clientRect . bottom % pxPageHeight ) + 'px'
136
- } } ) ;
137
- el . parentNode . insertBefore ( pad , el . nextSibling ) ;
116
+ // Turn on rules.before if the el is broken and is at most one page long.
117
+ if ( endPage !== startPage && nPages <= 1 ) {
118
+ rules . before = true ;
138
119
}
139
- } ) ;
140
-
141
- // Call the original onclone callback.
142
- oncloneOrig ( doc ) ;
120
+ }
121
+
122
+ // Before: Create a padding div to push the element to the next page.
123
+ if ( rules . before ) {
124
+ var pad = createElement ( 'div' , { style : {
125
+ display : 'block' ,
126
+ height : pxPageHeight - ( clientRect . top % pxPageHeight ) + 'px'
127
+ } } ) ;
128
+ el . parentNode . insertBefore ( pad , el ) ;
129
+ }
130
+
131
+ // After: Create a padding div to fill the remaining page.
132
+ if ( rules . after ) {
133
+ var pad = createElement ( 'div' , { style : {
134
+ display : 'block' ,
135
+ height : pxPageHeight - ( clientRect . bottom % pxPageHeight ) + 'px'
136
+ } } ) ;
137
+ el . parentNode . insertBefore ( pad , el . nextSibling ) ;
138
+ }
139
+ } ) ;
140
+
141
+ // Call the original onclone callback.
142
+ oncloneOrig ( doc ) ;
143
143
}
0 commit comments