Skip to content

Commit 8c79ec2

Browse files
committed
Factors out code into separate functions
1 parent 64fa87d commit 8c79ec2

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

htmlpreview.js

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,33 @@
8181
// Get URL of the raw file
8282
const rawFileUrl = getRawFileUrl();
8383

84-
const replaceAssets = function () {
85-
let i, href, src;
86-
const links = [];
87-
const scripts = [];
88-
// Framesets
89-
if (document.querySelectorAll('frameset').length) {
90-
// Don't replace CSS/JS if it's a frameset,
91-
// because it will be erased by document.write()
92-
return;
93-
}
94-
// Frames
84+
const replaceFrames = function () {
9585
const frame = document.querySelectorAll('iframe[src],frame[src]');
96-
for (i = 0; i < frame.length; ++i) {
86+
for (let i = 0; i < frame.length; ++i) {
9787
rewriteCond(frame[i], "src");
9888
}
99-
// Objects
89+
};
90+
91+
const replaceObjects = function () {
10092
const object = document.querySelectorAll('object[data]');
101-
for (i = 0; i < object.length; ++i) {
93+
for (let i = 0; i < object.length; ++i) {
10294
rewriteCond(object[i], "data");
10395
}
104-
// Links
96+
};
97+
98+
const replaceLinks = function () {
10599
const a = document.querySelectorAll('a[href]');
106-
for (i = 0; i < a.length; ++i) {
100+
let href;
101+
for (let i = 0; i < a.length; ++i) {
107102
// Get absolute URL
108103
href = a[i].href;
109104
// Check if it's an anchor
110105
if (href.indexOf('#') > 0) {
111106
// Rewrite links to this document only
112107
if ((a[i].protocol + '//' + a[i].hostname + a[i].pathname) == rawFileUrl) {
113108
// Then rewrite URL with support for empty anchor
114-
a[i].href =
115-
location.origin + location.pathname + location.search
109+
a[i].href
110+
= location.origin + location.pathname + location.search
116111
+ '#' + a[i].hash.substring(1);
117112
}
118113
// Do not modify external URLs with fragment
@@ -121,9 +116,13 @@
121116
a[i].href = rewrite(href);
122117
}
123118
}
124-
// Stylesheets
119+
};
120+
121+
const replaceStylesheets = function () {
125122
const link = document.querySelectorAll('link[rel=stylesheet]');
126-
for (i = 0; i < link.length; ++i) {
123+
const links = [];
124+
let href;
125+
for (let i = 0; i < link.length; ++i) {
127126
// Get absolute URL
128127
href = link[i].href;
129128
if (isGitForgeFileUrl(href)) {
@@ -132,15 +131,18 @@
132131
}
133132
}
134133
Promise.all(links).then(function (res) {
135-
for (i = 0; i < res.length; ++i) {
134+
for (let i = 0; i < res.length; ++i) {
136135
loadCSS(res[i]);
137136
}
138137
});
139-
// Scripts
140-
const script = document.querySelectorAll(
141-
'script[type="text/htmlpreview"]'
142-
);
143-
for (i = 0; i < script.length; ++i) {
138+
};
139+
140+
const replaceScripts = function () {
141+
// eslint-disable-next-line max-len
142+
const script = document.querySelectorAll('script[type="text/htmlpreview"]');
143+
const scripts = [];
144+
let src;
145+
for (let i = 0; i < script.length; ++i) {
144146
// Get absolute URL
145147
src = script[i].src;
146148
if (isGitForgeFileUrl(src)) {
@@ -153,7 +155,7 @@
153155
}
154156
}
155157
Promise.all(scripts).then(function (res) {
156-
for (i = 0; i < res.length; ++i) {
158+
for (let i = 0; i < res.length; ++i) {
157159
loadJS(res[i]);
158160
}
159161
// Dispatch DOMContentLoaded event after loading all scripts
@@ -164,6 +166,20 @@
164166
});
165167
};
166168

169+
const replaceAssets = function () {
170+
// Framesets
171+
if (document.querySelectorAll('frameset').length) {
172+
// Don't replace CSS/JS if it's a frameset,
173+
// because it will be erased by document.write()
174+
return;
175+
}
176+
replaceFrames();
177+
replaceObjects();
178+
replaceLinks();
179+
replaceStylesheets();
180+
replaceScripts();
181+
};
182+
167183
const loadHTML = function (data) {
168184
if (data) {
169185
// Add <base> just after <head>

0 commit comments

Comments
 (0)