diff --git a/dist/htmx.js b/dist/htmx.js index a9792d17..c4487cbd 100644 --- a/dist/htmx.js +++ b/dist/htmx.js @@ -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; } diff --git a/dist/htmx.min.js b/dist/htmx.min.js index 3bab9c22..060d1d5c 100644 --- a/dist/htmx.min.js +++ b/dist/htmx.min.js @@ -1 +1 @@ -(function(e,t){if(typeof define==="function"&&define.amd){define([],t)}else{e.htmx=t()}})(typeof self!=="undefined"?self:this,function(){return function(){"use strict";var t=["get","post","put","delete","patch"];var r=t.map(function(e){return"[hx-"+e+"], [data-hx-"+e+"]"}).join(", ");function a(e){if(e==null||e==="null"||e==="false"||e===""){return null}else if(e.lastIndexOf("ms")===e.length-2){return parseFloat(e.substr(0,e.length-2))}else if(e.lastIndexOf("s")===e.length-1){return parseFloat(e.substr(0,e.length-1))*1e3}else{return parseFloat(e)}}function u(e,t){return e.getAttribute&&e.getAttribute(t)}function l(e,t){return e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function w(e,t){return u(e,t)||u(e,"data-"+t)}function o(e){return e.parentElement}function E(){return document}function s(e,t){if(t(e)){return e}else if(o(e)){return s(o(e),t)}else{return null}}function L(e,t){var r=null;s(e,function(e){return r=w(e,t)});return r}function f(e,t){var r=e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector;return r&&r.call(e,t)}function n(e){var t=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i;var r=t.exec(e);if(r){return r[1].toLowerCase()}else{return""}}function i(e,t){var r=new DOMParser;var n=r.parseFromString(e,"text/html");var i=n.body;while(t>0){t--;i=i.firstChild}if(i==null){i=E().createDocumentFragment()}return i}function c(e){var t=n(e);switch(t){case"thead":case"tbody":case"tfoot":case"colgroup":case"caption":return i(""+e+"
",1);case"col":return i(""+e+"
",2);case"tr":return i(""+e+"
",2);case"td":case"th":return i(""+e+"
",3);default:return i(e,0)}}function h(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function v(e){return h(e,"Function")}function d(e){return h(e,"Object")}function O(e){var t="htmx-internal-data";var r=e[t];if(!r){r=e[t]={}}return r}function m(e){var t=[];if(e){for(var r=0;r=0}function A(e){return E().body.contains(e)}function p(e){return e.split(/\s+/)}function T(e,t){for(var r in t){if(t.hasOwnProperty(r)){e[r]=t[r]}}return e}function e(e){return eval(e)}function y(t){var e=htmx.on("load.htmx",function(e){t(e.detail.elt)});return e}function x(){htmx.logger=function(e,t,r){if(console){console.log(t,e,r)}}}function b(e,t){if(t){return e.querySelector(t)}else{return E().body.querySelector(e)}}function S(e,t){if(t){return e.querySelectorAll(t)}else{return E().body.querySelectorAll(e)}}function q(e,t){if(t){setTimeout(function(){q(e)},t)}else{e.parentElement.removeChild(e)}}function H(e,t,r){if(r){setTimeout(function(){H(e,t)},r)}else{e.classList.add(t)}}function N(e,t,r){if(r){setTimeout(function(){N(e,t)},r)}else{e.classList.remove(t)}}function R(e,t){e.classList.toggle(t)}function k(e,t){C(e.parentElement.children,function(e){N(e,t)});H(e,t)}function D(e,t){do{if(e==null||f(e,t))return e}while(e=e&&o(e))}function I(e,t,r){if(v(t)){return{target:E().body,event:e,listener:t}}else{return{target:e,event:t,listener:r}}}function M(t,r,n){ft(function(){var e=I(t,r,n);e.target.addEventListener(e.event,e.listener)});var e=v(r);return e?r:n}function X(t,r,n){ft(function(){var e=I(t,r,n);e.target.removeEventListener(e.event,e.listener)});return v(r)?r:n}function F(e){var t=s(e,function(e){return w(e,"hx-target")!==null});if(t){var r=w(t,"hx-target");if(r==="this"){return t}else if(r.indexOf("closest ")===0){return D(e,r.substr(8))}else{return E().querySelector(r)}}else{var n=O(e);if(n.boosted){return E().body}else{return e}}}function P(t,r){C(t.attributes,function(e){if(!r.hasAttribute(e.name)){t.removeAttribute(e.name)}});C(r.attributes,function(e){t.setAttribute(e.name,e.value)})}function j(e,t){var r=st(t);for(var n=0;n0){var t=n.querySelector(e.tagName+"[id="+e.id+"]");if(t&&t!==n){var r=e.cloneNode();P(e,t);i.tasks.push(function(){P(e,r)})}}})}function z(e){return function(){Ae(e);Le(e);ke(e,"load.htmx",{})}}function V(e,t,r,n){U(e,r,n);while(r.childNodes.length>0){var i=r.firstChild;e.insertBefore(i,t);if(i.nodeType!==Node.TEXT_NODE){n.tasks.push(z(i))}}}function W(e){var t=O(e);if(t.webSocket){t.webSocket.close()}if(t.sseEventSource){t.sseEventSource.close()}if(e.children){C(e.children,function(e){W(e)})}}function _(e,t,r){if(e.tagName==="BODY"){return Z(e,t)}else{var n=e.previousSibling;V(o(e),e,t,r);if(n==null){var i=o(e).firstChild}else{var i=n.nextSibling}while(i&&i!=e){r.elts.push(i);i=i.nextSibling}W(e);o(e).removeChild(e)}}function G(e,t,r){return V(e,e.firstChild,t,r)}function Y(e,t,r){return V(o(e),e,t,r)}function K(e,t,r){return V(e,null,t,r)}function Q(e,t,r){return V(o(e),e.nextSibling,t,r)}function Z(e,t,r){var n=e.firstChild;V(e,n,t,r);if(n){while(n.nextSibling){e.removeChild(n.nextSibling)}e.removeChild(n)}}function $(e,t){var r=L(e,"hx-select");if(r){var n=E().createDocumentFragment();C(t.querySelectorAll(r),function(e){n.appendChild(e)});t=n}return t}function ee(e,t,r,n,i){switch(e){case"none":return;case"outerHTML":_(r,n,i);return;case"afterbegin":G(r,n,i);return;case"beforebegin":Y(r,n,i);return;case"beforeend":K(r,n,i);return;case"afterend":Q(r,n,i);return;default:var o=st(t);for(var a=0;ahtmx.config.historyCacheSize){i.shift()}localStorage.setItem("htmx-history-cache",JSON.stringify(i))}function Xe(e){var t=JSON.parse(localStorage.getItem("htmx-history-cache"))||[];for(var r=0;r=200&&this.status<400){ke(E().body,"historyCacheMissLoad.htmx",i);var e=c(this.response);e=e.querySelector("[hx-history-elt],[data-hx-history-elt]")||e;var t=Ie();var r=nt(t);Z(t,e,r);je(r.tasks);De=n}else{qe(E().body,"historyCacheMissLoadError.htmx",i)}};e.send()}function Be(e){Fe(De);e=e||location.pathname+location.search;ke(E().body,"historyRestore.htmx",{path:e});var t=Xe(e);if(t){var r=c(t.content);var n=Ie();var i=nt(n);Z(n,r,i);je(i.tasks);document.title=t.title;window.scrollTo(0,t.scroll);De=e}else{Je(e)}}function Ue(e){return L(e,"hx-push-url")==="true"||e.tagName==="A"&&O(e).boosted}function ze(e){We(e,"add")}function Ve(e){We(e,"remove")}function We(e,t){var r=L(e,"hx-indicator");if(r){var n=E().querySelectorAll(r)}else{n=[e]}C(n,function(e){e.classList[t].call(e.classList,"htmx-request")})}function _e(e,t){for(var r=0;r0){r["swapStyle"]=n[0];for(var i=1;i=200&&this.status<400){if(this.status===286){ie(s)}if(this.status!==204){if(!ke(c,"beforeSwap.htmx",S))return;var u=this.response;Ne(s,function(e){u=e.transformResponse(u,h,s)});if(a){Fe()}var l=tt(s);c.classList.add("htmx-swapping");var e=function(){try{var e=document.activeElement;var t={elt:e,start:e.selectionStart,end:e.selectionEnd};var r=nt(c);te(l.swapStyle,c,s,u,r);if(!A(t.elt)&&t.elt.id){var n=document.getElementById(t.elt.id);if(t.start&&n.setSelectionRange){n.setSelectionRange(t.start,t.end)}n.focus()}c.classList.remove("htmx-swapping");C(r.elts,function(e){if(e.classList){e.classList.add("htmx-settling")}ke(e,"afterSwap.htmx",S)});if(p){location.hash=p}var i=function(){C(r.tasks,function(e){e.call()});C(r.elts,function(e){if(e.classList){e.classList.remove("htmx-settling")}ke(e,"afterSettle.htmx",S)});if(a){var e=o||y||f;Pe(e);ke(E().body,"pushedIntoHistory.htmx",{path:e})}};if(l.settleDelay>0){setTimeout(i,l.settleDelay)}else{i()}}catch(e){qe(s,"swapError.htmx",S);throw e}};if(l.swapDelay>0){setTimeout(e,l.swapDelay)}else{e()}}}else{qe(s,"responseError.htmx",T({error:"Response Status Error Code "+this.status+" from "+f},S))}}catch(e){qe(s,"onLoadError.htmx",T({error:e},S));throw e}finally{Ve(s);ke(s,"afterRequest.htmx",S);ke(s,"afterOnLoad.htmx",S);n()}};h.onerror=function(){Ve(s);qe(s,"afterRequest.htmx",S);qe(s,"sendError.htmx",S);n()};if(!ke(s,"beforeRequest.htmx",S))return n();ze(s);h.send(e==="get"?null:rt(h,s,v))}var ot={};function at(){return{onEvent:function(e,t){return true},transformResponse:function(e,t,r){return e},isInlineSwap:function(e){return false},handleSwap:function(e,t,r,n){return false},encodeParameters:function(e,t,r){return null}}}function ut(e,t){ot[e]=T(at(),t)}function lt(e){delete ot[e]}function st(e,r){if(e==null){return r}if(r==null){r=[]}var t=w(e,"hx-ext");if(t){C(t.split(","),function(e){e=e.replace(/ /g,"");var t=ot[e];if(t&&r.indexOf(t)<0){r.push(t)}})}return st(o(e),r)}function ft(e){if(E().readyState!=="loading"){e()}else{E().addEventListener("DOMContentLoaded",e)}}(function(){var e=ct();if(e===null||e.includeIndicatorStyles!==false){E().head.insertAdjacentHTML("beforeend","")}})();function ct(){var e=E().querySelector('meta[name="htmx-config"]');if(e){return JSON.parse(e.content)}else{return null}}function ht(){var e=ct();if(e){htmx.config=T(htmx.config,e)}}ft(function(){ht();var e=E().body;Ae(e,true);ke(e,"load.htmx",{});window.onpopstate=function(){Be()}});return{onLoad:y,process:Ae,on:M,off:X,trigger:ke,find:b,findAll:S,closest:D,remove:q,addClass:H,removeClass:N,toggleClass:R,takeClass:k,defineExtension:ut,removeExtension:lt,logAll:x,logger:null,config:{historyEnabled:true,historyCacheSize:10,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:100,includeIndicatorStyles:true},parseInterval:a,_:e,createEventSource:function(e){return new EventSource(e,{withCredentials:true})},createWebSocket:function(e){return new WebSocket(e,[])}}}()}); \ No newline at end of file +(function(e,t){if(typeof define==="function"&&define.amd){define([],t)}else{e.htmx=t()}})(typeof self!=="undefined"?self:this,function(){return function(){"use strict";var t=["get","post","put","delete","patch"];var r=t.map(function(e){return"[hx-"+e+"], [data-hx-"+e+"]"}).join(", ");function a(e){if(e==null||e==="null"||e==="false"||e===""){return null}else if(e.lastIndexOf("ms")===e.length-2){return parseFloat(e.substr(0,e.length-2))}else if(e.lastIndexOf("s")===e.length-1){return parseFloat(e.substr(0,e.length-1))*1e3}else{return parseFloat(e)}}function u(e,t){return e.getAttribute&&e.getAttribute(t)}function l(e,t){return e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function w(e,t){return u(e,t)||u(e,"data-"+t)}function o(e){return e.parentElement}function E(){return document}function s(e,t){if(t(e)){return e}else if(o(e)){return s(o(e),t)}else{return null}}function L(e,t){var r=null;s(e,function(e){return r=w(e,t)});return r}function f(e,t){var r=e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector;return r&&r.call(e,t)}function n(e){var t=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i;var r=t.exec(e);if(r){return r[1].toLowerCase()}else{return""}}function i(e,t){var r=new DOMParser;var n=r.parseFromString(e,"text/html");var i=n.body;while(t>0){t--;i=i.firstChild}if(i==null){i=E().createDocumentFragment()}return i}function c(e){var t=n(e);switch(t){case"thead":case"tbody":case"tfoot":case"colgroup":case"caption":return i(""+e+"
",1);case"col":return i(""+e+"
",2);case"tr":return i(""+e+"
",2);case"td":case"th":return i(""+e+"
",3);default:return i(e,0)}}function h(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function v(e){return h(e,"Function")}function d(e){return h(e,"Object")}function C(e){var t="htmx-internal-data";var r=e[t];if(!r){r=e[t]={}}return r}function m(e){var t=[];if(e){for(var r=0;r=0}function A(e){return E().body.contains(e)}function p(e){return e.split(/\s+/)}function T(e,t){for(var r in t){if(t.hasOwnProperty(r)){e[r]=t[r]}}return e}function y(e){try{return JSON.parse(e)}catch(e){Ne(e);return null}}function e(e){return eval(e)}function x(t){var e=htmx.on("load.htmx",function(e){t(e.detail.elt)});return e}function b(){htmx.logger=function(e,t,r){if(console){console.log(t,e,r)}}}function S(e,t){if(t){return e.querySelector(t)}else{return E().body.querySelector(e)}}function q(e,t){if(t){return e.querySelectorAll(t)}else{return E().body.querySelectorAll(e)}}function H(e,t){if(t){setTimeout(function(){H(e)},t)}else{e.parentElement.removeChild(e)}}function R(e,t,r){if(r){setTimeout(function(){R(e,t)},r)}else{e.classList.add(t)}}function k(e,t,r){if(r){setTimeout(function(){k(e,t)},r)}else{e.classList.remove(t)}}function N(e,t){e.classList.toggle(t)}function D(e,t){O(e.parentElement.children,function(e){k(e,t)});R(e,t)}function I(e,t){do{if(e==null||f(e,t))return e}while(e=e&&o(e))}function M(e,t,r){if(v(t)){return{target:E().body,event:e,listener:t}}else{return{target:e,event:t,listener:r}}}function X(t,r,n){ct(function(){var e=M(t,r,n);e.target.addEventListener(e.event,e.listener)});var e=v(r);return e?r:n}function F(t,r,n){ct(function(){var e=M(t,r,n);e.target.removeEventListener(e.event,e.listener)});return v(r)?r:n}function P(e){var t=s(e,function(e){return w(e,"hx-target")!==null});if(t){var r=w(t,"hx-target");if(r==="this"){return t}else if(r.indexOf("closest ")===0){return I(e,r.substr(8))}else{return E().querySelector(r)}}else{var n=C(e);if(n.boosted){return E().body}else{return e}}}function j(t,r){O(t.attributes,function(e){if(!r.hasAttribute(e.name)){t.removeAttribute(e.name)}});O(r.attributes,function(e){t.setAttribute(e.name,e.value)})}function B(e,t){var r=ft(t);for(var n=0;n0){var t=n.querySelector(e.tagName+"[id="+e.id+"]");if(t&&t!==n){var r=e.cloneNode();j(e,t);i.tasks.push(function(){j(e,r)})}}})}function W(e){return function(){Te(e);Ce(e);De(e,"load.htmx",{})}}function _(e,t,r,n){V(e,r,n);while(r.childNodes.length>0){var i=r.firstChild;e.insertBefore(i,t);if(i.nodeType!==Node.TEXT_NODE){n.tasks.push(W(i))}}}function J(e){var t=C(e);if(t.webSocket){t.webSocket.close()}if(t.sseEventSource){t.sseEventSource.close()}if(e.children){O(e.children,function(e){J(e)})}}function G(e,t,r){if(e.tagName==="BODY"){return $(e,t)}else{var n=e.previousSibling;_(o(e),e,t,r);if(n==null){var i=o(e).firstChild}else{var i=n.nextSibling}while(i&&i!=e){r.elts.push(i);i=i.nextSibling}J(e);o(e).removeChild(e)}}function Y(e,t,r){return _(e,e.firstChild,t,r)}function K(e,t,r){return _(o(e),e,t,r)}function Q(e,t,r){return _(e,null,t,r)}function Z(e,t,r){return _(o(e),e.nextSibling,t,r)}function $(e,t,r){var n=e.firstChild;_(e,n,t,r);if(n){while(n.nextSibling){e.removeChild(n.nextSibling)}e.removeChild(n)}}function ee(e,t){var r=L(e,"hx-select");if(r){var n=E().createDocumentFragment();O(t.querySelectorAll(r),function(e){n.appendChild(e)});t=n}return t}function te(e,t,r,n,i){switch(e){case"none":return;case"outerHTML":G(r,n,i);return;case"afterbegin":Y(r,n,i);return;case"beforebegin":K(r,n,i);return;case"beforeend":Q(r,n,i);return;case"afterend":Z(r,n,i);return;default:var o=ft(t);for(var a=0;ahtmx.config.historyCacheSize){i.shift()}localStorage.setItem("htmx-history-cache",JSON.stringify(i))}function Fe(e){var t=y(localStorage.getItem("htmx-history-cache"))||[];for(var r=0;r=200&&this.status<400){De(E().body,"historyCacheMissLoad.htmx",i);var e=c(this.response);e=e.querySelector("[hx-history-elt],[data-hx-history-elt]")||e;var t=Me();var r=it(t);$(t,e,r);Be(r.tasks);Ie=n}else{He(E().body,"historyCacheMissLoadError.htmx",i)}};e.send()}function ze(e){Pe(Ie);e=e||location.pathname+location.search;De(E().body,"historyRestore.htmx",{path:e});var t=Fe(e);if(t){var r=c(t.content);var n=Me();var i=it(n);$(n,r,i);Be(i.tasks);document.title=t.title;window.scrollTo(0,t.scroll);Ie=e}else{Ue(e)}}function Ve(e){return L(e,"hx-push-url")==="true"||e.tagName==="A"&&C(e).boosted}function We(e){Je(e,"add")}function _e(e){Je(e,"remove")}function Je(e,t){var r=L(e,"hx-indicator");if(r){var n=E().querySelectorAll(r)}else{n=[e]}O(n,function(e){e.classList[t].call(e.classList,"htmx-request")})}function Ge(e,t){for(var r=0;r0){r["swapStyle"]=n[0];for(var i=1;i=200&&this.status<400){if(this.status===286){oe(s)}if(this.status!==204){if(!De(c,"beforeSwap.htmx",S))return;var u=this.response;ke(s,function(e){u=e.transformResponse(u,h,s)});if(a){Pe()}var l=rt(s);c.classList.add("htmx-swapping");var e=function(){try{var e=document.activeElement;var t={elt:e,start:e.selectionStart,end:e.selectionEnd};var r=it(c);re(l.swapStyle,c,s,u,r);if(!A(t.elt)&&t.elt.id){var n=document.getElementById(t.elt.id);if(t.start&&n.setSelectionRange){n.setSelectionRange(t.start,t.end)}n.focus()}c.classList.remove("htmx-swapping");O(r.elts,function(e){if(e.classList){e.classList.add("htmx-settling")}De(e,"afterSwap.htmx",S)});if(p){location.hash=p}var i=function(){O(r.tasks,function(e){e.call()});O(r.elts,function(e){if(e.classList){e.classList.remove("htmx-settling")}De(e,"afterSettle.htmx",S)});if(a){var e=o||y||f;je(e);De(E().body,"pushedIntoHistory.htmx",{path:e})}};if(l.settleDelay>0){setTimeout(i,l.settleDelay)}else{i()}}catch(e){He(s,"swapError.htmx",S);throw e}};if(l.swapDelay>0){setTimeout(e,l.swapDelay)}else{e()}}}else{He(s,"responseError.htmx",T({error:"Response Status Error Code "+this.status+" from "+f},S))}}catch(e){He(s,"onLoadError.htmx",T({error:e},S));throw e}finally{_e(s);De(s,"afterRequest.htmx",S);De(s,"afterOnLoad.htmx",S);n()}};h.onerror=function(){_e(s);He(s,"afterRequest.htmx",S);He(s,"sendError.htmx",S);n()};if(!De(s,"beforeRequest.htmx",S))return n();We(s);h.send(e==="get"?null:nt(h,s,v))}var at={};function ut(){return{onEvent:function(e,t){return true},transformResponse:function(e,t,r){return e},isInlineSwap:function(e){return false},handleSwap:function(e,t,r,n){return false},encodeParameters:function(e,t,r){return null}}}function lt(e,t){at[e]=T(ut(),t)}function st(e){delete at[e]}function ft(e,r){if(e==null){return r}if(r==null){r=[]}var t=w(e,"hx-ext");if(t){O(t.split(","),function(e){e=e.replace(/ /g,"");var t=at[e];if(t&&r.indexOf(t)<0){r.push(t)}})}return ft(o(e),r)}function ct(e){if(E().readyState!=="loading"){e()}else{E().addEventListener("DOMContentLoaded",e)}}(function(){var e=ht();if(e===null||e.includeIndicatorStyles!==false){E().head.insertAdjacentHTML("beforeend","")}})();function ht(){var e=E().querySelector('meta[name="htmx-config"]');if(e){return y(e.content)}else{return null}}function vt(){var e=ht();if(e){htmx.config=T(htmx.config,e)}}ct(function(){vt();var e=E().body;Te(e,true);De(e,"load.htmx",{});window.onpopstate=function(){ze()}});return{onLoad:x,process:Te,on:X,off:F,trigger:De,find:S,findAll:q,closest:I,remove:H,addClass:R,removeClass:k,toggleClass:N,takeClass:D,defineExtension:lt,removeExtension:st,logAll:b,logger:null,config:{historyEnabled:true,historyCacheSize:10,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:100,includeIndicatorStyles:true},parseInterval:a,_:e,createEventSource:function(e){return new EventSource(e,{withCredentials:true})},createWebSocket:function(e){return new WebSocket(e,[])}}}()}); \ No newline at end of file diff --git a/dist/htmx.min.js.gz b/dist/htmx.min.js.gz index 062fa521..ed460236 100644 Binary files a/dist/htmx.min.js.gz and b/dist/htmx.min.js.gz differ diff --git a/www/js/htmx.js b/www/js/htmx.js index a9792d17..c4487cbd 100644 --- a/www/js/htmx.js +++ b/www/js/htmx.js @@ -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; } diff --git a/www/test/0.0.5/src/htmx.js b/www/test/0.0.5/src/htmx.js index a9792d17..c4487cbd 100644 --- a/www/test/0.0.5/src/htmx.js +++ b/www/test/0.0.5/src/htmx.js @@ -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; } diff --git a/www/test/0.0.5/test/attributes/hx-push-url.js b/www/test/0.0.5/test/attributes/hx-push-url.js index d1f6b137..1ff0e595 100644 --- a/www/test/0.0.5/test/attributes/hx-push-url.js +++ b/www/test/0.0.5/test/attributes/hx-push-url.js @@ -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() { } }); -}); \ No newline at end of file +}); diff --git a/www/test/0.0.5/test/core/headers.js b/www/test/0.0.5/test/core/headers.js index 56527b4c..ab59f992 100644 --- a/www/test/0.0.5/test/core/headers.js +++ b/www/test/0.0.5/test/core/headers.js @@ -107,4 +107,12 @@ describe("Core htmx AJAX headers", function() { invokedEvent.should.equal(true); }) -}); \ No newline at end of file + 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.click(); + this.server.respond(); + }) + +});