mirror of
https://github.com/bigskysoftware/htmx.git
synced 2026-04-26 05:42:31 +00:00
Avoid throwing errors when parsing malformed JSON
Instead, treat it as if no value was present.
This commit is contained in:
18
src/htmx.js
18
src/htmx.js
@@ -184,6 +184,16 @@ return (function () {
|
||||
return obj1;
|
||||
}
|
||||
|
||||
function parseJSON(jString) {
|
||||
try {
|
||||
return JSON.parse(jString);
|
||||
} catch(error) {
|
||||
console.warn('Failed to parse JSON string `' + jString +'`:');
|
||||
console.warn(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================================
|
||||
// public API
|
||||
//==========================================================================================
|
||||
@@ -525,7 +535,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];
|
||||
@@ -977,7 +987,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);
|
||||
@@ -992,7 +1002,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];
|
||||
@@ -1552,7 +1562,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;
|
||||
}
|
||||
|
||||
@@ -131,4 +131,17 @@ describe("hx-push-url attribute", function() {
|
||||
cache.length.should.equal(1);
|
||||
});
|
||||
|
||||
});
|
||||
it("deals with malformed JSON in history cache when getting", function () {
|
||||
localStorage.setItem(KUTTY_HISTORY_CACHE, "Invalid JSON");
|
||||
var history = htmx._('getCachedHistory')('url');
|
||||
should.equal(history, null);
|
||||
});
|
||||
|
||||
it("deals with malformed JSON in history cache when saving", function () {
|
||||
localStorage.setItem(KUTTY_HISTORY_CACHE, "Invalid JSON");
|
||||
htmx._('saveToHistoryCache')('url', 'content', 'title', 'scroll');
|
||||
var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE));
|
||||
cache.length.should.equal(1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user