Skip to content

Commit 0685bf1

Browse files
authored
Merge pull request #1238 from Patternslib/fix-pat-inject-tables
fix(pat-inject): Fix problem with inserting table rows.
2 parents e2f5185 + 2e70a13 commit 0685bf1

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/pat/inject/inject.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,15 @@ const inject = {
459459
/* Called after the XHR has succeeded and we have a new $sources
460460
* element to inject.
461461
*/
462-
const wrapper = document.createElement("div");
462+
const wrapper = document.createElement("template");
463463
if ($sources.length > 0) {
464464
const method = cfg.sourceMod === "content" ? "innerHTML" : "outerHTML";
465465
// There might be multiple sources, so we need to loop over them.
466466
// Access them with "innerHTML" or "outerHTML" depending on the sourceMod.
467467
const sources_string = [...$sources].map(source => source[method]).join("\n");
468468
wrapper.innerHTML = sources_string;
469469

470-
for (const img of wrapper.querySelectorAll("img")) {
470+
for (const img of wrapper.content.querySelectorAll("img")) {
471471
events.add_event_listener(
472472
img,
473473
"load",
@@ -481,7 +481,7 @@ const inject = {
481481
}
482482

483483
// Copy, because after insertion wrapper.children is empty.
484-
const source_nodes = [...wrapper.childNodes];
484+
const source_nodes = [...wrapper.content.childNodes];
485485

486486
// Now the injection actually happens.
487487
if (this._inject(trigger, source_nodes, target, cfg)) {

src/pat/inject/inject.test.js

+55
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,61 @@ describe("pat-inject", function () {
18681868
});
18691869
});
18701870

1871+
describe("9.6 - injecting tables.", function () {
1872+
let spy_ajax;
1873+
1874+
beforeEach(function () {
1875+
spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
1876+
});
1877+
1878+
afterEach(function () {
1879+
spy_ajax.mockRestore();
1880+
});
1881+
1882+
it("9.6.1 - Correctly injects table rows", async function () {
1883+
// Table rows <tr> can only be childs of <tbodys> or <thead> elements.
1884+
// If they are childs of <table> elements, the browser implicitly adds a <tbody> element.
1885+
// Table rows cannot be childs of <div> elements or others.
1886+
// There is one exception though - they can be directly added to <template> elements.
1887+
// This test checks this problem which popped up during the 9.10 release cycle and was fixed in:
1888+
// https://github.com/Patternslib/Patterns/pull/1238
1889+
1890+
document.body.innerHTML = `
1891+
<a class="pat-inject"
1892+
href="test.html"
1893+
data-pat-inject="
1894+
source: tr::element;
1895+
target: table;
1896+
">link</a>
1897+
<table>
1898+
</table>
1899+
`;
1900+
1901+
answer(`
1902+
<html>
1903+
<body>
1904+
<table>
1905+
<tr><td>wohoo</td></tr>
1906+
</table>
1907+
</body>
1908+
</html>
1909+
`);
1910+
1911+
const inject = document.querySelector(".pat-inject");
1912+
1913+
pattern.init($(inject));
1914+
await utils.timeout(1); // wait a tick for async to settle.
1915+
1916+
inject.click();
1917+
await utils.timeout(1); // wait a tick for async to settle.
1918+
1919+
1920+
console.log(document.body.outerHTML);
1921+
1922+
const injected = document.querySelectorAll("table tr");
1923+
expect(injected.length).toBe(1);
1924+
});
1925+
});
18711926
});
18721927

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

0 commit comments

Comments
 (0)