Skip to content

Commit

Permalink
add duplicate stylesheet filename test
Browse files Browse the repository at this point in the history
  • Loading branch information
smhg committed Jul 28, 2020
1 parent 1913400 commit f04706f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
31 changes: 19 additions & 12 deletions src/reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ const IMAGE_STYLES = [
{ selector: 'border', styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage'] }
];

const DEFAULT_OPTIONS = {
stylesheetReloadTimeout: 15000
};

class Reloader {
constructor (window, console, Timer) {
this.window = window;
Expand All @@ -145,12 +149,11 @@ class Reloader {
analyze (callback) {
}

reload (path, options) {
this.options = options; // avoid passing it through all the funcs

if (!this.options.stylesheetReloadTimeout) {
this.options.stylesheetReloadTimeout = 15000;
}
reload (path, options = {}) {
this.options = {
...DEFAULT_OPTIONS,
...options
}; // avoid passing it through all the funcs

if (this.options.pluginOrder && this.options.pluginOrder.length) {
this.runPluginsByOrder(path, options);
Expand Down Expand Up @@ -312,6 +315,8 @@ class Reloader {
}

reloadStylesheet (path) {
const options = this.options || DEFAULT_OPTIONS;

// has to be a real array, because DOMNodeList will be modified
let style;
let link;
Expand Down Expand Up @@ -365,7 +370,7 @@ class Reloader {
this.reattachStylesheetLink(match.object);
}
} else {
if (this.options.reloadMissingCSS) {
if (options.reloadMissingCSS) {
this.console.log(`LiveReload will reload all stylesheets because path '${path}' did not match any specific one. \
To disable this behavior, set 'options.reloadMissingCSS' to 'false'.`
);
Expand Down Expand Up @@ -411,6 +416,7 @@ and 'options.reloadMissingCSS' was set to 'false'.`
}

waitUntilCssLoads (clone, func) {
const options = this.options || DEFAULT_OPTIONS;
let callbackExecuted = false;

const executeCallback = () => {
Expand Down Expand Up @@ -448,7 +454,7 @@ and 'options.reloadMissingCSS' was set to 'false'.`
}

// fail safe
return this.Timer.start(this.options.stylesheetReloadTimeout, executeCallback);
return this.Timer.start(options.stylesheetReloadTimeout, executeCallback);
}

linkHref (link) {
Expand Down Expand Up @@ -490,7 +496,7 @@ and 'options.reloadMissingCSS' was set to 'false'.`
return this.waitUntilCssLoads(clone, () => {
let additionalWaitingTime;

if (/AppleWebKit/.test(navigator.userAgent)) {
if (/AppleWebKit/.test(this.window.navigator.userAgent)) {
additionalWaitingTime = 5;
} else {
additionalWaitingTime = 200;
Expand Down Expand Up @@ -567,6 +573,7 @@ and 'options.reloadMissingCSS' was set to 'false'.`
}

generateCacheBustUrl (url, expando) {
const options = this.options || DEFAULT_OPTIONS;
let hash, oldParams;

if (!expando) {
Expand All @@ -575,11 +582,11 @@ and 'options.reloadMissingCSS' was set to 'false'.`

({ url, hash, params: oldParams } = splitUrl(url));

if (this.options.overrideURL) {
if (url.indexOf(this.options.serverURL) < 0) {
if (options.overrideURL) {
if (url.indexOf(options.serverURL) < 0) {
const originalUrl = url;

url = this.options.serverURL + this.options.overrideURL + '?url=' + encodeURIComponent(url);
url = options.serverURL + options.overrideURL + '?url=' + encodeURIComponent(url);

this.console.log(`LiveReload is overriding source URL ${originalUrl} with ${url}`);
}
Expand Down
44 changes: 41 additions & 3 deletions test/reloader_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('pathFromUrl', () => {
});

describe('numberOfMatchingSegments', () => {
it('should', () => {
it('should count matching path parts', () => {
const res = numberOfMatchingSegments(
'/Users/abc/def/test.css',
pathFromUrl('https://www.example.com/abc/test.css')
Expand All @@ -83,11 +83,11 @@ describe('numberOfMatchingSegments', () => {
});

describe('pickBestMatch', () => {
it('should', () => {
it('should return the best match', () => {
const res = pickBestMatch(
'/xyz/abc/def/test.css',
[
'/abc/test.css',
'/abc/example.css',
'/abc/def/test.css'
]
);
Expand Down Expand Up @@ -398,4 +398,42 @@ describe('Reloader', () => {
}, 100);
});
});

describe('reloadStylesheet', done => {
it('should handle duplicate filenames', done => {
const dom = new JSDOM(`
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://localhost/abc/test.css">
<link rel="stylesheet" href="http://localhost/def/test.css">
</head>
<body></body>
</html>
`);

const cons = {
log () {
}
};

const reloader = new Reloader(
dom.window,
cons,
Timer
);

reloader.reattachStylesheetLink = function (link) {
const href = reloader.linkHref(link);

assert(href === 'http://localhost/def/test.css');

done();
};

reloader.reloadStylesheet(
'/def/test.css'
);
});
});
});

0 comments on commit f04706f

Please sign in to comment.