Skip to content

Commit

Permalink
Handle non-string HTML passed to loadContent() (#200)
Browse files Browse the repository at this point in the history
Fixes #187.
  • Loading branch information
BehindTheMath committed Mar 4, 2019
1 parent 493d56c commit 7940a6e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ Pjax.prototype = {
},

loadContent: function(html, options) {
if (typeof html !== "string") {
trigger(document, "pjax:complete pjax:error", options);

return;
}

var tmpEl = document.implementation.createHTMLDocument("pjax");

// parse HTML attributes to copy them
Expand Down
57 changes: 48 additions & 9 deletions tests/lib/proto/handle-response.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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 loadContent = require("../../../index").prototype.loadContent;

var href = "https://example.org/";

Expand All @@ -28,14 +29,17 @@ tape("test events triggered when handleResponse(false) is called", function(t) {
document.addEventListener("pjax:complete", storeEventHandler);
document.addEventListener("pjax:error", storeEventHandler);

handleReponse.bind(pjax)(false, null);
handleResponse.bind(pjax)(false, null);

t.same(
events,
["pjax:complete", "pjax:error"],
"calling handleResponse(false) triggers 'pjax:complete' and 'pjax:error'"
);

document.removeEventListener("pjax:complete", storeEventHandler);
document.removeEventListener("pjax:error", storeEventHandler);

t.end();
});

Expand All @@ -52,7 +56,7 @@ tape("test when handleResponse() is called normally", function(t) {
getResponseHeader: noop
};

handleReponse.bind(pjax)("", request, "href");
handleResponse.bind(pjax)("", request, "href");

delete window.history.state.uid;
t.same(
Expand Down Expand Up @@ -94,7 +98,7 @@ tape(
getResponseHeader: noop
};

handleReponse.bind(pjax)("", request, "");
handleResponse.bind(pjax)("", request, "");

t.equals(
pjax.state.href,
Expand Down Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -167,7 +171,7 @@ tape("test when handleResponse() is called normally with a hash", function(t) {
getResponseHeader: noop
};

handleReponse.bind(pjax)("", request, href + "1#test");
handleResponse.bind(pjax)("", request, href + "1#test");

t.equals(
pjax.state.href,
Expand Down Expand Up @@ -206,7 +210,7 @@ tape("test try...catch for loadContent() when options.debug is true", function(

t.throws(
function() {
handleReponse.bind(pjax)("", request, "");
handleResponse.bind(pjax)("", request, "");
},
Error,
"error is rethrown"
Expand Down Expand Up @@ -241,7 +245,7 @@ tape("test try...catch for loadContent()", function(t) {
t.doesNotThrow(
function() {
t.equals(
handleReponse.bind(pjax)("", request, ""),
handleResponse.bind(pjax)("", request, ""),
true,
"this.latestChance() is called"
);
Expand All @@ -252,3 +256,38 @@ tape("test try...catch for loadContent()", function(t) {

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();
}
);

0 comments on commit 7940a6e

Please sign in to comment.