tests + dist

This commit is contained in:
carson 2020-06-17 15:34:09 -07:00
parent c1a76c7af9
commit 38e2a2fe76
7 changed files with 64 additions and 15 deletions

17
dist/htmx.js vendored
View File

@ -187,6 +187,15 @@ return (function () {
return obj1;
}
function parseJSON(jString) {
try {
return JSON.parse(jString);
} catch(error) {
logError(error);
return null;
}
}
//==========================================================================================
// public API
//==========================================================================================
@ -533,7 +542,7 @@ return (function () {
function handleTrigger(elt, trigger) {
if (trigger) {
if (trigger.indexOf("{") === 0) {
var triggers = JSON.parse(trigger);
var triggers = parseJSON(trigger);
for (var eventName in triggers) {
if (triggers.hasOwnProperty(eventName)) {
var detail = triggers[eventName];
@ -1013,7 +1022,7 @@ return (function () {
}
function saveToHistoryCache(url, content, title, scroll) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
historyCache = historyCache.slice(i, 1);
@ -1028,7 +1037,7 @@ return (function () {
}
function getCachedHistory(url) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
return historyCache[i];
@ -1608,7 +1617,7 @@ return (function () {
function getMetaConfig() {
var element = getDocument().querySelector('meta[name="htmx-config"]');
if (element) {
return JSON.parse(element.content);
return parseJSON(element.content);
} else {
return null;
}

2
dist/htmx.min.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/htmx.min.js.gz vendored

Binary file not shown.

View File

@ -187,6 +187,15 @@ return (function () {
return obj1;
}
function parseJSON(jString) {
try {
return JSON.parse(jString);
} catch(error) {
logError(error);
return null;
}
}
//==========================================================================================
// public API
//==========================================================================================
@ -533,7 +542,7 @@ return (function () {
function handleTrigger(elt, trigger) {
if (trigger) {
if (trigger.indexOf("{") === 0) {
var triggers = JSON.parse(trigger);
var triggers = parseJSON(trigger);
for (var eventName in triggers) {
if (triggers.hasOwnProperty(eventName)) {
var detail = triggers[eventName];
@ -1013,7 +1022,7 @@ return (function () {
}
function saveToHistoryCache(url, content, title, scroll) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
historyCache = historyCache.slice(i, 1);
@ -1028,7 +1037,7 @@ return (function () {
}
function getCachedHistory(url) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
return historyCache[i];
@ -1608,7 +1617,7 @@ return (function () {
function getMetaConfig() {
var element = getDocument().querySelector('meta[name="htmx-config"]');
if (element) {
return JSON.parse(element.content);
return parseJSON(element.content);
} else {
return null;
}

View File

@ -187,6 +187,15 @@ return (function () {
return obj1;
}
function parseJSON(jString) {
try {
return JSON.parse(jString);
} catch(error) {
logError(error);
return null;
}
}
//==========================================================================================
// public API
//==========================================================================================
@ -533,7 +542,7 @@ return (function () {
function handleTrigger(elt, trigger) {
if (trigger) {
if (trigger.indexOf("{") === 0) {
var triggers = JSON.parse(trigger);
var triggers = parseJSON(trigger);
for (var eventName in triggers) {
if (triggers.hasOwnProperty(eventName)) {
var detail = triggers[eventName];
@ -1013,7 +1022,7 @@ return (function () {
}
function saveToHistoryCache(url, content, title, scroll) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
historyCache = historyCache.slice(i, 1);
@ -1028,7 +1037,7 @@ return (function () {
}
function getCachedHistory(url) {
var historyCache = JSON.parse(localStorage.getItem("htmx-history-cache")) || [];
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
for (var i = 0; i < historyCache.length; i++) {
if (historyCache[i].url === url) {
return historyCache[i];
@ -1608,7 +1617,7 @@ return (function () {
function getMetaConfig() {
var element = getDocument().querySelector('meta[name="htmx-config"]');
if (element) {
return JSON.parse(element.content);
return parseJSON(element.content);
} else {
return null;
}

View File

@ -98,6 +98,20 @@ describe("hx-push-url attribute", function() {
cache.length.should.equal(1);
});
it("deals with malformed JSON in history cache when getting", function () {
localStorage.setItem(HTMX_HISTORY_CACHE_NAME, "Invalid JSON");
var history = htmx._('getCachedHistory')('url');
should.equal(history, null);
});
it("deals with malformed JSON in history cache when saving", function () {
localStorage.setItem(HTMX_HISTORY_CACHE_NAME, "Invalid JSON");
htmx._('saveToHistoryCache')('url', 'content', 'title', 'scroll');
var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE));
cache.length.should.equal(1);
});
it("afterSettle.htmx is called when replacing outerHTML", function () {
var called = false;
var handler = htmx.on("afterSettle.htmx", function (evt) {
@ -135,4 +149,4 @@ describe("hx-push-url attribute", function() {
}
});
});
});

View File

@ -107,4 +107,12 @@ describe("Core htmx AJAX headers", function() {
invokedEvent.should.equal(true);
})
});
it("should survive malformed JSON in HX-Trigger response header", function(){
this.server.respondWith("GET", "/test", [200, {"HX-Trigger" : "{not: valid}"}, ""]);
var div = make('<div hx-get="/test"></div>');
div.click();
this.server.respond();
})
});