Skip to content

Commit ba35fda

Browse files
committed
fix(pat-inject): Fix broken scrolling after injection.
This fixes a problem where scrolling wasn't working after injection anymore when the injection result is multiple nodes and not a single one.
1 parent 2d7a0e9 commit ba35fda

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/pat/inject/inject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ const inject = {
557557
// 2) getting the element to scroll to (if not "top")
558558
const scroll_target = ["top", "target"].includes(cfg.scroll)
559559
? cfg.$target[0]
560-
: dom.querySelectorAllAndMe($injected[0], cfg.scroll);
560+
: dom.querySelectorAllAndMe($injected, cfg.scroll)[0];
561561

562562
const scroll_container = dom.find_scroll_container(
563563
scroll_target,

src/pat/inject/inject.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,94 @@ describe("pat-inject", function () {
947947

948948
expect($div.contents().last().text()).toBe("repl");
949949
});
950+
951+
it("9.1.13 - injects and scrolls to scroll target, if set.", async function () {
952+
const scroll_spy = jest
953+
.spyOn(window, "scrollTo")
954+
.mockImplementation(() => null);
955+
956+
957+
answer(`
958+
<html>
959+
<body>
960+
<div id="source">
961+
</div>
962+
</body>
963+
</html>
964+
`);
965+
966+
document.body.innerHTML = `
967+
<html>
968+
<body>
969+
<a class="pat-inject"
970+
href="test.html"
971+
data-pat-inject="
972+
source: #source::element;
973+
target: self::element;
974+
scroll: #source;
975+
"
976+
>inject this</a>
977+
</body>
978+
</html>
979+
`;
980+
981+
const trigger = document.querySelector(".pat-inject");
982+
983+
pattern.init($(trigger));
984+
await utils.timeout(1); // wait a tick for async to settle.
985+
986+
trigger.click();
987+
await utils.timeout(1); // wait a tick for async to settle.
988+
989+
expect(scroll_spy).toHaveBeenCalledTimes(1);
990+
});
991+
992+
it("9.1.14 - injects and scrolls to scroll target with injection results as a list of jQuery nodes.", async function () {
993+
const scroll_spy = jest
994+
.spyOn(window, "scrollTo")
995+
.mockImplementation(() => null);
996+
997+
998+
answer(`
999+
<html>
1000+
<body>
1001+
<article id="source">
1002+
<section class="paragraph-1"></section>
1003+
<section class="paragraph-2"></section>
1004+
<section class="paragraph-3"></section>
1005+
</article>
1006+
</body>
1007+
</html>
1008+
`);
1009+
1010+
document.body.innerHTML = `
1011+
<html>
1012+
<body>
1013+
<a class="pat-inject"
1014+
href="test.html"
1015+
data-pat-inject="
1016+
source: #source;
1017+
target: self::element;
1018+
scroll: .paragraph-2;
1019+
"
1020+
>inject this</a>
1021+
</body>
1022+
</html>
1023+
`;
1024+
1025+
const trigger = document.querySelector(".pat-inject");
1026+
1027+
pattern.init($(trigger));
1028+
await utils.timeout(1); // wait a tick for async to settle.
1029+
1030+
trigger.click();
1031+
await utils.timeout(1); // wait a tick for async to settle.
1032+
1033+
console.log(document.body.outerHTML)
1034+
1035+
expect(scroll_spy).toHaveBeenCalledTimes(1);
1036+
});
1037+
9501038
});
9511039

9521040
describe("9.2 - inject on forms", function () {

0 commit comments

Comments
 (0)