Skip to content
This repository was archived by the owner on Oct 13, 2018. It is now read-only.

#1379 fix #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 92 additions & 10 deletions dist/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,98 @@ sync.swapFile = function (elem, attr, options) {
var suffix = key + "=" + timeStamp;
var anchor = utils.getLocation(currentValue);
var search = sync.updateSearch(anchor.search, key, suffix);
var newValue = anchor.href;

if (options.timestamps === false) {
elem[attr] = anchor.href;
if (options.timestamps !== false) {
newValue = newValue.split("?")[0] + search;
}

if (elem.tagName === "LINK" && typeof elem.rel === "string" && elem.rel.toLowerCase() === "stylesheet") {
elem = sync.swapStyle(elem, newValue);
} else {
elem[attr] = anchor.href.split("?")[0] + search;
elem[attr] = newValue;
sync.triggerReflow();
}

return {
elem: elem,
timeStamp: timeStamp
};
};

/**
* @param link
* @param newHref
* @returns HTMLElement
*/
sync.swapStyle = function (link, newHref) {

var clone = link.cloneNode(false);
clone.href = newHref;

link.parentNode.insertBefore(clone, link.nextSibling);

sync.onLinkLoad(clone, function () {
if (link.parentNode) {
link.parentNode.removeChild(link);
}
});

return clone;
};

/**
* @param link
* @param onLoad
*/
sync.onLinkLoad = function (link, onLoad) {

var loaded = false;

function cb() {

if (loaded) {
return;
}

if (link.addEventListener) {
link.removeEventListener("load", cb);
}

loaded = true;
onLoad();
}

if (link.addEventListener) {
link.addEventListener("load", cb);
}

sync.onLinkDefined(link, cb);
};

/**
* @param link
* @param onLoad
*/
sync.onLinkDefined = function (link, onLoad) {

var sheets = document.styleSheets,
i = sheets.length;

while (i--) {
if (sheets[i].href === link.href) {
onLoad();
return;
}
}

setTimeout(function () {
sync.onLinkDefined(link, onLoad);
});
};

sync.triggerReflow = function() {

var body = document.body;

setTimeout(function () {
Expand All @@ -638,11 +723,6 @@ sync.swapFile = function (elem, attr, options) {
hiddenElem.style.display = "block";
}
}, 200);

return {
elem: elem,
timeStamp: timeStamp
};
};

sync.getFilenameOnly = function (url) {
Expand Down Expand Up @@ -727,12 +807,14 @@ sync.isBlacklisted = function (incoming) {
*/
sync.getMatches = function (elems, url, attr) {

if (url[0] === "*") {
var isExtPattern = /^\*\.[a-z\d]+$/i.test(url);

if (!isExtPattern && url[0] === "*") {
return elems;
}

var matches = [];
var urlMatcher = new RegExp("(^|/)" + url);
var urlMatcher = new RegExp(isExtPattern ? ("\\" + url.replace("*", "") + "(\\?rel=.+)?$") : ("(^|/)" + url));

for (var i = 0, len = elems.length; i < len; i += 1) {
if (urlMatcher.test(elems[i][attr])) {
Expand Down
Loading