UNPKG

1.55 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var path_1 = require("path");
3var filesystem_1 = require("../filesystem");
4var exception_1 = require("../exception");
5exports.inlineStylesheets = function (path, document) {
6 try {
7 var links = Array.from(document.querySelectorAll('link[rel="stylesheet"]'));
8 for (var _i = 0, links_1 = links; _i < links_1.length; _i++) {
9 var link = links_1[_i];
10 var resource = readResource(window.document, path, link);
11 if (resource == null) {
12 continue;
13 }
14 link.parentElement.replaceChild(resource, link);
15 }
16 return window.document.documentElement.outerHTML;
17 }
18 catch (exception) {
19 throw new exception_1.RuntimeException('Failed to inline stylesheet resources', exception);
20 }
21};
22var readResource = function (document, path, link) {
23 if (link.href == null || link.href.length === 0) {
24 return null;
25 }
26 var href = Array.from(link.attributes).find(function (a) { return a.localName.toLowerCase() === 'href'; });
27 if (href == null) {
28 return null;
29 }
30 var file = filesystem_1.fileFromString(path_1.join(path.toString(), href.value));
31 if (file.exists() === false) {
32 return null;
33 }
34 var element = document.createElement('style');
35 element.setAttribute('type', 'text/css');
36 element.setAttribute('media', link.media);
37 element.textContent = file.content();
38 return element;
39};
40//# sourceMappingURL=stylesheets.js.map
\No newline at end of file