Skip to content

Commit bf30719

Browse files
authored
Merge pull request #1229 from Patternslib/fix-inject
fix(pat-inject): Re-enable support for multiple source matches.
2 parents 15bf4f2 + 07a244f commit bf30719

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

src/pat/inject/inject.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,10 @@ const inject = {
399399
* Cancel button is pressed (this triggers reset event on the
400400
* form) you would expect to populate with initial placeholder
401401
*/
402-
if (cfg.target === "none")
402+
if (cfg.target === "none") {
403403
// Special case, we don't want to display any return value.
404404
return;
405+
}
405406
const $form = cfg.$target.parents("form");
406407
if ($form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
407408
cfg.$target.data("initial-value", cfg.$target.html());
@@ -454,17 +455,17 @@ const inject = {
454455
return $target;
455456
},
456457

457-
_performInjection(target, $el, $source, cfg, trigger, $title) {
458-
/* Called after the XHR has succeeded and we have a new $source
458+
_performInjection(target, $el, $sources, cfg, trigger, $title) {
459+
/* Called after the XHR has succeeded and we have a new $sources
459460
* element to inject.
460461
*/
461462
const wrapper = document.createElement("div");
462-
if ($source.length > 0) {
463-
if (cfg.sourceMod === "content") {
464-
wrapper.innerHTML = $source[0].innerHTML;
465-
} else {
466-
wrapper.innerHTML = $source[0].outerHTML;
467-
}
463+
if ($sources.length > 0) {
464+
const method = cfg.sourceMod === "content" ? "innerHTML" : "outerHTML";
465+
// There might be multiple sources, so we need to loop over them.
466+
// Access them with "innerHTML" or "outerHTML" depending on the sourceMod.
467+
const sources_string = [...$sources].map(source => source[method]).join("\n");
468+
wrapper.innerHTML = sources_string;
468469

469470
for (const img of wrapper.querySelectorAll("img")) {
470471
events.add_event_listener(

src/pat/inject/inject.test.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,6 @@ describe("pat-inject", function () {
16091609

16101610
await utils.timeout(1); // wait a tick for async to settle.
16111611

1612-
console.log(document.body.innerHTML);
16131612
const modal = document.querySelector("#pat-modal");
16141613
expect(modal).toBeTruthy();
16151614
expect(modal.innerHTML.replace(/\s/g, "")).toBe(
@@ -1644,7 +1643,6 @@ describe("pat-inject", function () {
16441643

16451644
await utils.timeout(1); // wait a tick for async to settle.
16461645

1647-
console.log(document.body.innerHTML);
16481646
const modal = document.querySelector("#pat-modal");
16491647
expect(modal).toBeFalsy();
16501648
});
@@ -1742,6 +1740,56 @@ describe("pat-inject", function () {
17421740
expect(title.textContent.trim()).toBe("test"); // Old title
17431741
});
17441742
});
1743+
1744+
describe("9.5 - support multiple source element matches.", function () {
1745+
let spy_ajax;
1746+
1747+
beforeEach(function () {
1748+
spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
1749+
});
1750+
1751+
afterEach(function () {
1752+
spy_ajax.mockRestore();
1753+
});
1754+
1755+
it("9.5.1 - Injects multiple source element matches", async function () {
1756+
document.body.innerHTML = `
1757+
<a class="pat-inject"
1758+
href="test.html"
1759+
data-pat-inject="
1760+
source: a::element;
1761+
target: .result;
1762+
">link</a>
1763+
<section class="result">
1764+
</section>
1765+
`;
1766+
1767+
answer(`
1768+
<html>
1769+
<body>
1770+
<a>Link 1</a>
1771+
<a>Link 2</a>
1772+
<a>Link 3</a>
1773+
</body>
1774+
</html>
1775+
`);
1776+
1777+
const inject = document.querySelector(".pat-inject");
1778+
1779+
pattern.init($(inject));
1780+
await utils.timeout(1); // wait a tick for async to settle.
1781+
1782+
inject.click();
1783+
await utils.timeout(1); // wait a tick for async to settle.
1784+
1785+
const injected = document.querySelectorAll(".result a");
1786+
expect(injected.length).toBe(3);
1787+
expect(injected[0].textContent).toBe("Link 1");
1788+
expect(injected[1].textContent).toBe("Link 2");
1789+
expect(injected[2].textContent).toBe("Link 3");
1790+
});
1791+
});
1792+
17451793
});
17461794

17471795
describe("10 - Error handling", () => {

0 commit comments

Comments
 (0)