Test link hrefs (#999)

* Add validatePath function

* Validate <link>s
This commit is contained in:
toasted-nutbread 2020-11-07 11:30:21 -05:00 committed by GitHub
parent f4325b7db9
commit ca26c648c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,15 +31,7 @@ function lstatSyncSafe(fileName) {
}
}
function validateHtmlScripts(fileName, extDir) {
const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
const dom = new JSDOM(domSource);
const {window} = dom;
const {document} = window;
try {
const scripts = document.querySelectorAll('script');
for (const script of scripts) {
const {src} = script;
function validatePath(src, fileName, extDir) {
assert.ok(typeof src === 'string', `<script> missing src attribute in ${fileName}`);
assert.ok(src.startsWith('/'), `<script> src attribute is not absolute in ${fileName} (src=${JSON.stringify(src)})`);
const relativeSrc = src.substring(1);
@ -49,6 +41,19 @@ function validateHtmlScripts(fileName, extDir) {
assert.ok(stats !== null, `<script> src file not found in ${fileName} (src=${JSON.stringify(src)})`);
assert.ok(stats.isFile(), `<script> src file invalid in ${fileName} (src=${JSON.stringify(src)})`);
}
function validateHtmlScripts(fileName, extDir) {
const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
const dom = new JSDOM(domSource);
const {window} = dom;
const {document} = window;
try {
for (const {src} of document.querySelectorAll('script')) {
validatePath(src, fileName, extDir);
}
for (const {href} of document.querySelectorAll('link')) {
validatePath(href, fileName, extDir);
}
} finally {
window.close();
}