Skip to content

Commit e3036e9

Browse files
committed
pat inject: Rebase URLs in pattern configuration attributes.
This avoids URLs in pattern configuration to point to unreachable paths in the context where the result is injected into.
1 parent 0a3fd54 commit e3036e9

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- pat autofocus: Do not autofocus in iframes. Fixes: #761.
5757
- pat inject: Allow configurable error pages. Can be disabled by adding ``pat-inject-errorhandler.off`` to the URL's query string.
5858
- core utils: Add ``jqToNode`` to return a DOM node if a jQuery node was passed.
59+
- pat inject: Rebase URLs in pattern configuration attributes. This avoids URLs in pattern configuration to point to unreachable paths in the context where the result is injected into.
5960

6061
### Technical
6162

src/pat/inject/index.html

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

416+
<section id="rebase-url-demo">
417+
<div>
418+
<h3>Inject and fix URLs of options</h3>
419+
<a
420+
class="pat-inject"
421+
data-pat-inject="url:./index.html#rebase-url-demo; target: self::element"
422+
>
423+
injection happens here!
424+
</button>
425+
</div>
426+
</section>
427+
416428
<div id="inject-demo__content-wrapper" style="display: none">
417429
<section id="inject-demo__content">Liked</section>
418430
</div>
@@ -667,6 +679,14 @@ <h3>
667679
transform: scale(1);
668680
}
669681
}
682+
683+
#rebase-url-demo a {
684+
cursor: pointer;
685+
}
686+
#rebase-url-demo div div {
687+
margin-left: 2em;
688+
border: 1px solid black;
689+
}
670690
</style>
671691
</body>
672692
</html>

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)