Handle non-string HTML passed to loadContent() #200

Merged
BehindTheMath merged 1 commits from fix/loadContent-non-string into master 2019-03-04 09:32:27 -05:00
2 changed files with 54 additions and 9 deletions

View File

@@ -117,6 +117,12 @@ Pjax.prototype = {
}, },
loadContent: function(html, options) { loadContent: function(html, options) {
if (typeof html !== "string") {
trigger(document, "pjax:complete pjax:error", options);
return;
}
var tmpEl = document.implementation.createHTMLDocument("pjax"); var tmpEl = document.implementation.createHTMLDocument("pjax");
// parse HTML attributes to copy them // parse HTML attributes to copy them

View File

@@ -1,7 +1,8 @@
var tape = require("tape"); var tape = require("tape");
var handleReponse = require("../../../lib/proto/handle-response"); var handleResponse = require("../../../lib/proto/handle-response");
var noop = require("../../../lib/util/noop"); var noop = require("../../../lib/util/noop");
var loadContent = require("../../../index").prototype.loadContent;
var href = "https://example.org/"; var href = "https://example.org/";
@@ -28,7 +29,7 @@ tape("test events triggered when handleResponse(false) is called", function(t) {
document.addEventListener("pjax:complete", storeEventHandler); document.addEventListener("pjax:complete", storeEventHandler);
document.addEventListener("pjax:error", storeEventHandler); document.addEventListener("pjax:error", storeEventHandler);
handleReponse.bind(pjax)(false, null); handleResponse.bind(pjax)(false, null);
t.same( t.same(
events, events,
@@ -36,6 +37,9 @@ tape("test events triggered when handleResponse(false) is called", function(t) {
"calling handleResponse(false) triggers 'pjax:complete' and 'pjax:error'" "calling handleResponse(false) triggers 'pjax:complete' and 'pjax:error'"
); );
document.removeEventListener("pjax:complete", storeEventHandler);
document.removeEventListener("pjax:error", storeEventHandler);
t.end(); t.end();
}); });
@@ -52,7 +56,7 @@ tape("test when handleResponse() is called normally", function(t) {
getResponseHeader: noop getResponseHeader: noop
}; };
handleReponse.bind(pjax)("", request, "href"); handleResponse.bind(pjax)("", request, "href");
delete window.history.state.uid; delete window.history.state.uid;
t.same( t.same(
@@ -94,7 +98,7 @@ tape(
getResponseHeader: noop getResponseHeader: noop
}; };
handleReponse.bind(pjax)("", request, ""); handleResponse.bind(pjax)("", request, "");
t.equals( t.equals(
pjax.state.href, pjax.state.href,
@@ -123,7 +127,7 @@ tape("test when handleResponse() is called normally with X-PJAX-URL", function(
} }
}; };
handleReponse.bind(pjax)("", request, ""); handleResponse.bind(pjax)("", request, "");
t.equals(pjax.state.href, href + "2", "this.state.href is set correctly"); t.equals(pjax.state.href, href + "2", "this.state.href is set correctly");
@@ -147,7 +151,7 @@ tape(
} }
}; };
handleReponse.bind(pjax)("", request, ""); handleResponse.bind(pjax)("", request, "");
t.equals(pjax.state.href, href + "3", "this.state.href is set correctly"); t.equals(pjax.state.href, href + "3", "this.state.href is set correctly");
@@ -167,7 +171,7 @@ tape("test when handleResponse() is called normally with a hash", function(t) {
getResponseHeader: noop getResponseHeader: noop
}; };
handleReponse.bind(pjax)("", request, href + "1#test"); handleResponse.bind(pjax)("", request, href + "1#test");
t.equals( t.equals(
pjax.state.href, pjax.state.href,
@@ -206,7 +210,7 @@ tape("test try...catch for loadContent() when options.debug is true", function(
t.throws( t.throws(
function() { function() {
handleReponse.bind(pjax)("", request, ""); handleResponse.bind(pjax)("", request, "");
}, },
Error, Error,
"error is rethrown" "error is rethrown"
@@ -241,7 +245,7 @@ tape("test try...catch for loadContent()", function(t) {
t.doesNotThrow( t.doesNotThrow(
function() { function() {
t.equals( t.equals(
handleReponse.bind(pjax)("", request, ""), handleResponse.bind(pjax)("", request, ""),
true, true,
"this.latestChance() is called" "this.latestChance() is called"
); );
@@ -252,3 +256,38 @@ tape("test try...catch for loadContent()", function(t) {
t.end(); t.end();
}); });
tape(
"test events triggered when loadContent() is called with a non-string html argument",
function(t) {
t.plan(3);
var options = {
test: 1
};
var events = [];
storeEventHandler = function(e) {
events.push(e.type);
t.equal(e.test, 1);
};
document.addEventListener("pjax:complete", storeEventHandler);
document.addEventListener("pjax:error", storeEventHandler);
loadContent(null, options);
t.same(
events,
["pjax:complete", "pjax:error"],
"calling loadContent() with a non-string html argument triggers 'pjax:complete' and 'pjax:error'"
);
document.removeEventListener("pjax:complete", storeEventHandler);
document.removeEventListener("pjax:error", storeEventHandler);
t.end();
}
);