Skip to content

Commit c281ae4

Browse files
committed
fix(pat inject): Fix code error with not scanning and triggering for comment nodes.
1 parent 00b66fe commit c281ae4

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/pat/inject/inject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ $(document).on("patterns-injected.inject", async (ev, cfg, trigger, injected) =>
11051105
// Remove the executing class, add the executed class to the element with pat.inject on it.
11061106
$(trigger).removeClass(cfg.executingClass).addClass(cfg.executedClass);
11071107
}
1108-
if (injected.nodeType !== TEXT_NODE && injected !== COMMENT_NODE) {
1108+
if (injected.nodeType !== TEXT_NODE && injected.nodeType !== COMMENT_NODE) {
11091109
registry.scan(injected, null, { type: "injection", element: trigger });
11101110
$(injected).trigger("patterns-injected-scanned");
11111111

src/pat/inject/inject.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,54 @@ describe("pat-inject", function () {
602602

603603
spy_ajax.mockRestore();
604604
});
605+
606+
it("does not trigger the patterns-injected-scanned event for text nodes", async function () {
607+
const spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
608+
var $a = $('<a class="pat-inject" href="test.html#someid">link</a>');
609+
var $div = $('<div id="someid" />');
610+
$("#lab").empty().append($a).append($div);
611+
var callback = jest.fn();
612+
$(document).on("patterns-injected-scanned", callback);
613+
pattern.init($a);
614+
$a.trigger("click");
615+
answer(`
616+
<html>
617+
<body>
618+
<div id="someid">
619+
repl
620+
</div>
621+
</body>
622+
</html>
623+
`);
624+
await utils.timeout(1); // wait a tick for async to settle.
625+
expect(callback).not.toHaveBeenCalled();
626+
627+
spy_ajax.mockRestore();
628+
});
629+
630+
it("does not trigger the patterns-injected-scanned event for comment nodes", async function () {
631+
const spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
632+
var $a = $('<a class="pat-inject" href="test.html#someid">link</a>');
633+
var $div = $('<div id="someid" />');
634+
$("#lab").empty().append($a).append($div);
635+
var callback = jest.fn();
636+
$(document).on("patterns-injected-scanned", callback);
637+
pattern.init($a);
638+
$a.trigger("click");
639+
answer(`
640+
<html>
641+
<body>
642+
<div id="someid">
643+
<!-- repl -->
644+
</div>
645+
</body>
646+
</html>
647+
`);
648+
await utils.timeout(1); // wait a tick for async to settle.
649+
expect(callback).not.toHaveBeenCalled();
650+
651+
spy_ajax.mockRestore();
652+
});
605653
});
606654

607655
describe("DOM tests", function () {

0 commit comments

Comments
 (0)