Skip to content

Commit df15e57

Browse files
author
Christoph Burgmer
committed
Refactor loadAndInlineStyles()
1 parent 95654d0 commit df15e57

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

dist/rasterizeHTML.allinone.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rasterizeHTML.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,37 @@ window.rasterizeHTMLInline = (function (module) {
8484
});
8585
};
8686

87+
var getArrayForArrayLike = function (list) {
88+
return Array.prototype.slice.call(list);
89+
};
90+
91+
var getCssStyleElements = function (doc) {
92+
var styles = getArrayForArrayLike(doc.getElementsByTagName("style")),
93+
cssStyles = [];
94+
95+
styles.forEach(function (style) {
96+
if (!style.attributes.type || style.attributes.type.nodeValue === "text/css") {
97+
cssStyles.push(style);
98+
}
99+
});
100+
101+
return cssStyles;
102+
};
103+
87104
module.loadAndInlineStyles = function (doc, options, callback) {
88105
var params = module.util.parseOptionalParameters(options, callback),
89-
styles = doc.getElementsByTagName("style"),
106+
styles = getCssStyleElements(doc),
90107
base = params.options.baseUrl || doc.baseURI,
91108
cache = params.options.cache !== false,
92109
allErrors = [],
93110
alreadyLoadedCssUrls = [];
94111

95112
module.util.map(styles, function (style, finish) {
96-
if (!style.attributes.type || style.attributes.type.nodeValue === "text/css") {
97-
loadAndInlineCssForStyle(style, base, cache, alreadyLoadedCssUrls, function (errors) {
98-
allErrors = allErrors.concat(errors);
113+
loadAndInlineCssForStyle(style, base, cache, alreadyLoadedCssUrls, function (errors) {
114+
allErrors = allErrors.concat(errors);
99115

100-
finish();
101-
});
102-
} else {
103-
// We need to properly deal with non-css in this concurrent context
104116
finish();
105-
}
117+
});
106118
}, function () {
107119
params.callback(allErrors);
108120
});

dist/rasterizeHTML.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/inline.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,37 @@ window.rasterizeHTMLInline = (function (module) {
8080
});
8181
};
8282

83+
var getArrayForArrayLike = function (list) {
84+
return Array.prototype.slice.call(list);
85+
};
86+
87+
var getCssStyleElements = function (doc) {
88+
var styles = getArrayForArrayLike(doc.getElementsByTagName("style")),
89+
cssStyles = [];
90+
91+
styles.forEach(function (style) {
92+
if (!style.attributes.type || style.attributes.type.nodeValue === "text/css") {
93+
cssStyles.push(style);
94+
}
95+
});
96+
97+
return cssStyles;
98+
};
99+
83100
module.loadAndInlineStyles = function (doc, options, callback) {
84101
var params = module.util.parseOptionalParameters(options, callback),
85-
styles = doc.getElementsByTagName("style"),
102+
styles = getCssStyleElements(doc),
86103
base = params.options.baseUrl || doc.baseURI,
87104
cache = params.options.cache !== false,
88105
allErrors = [],
89106
alreadyLoadedCssUrls = [];
90107

91108
module.util.map(styles, function (style, finish) {
92-
if (!style.attributes.type || style.attributes.type.nodeValue === "text/css") {
93-
loadAndInlineCssForStyle(style, base, cache, alreadyLoadedCssUrls, function (errors) {
94-
allErrors = allErrors.concat(errors);
109+
loadAndInlineCssForStyle(style, base, cache, alreadyLoadedCssUrls, function (errors) {
110+
allErrors = allErrors.concat(errors);
95111

96-
finish();
97-
});
98-
} else {
99-
// We need to properly deal with non-css in this concurrent context
100112
finish();
101-
}
113+
});
102114
}, function () {
103115
params.callback(allErrors);
104116
});

test/InlineStylesSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ describe("Import styles", function () {
7171
expect(loadAndInlineCSSResourcesForRulesSpy).toHaveBeenCalled();
7272
});
7373

74+
it("should ignore a style element with a non CSS type", function () {
75+
var styleNode = doc.createElement("style");
76+
styleNode.type = "text/plain";
77+
78+
styleNode.appendChild(doc.createTextNode('@import url("imported.css");'));
79+
doc.head.appendChild(styleNode);
80+
81+
rasterizeHTMLInline.loadAndInlineStyles(doc, callback);
82+
83+
expect(loadCSSImportsForRulesSpy).not.toHaveBeenCalled();
84+
expect(loadAndInlineCSSResourcesForRulesSpy).not.toHaveBeenCalled();
85+
});
86+
7487
it("should respect the document's baseURI", function () {
7588
doc = rasterizeHTMLTestHelper.readDocumentFixture("importCss.html");
7689

0 commit comments

Comments
 (0)