Skip to content

Commit df710a2

Browse files
committed
maint(pat-inject): Rework _rebaseHTML to non-JQuery code as preparation to fix a failing test with recent @patternslib/dev.
1 parent e9a5257 commit df710a2

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/pat/inject/inject.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -860,16 +860,16 @@ const inject = {
860860
// Special case, source is none
861861
return "";
862862
}
863-
const $page = $(
864-
html.replace(/(\s)(src\s*)=/gi, '$1src="" data-pat-inject-rebase-$2=').trim()
865-
)
866-
.wrapAll("<div>")
867-
.parent();
868863

869-
$page.find(Object.keys(this._rebaseAttrs).join(",")).each((idx, el_) => {
870-
const $el_ = $(el_);
871-
const attrName = this._rebaseAttrs[el_.tagName];
872-
let value = $el_.attr(attrName);
864+
// Create temporary DOM node and store the html contents on it where
865+
// the original src attribute is kept.
866+
const node = document.createElement("div");
867+
node.innerHTML = html.replace(/(\s)(src\s*)=/gi, '$1src="" data-pat-inject-rebase-$2=').trim();
868+
869+
const selector = Object.keys(this._rebaseAttrs).join(",");
870+
for (const el_ of node.querySelectorAll(selector)) {
871+
const attr = this._rebaseAttrs[el_.tagName];
872+
let value = el_.getAttribute(attr);
873873

874874
if (
875875
value &&
@@ -884,15 +884,12 @@ const inject = {
884884
value.slice(0, 11) !== "javascript:"
885885
) {
886886
value = this._rebaseURL(base, value);
887-
$el_.attr(attrName, value);
887+
el_.setAttribute(attr, value);
888888
}
889-
});
889+
}
890890

891891
for (const [pattern_name, opts] of Object.entries(this._rebaseOptions)) {
892-
for (const el_ of dom.querySelectorAllAndMe(
893-
$page[0],
894-
`[data-pat-${pattern_name}]`
895-
)) {
892+
for (const el_ of node.querySelectorAll(`[data-pat-${pattern_name}]`)) {
896893
const pattern = registry.patterns?.[pattern_name];
897894
const pattern_parser = pattern?.parser;
898895
if (!pattern_parser) {
@@ -924,8 +921,8 @@ const inject = {
924921
}
925922
}
926923

927-
return $page
928-
.html()
924+
return node
925+
.innerHTML
929926
.replace(/src="" data-pat-inject-rebase-/g, "")
930927
.trim();
931928
},

0 commit comments

Comments
 (0)