Skip to content

Commit b793fb3

Browse files
committed
pat inject: Rebase URLs in pattern options.
1 parent fe5d6d9 commit b793fb3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/pat/inject/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ <h3>Shows a fallback message on error</h3>
413413
</a>
414414
</section>
415415

416+
<section>
417+
<h3>Inject and fix URLs of options</h3>
418+
<a
419+
type="button"
420+
class="pat-inject"
421+
data-pat-inject="url:./index.html; target: self::element"
422+
>
423+
injection happens here!
424+
</a>
425+
</section>
426+
416427
<div id="inject-demo__content-wrapper" style="display: none">
417428
<section id="inject-demo__content">Liked</section>
418429
</div>

src/pat/inject/inject.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import "regenerator-runtime/runtime"; // needed for ``await`` support
33
import $ from "jquery";
44
import _ from "underscore";
55
import ajax from "../ajax/ajax";
6+
import dom from "../../core/dom";
67
import logging from "../../core/logging";
78
import Parser from "../../core/parser";
89
import registry from "../../core/registry";
@@ -867,6 +868,14 @@ const inject = {
867868
VIDEO: "data-pat-inject-rebase-src",
868869
},
869870

871+
_rebaseOptions: {
872+
"data-pat-inject": ["url"],
873+
"data-pat-calendar": ["url"],
874+
"data-pat-date-picker": ["i18n"],
875+
"data-pat-datetime-picker": ["i18n"],
876+
"data-pat-collapsible": ["load-content"],
877+
},
878+
870879
_rebaseHTML(base, html) {
871880
if (html === "") {
872881
// Special case, source is none
@@ -901,6 +910,29 @@ const inject = {
901910
$el_.attr(attrName, value);
902911
}
903912
});
913+
914+
for (const [attr, opts] of Object.entries(this._rebaseOptions)) {
915+
for (const el_ of dom.querySelectorAllAndMe(
916+
$page[0],
917+
`[${attr}]`
918+
)) {
919+
const val = el_.getAttribute(attr, false);
920+
if (val) {
921+
let options = parser._parse(val);
922+
let changed = false;
923+
for (const opt of opts) {
924+
if (options[opt]) {
925+
options[opt] = utils.rebaseURL(base, options[opt]);
926+
changed = true;
927+
}
928+
}
929+
if (changed) {
930+
el_.setAttribute(attr, JSON.stringify(options));
931+
}
932+
}
933+
}
934+
}
935+
904936
// XXX: IE8 changes the order of attributes in html. The following
905937
// lines move data-pat-inject-rebase-src to src.
906938
$page.find("[data-pat-inject-rebase-src]").each((id, el_) => {

0 commit comments

Comments
 (0)