Skip to content

Commit ea444ab

Browse files
author
Christoph Burgmer
committed
Refactor for readability
1 parent 0424de0 commit ea444ab

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

dist/rasterizeHTML.allinone.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rasterizeHTML.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@
164164

165165
var module = {};
166166

167+
var asArray = function (arrayLike) {
168+
return Array.prototype.slice.call(arrayLike);
169+
};
170+
167171
module.addClassNameRecursively = function (element, className) {
168172
element.className += ' ' + className;
169173

@@ -174,7 +178,7 @@
174178

175179
var changeCssRule = function (rule, newRuleText) {
176180
var styleSheet = rule.parentStyleSheet,
177-
ruleIdx = Array.prototype.indexOf.call(styleSheet.cssRules, rule);
181+
ruleIdx = asArray(styleSheet.cssRules).indexOf(rule);
178182

179183
// Exchange rule with the new text
180184
styleSheet.insertRule(newRuleText, ruleIdx+1);
@@ -189,7 +193,7 @@
189193
};
190194

191195
var cssRulesToText = function (cssRules) {
192-
return Array.prototype.reduce.call(cssRules, function (cssText, rule) {
196+
return asArray(cssRules).reduce(function (cssText, rule) {
193197
return cssText + rule.cssText;
194198
}, '');
195199
};
@@ -202,8 +206,8 @@
202206
// Assume that oldSelector is always prepended with a ':' or '.' for now, so no special handling needed
203207
var oldSelectorRegex = oldSelector + '(?=\\W|$)';
204208

205-
Array.prototype.forEach.call(doc.querySelectorAll('style'), function (styleElement) {
206-
var matchingRules = Array.prototype.filter.call(styleElement.sheet.cssRules, function (rule) {
209+
asArray(doc.querySelectorAll('style')).forEach(function (styleElement) {
210+
var matchingRules = asArray(styleElement.sheet.cssRules).filter(function (rule) {
207211
return rule.selectorText && new RegExp(oldSelectorRegex).test(rule.selectorText);
208212
});
209213

@@ -227,6 +231,10 @@
227231

228232
var module = {};
229233

234+
var asArray = function (arrayLike) {
235+
return Array.prototype.slice.call(arrayLike);
236+
};
237+
230238
module.fakeHover = function (doc, hoverSelector) {
231239
var elem = doc.querySelector(hoverSelector),
232240
fakeHoverClass = 'rasterizehtmlhover';
@@ -250,13 +258,13 @@
250258
};
251259

252260
module.persistInputValues = function (doc) {
253-
var inputs = Array.prototype.slice.call(doc.querySelectorAll('input')),
254-
textareas = Array.prototype.slice.call(doc.querySelectorAll('textarea')),
261+
var inputs = doc.querySelectorAll('input'),
262+
textareas = doc.querySelectorAll('textarea'),
255263
isCheckable = function (input) {
256264
return input.type === 'checkbox' || input.type === 'radio';
257265
};
258266

259-
inputs.filter(isCheckable)
267+
asArray(inputs).filter(isCheckable)
260268
.forEach(function (input) {
261269
if (input.checked) {
262270
input.setAttribute('checked', '');
@@ -265,12 +273,12 @@
265273
}
266274
});
267275

268-
inputs.filter(function (input) { return !isCheckable(input); })
276+
asArray(inputs).filter(function (input) { return !isCheckable(input); })
269277
.forEach(function (input) {
270278
input.setAttribute('value', input.value);
271279
});
272280

273-
textareas
281+
asArray(textareas)
274282
.forEach(function (textarea) {
275283
textarea.textContent = textarea.value;
276284
});
@@ -506,6 +514,10 @@
506514

507515
var module = {};
508516

517+
var asArray = function (arrayLike) {
518+
return Array.prototype.slice.call(arrayLike);
519+
};
520+
509521
var supportsBlobBuilding = function () {
510522
// Newer WebKit (under PhantomJS) seems to support blob building, but loading an image with the blob fails
511523
if (window.navigator.userAgent.indexOf("WebKit") >= 0 && window.navigator.userAgent.indexOf("Chrome") < 0) {
@@ -607,7 +619,7 @@
607619
// DOM. While this does not directly affect the process when rastering to canvas, this is needed for the
608620
// workaround found in workAroundBrowserBugForBackgroundImages();
609621
if (window.navigator.userAgent.indexOf("WebKit") >= 0) {
610-
Array.prototype.forEach.call(doc.getElementsByTagName("style"), function (style) {
622+
asArray(doc.getElementsByTagName("style")).forEach(function (style) {
611623
style.textContent = "span {}\n" + style.textContent;
612624
});
613625
}

dist/rasterizeHTML.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/documentHelper.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var documentHelper = (function (documentUtil) {
33

44
var module = {};
55

6+
var asArray = function (arrayLike) {
7+
return Array.prototype.slice.call(arrayLike);
8+
};
9+
610
module.fakeHover = function (doc, hoverSelector) {
711
var elem = doc.querySelector(hoverSelector),
812
fakeHoverClass = 'rasterizehtmlhover';
@@ -26,13 +30,13 @@ var documentHelper = (function (documentUtil) {
2630
};
2731

2832
module.persistInputValues = function (doc) {
29-
var inputs = Array.prototype.slice.call(doc.querySelectorAll('input')),
30-
textareas = Array.prototype.slice.call(doc.querySelectorAll('textarea')),
33+
var inputs = doc.querySelectorAll('input'),
34+
textareas = doc.querySelectorAll('textarea'),
3135
isCheckable = function (input) {
3236
return input.type === 'checkbox' || input.type === 'radio';
3337
};
3438

35-
inputs.filter(isCheckable)
39+
asArray(inputs).filter(isCheckable)
3640
.forEach(function (input) {
3741
if (input.checked) {
3842
input.setAttribute('checked', '');
@@ -41,12 +45,12 @@ var documentHelper = (function (documentUtil) {
4145
}
4246
});
4347

44-
inputs.filter(function (input) { return !isCheckable(input); })
48+
asArray(inputs).filter(function (input) { return !isCheckable(input); })
4549
.forEach(function (input) {
4650
input.setAttribute('value', input.value);
4751
});
4852

49-
textareas
53+
asArray(textareas)
5054
.forEach(function (textarea) {
5155
textarea.textContent = textarea.value;
5256
});

src/documentUtil.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var documentUtil = (function () {
33

44
var module = {};
55

6+
var asArray = function (arrayLike) {
7+
return Array.prototype.slice.call(arrayLike);
8+
};
9+
610
module.addClassNameRecursively = function (element, className) {
711
element.className += ' ' + className;
812

@@ -13,7 +17,7 @@ var documentUtil = (function () {
1317

1418
var changeCssRule = function (rule, newRuleText) {
1519
var styleSheet = rule.parentStyleSheet,
16-
ruleIdx = Array.prototype.indexOf.call(styleSheet.cssRules, rule);
20+
ruleIdx = asArray(styleSheet.cssRules).indexOf(rule);
1721

1822
// Exchange rule with the new text
1923
styleSheet.insertRule(newRuleText, ruleIdx+1);
@@ -28,7 +32,7 @@ var documentUtil = (function () {
2832
};
2933

3034
var cssRulesToText = function (cssRules) {
31-
return Array.prototype.reduce.call(cssRules, function (cssText, rule) {
35+
return asArray(cssRules).reduce(function (cssText, rule) {
3236
return cssText + rule.cssText;
3337
}, '');
3438
};
@@ -41,8 +45,8 @@ var documentUtil = (function () {
4145
// Assume that oldSelector is always prepended with a ':' or '.' for now, so no special handling needed
4246
var oldSelectorRegex = oldSelector + '(?=\\W|$)';
4347

44-
Array.prototype.forEach.call(doc.querySelectorAll('style'), function (styleElement) {
45-
var matchingRules = Array.prototype.filter.call(styleElement.sheet.cssRules, function (rule) {
48+
asArray(doc.querySelectorAll('style')).forEach(function (styleElement) {
49+
var matchingRules = asArray(styleElement.sheet.cssRules).filter(function (rule) {
4650
return rule.selectorText && new RegExp(oldSelectorRegex).test(rule.selectorText);
4751
});
4852

src/render.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var render = (function (util, browser, documentHelper, xmlserializer, ayepromise
33

44
var module = {};
55

6+
var asArray = function (arrayLike) {
7+
return Array.prototype.slice.call(arrayLike);
8+
};
9+
610
var supportsBlobBuilding = function () {
711
// Newer WebKit (under PhantomJS) seems to support blob building, but loading an image with the blob fails
812
if (window.navigator.userAgent.indexOf("WebKit") >= 0 && window.navigator.userAgent.indexOf("Chrome") < 0) {
@@ -104,7 +108,7 @@ var render = (function (util, browser, documentHelper, xmlserializer, ayepromise
104108
// DOM. While this does not directly affect the process when rastering to canvas, this is needed for the
105109
// workaround found in workAroundBrowserBugForBackgroundImages();
106110
if (window.navigator.userAgent.indexOf("WebKit") >= 0) {
107-
Array.prototype.forEach.call(doc.getElementsByTagName("style"), function (style) {
111+
asArray(doc.getElementsByTagName("style")).forEach(function (style) {
108112
style.textContent = "span {}\n" + style.textContent;
109113
});
110114
}

0 commit comments

Comments
 (0)