Skip to content

Commit

Permalink
Handle the pathological case described in whatwg/urlpattern#210.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyroman committed Feb 27, 2024
1 parent 3052bca commit 49ed129
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = function(eleventyConfig, options = {}) {
// In the unlikely event it has a special URL pattern character, escape it.
let root = eleventyConfig.getFilter("url")("/", eleventyConfig.pathPrefix);
let escapedRoot = root.replaceAll(/[{}()*?+:]/g, "\\$&");
if (/[:?#]/.test(escapedRoot)) {
escapedRoot = `{${escapedRoot.slice(0, -1)}}/`;
}
let condition = {href_matches: escapedRoot + "*"};
if (exclude.length !== 0) {
let excludePatterns = exclude.map(e => e.replace(/^\/(?!\/)/, escapedRoot));
Expand Down
14 changes: 14 additions & 0 deletions test/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ test("with a path prefix and exclusions", async t => {
t.snapshot(index.content);
});

test("with a path prefix with stronger escaping needs", async t => {
// This path prefix not only contains :, a special character, but that
// character also needs to be further escaped to avoid being treated as a
// scheme separator.
// https://github.com/whatwg/urlpattern/issues/210
let eleventy = makeEleventy({
pathPrefix: "/zone:5/",
pluginOptions: {exclude: ["/dynamic/*"]},
});
let json = await eleventy.toJSON();
let index = json.find(p => p.url === '/');
t.snapshot(index.content);
});

test("with prerender", async t => {
let eleventy = makeEleventy({
pluginOptions: {action: "prerender"}
Expand Down
7 changes: 7 additions & 0 deletions test/snapshots/Test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Generated by [AVA](https://avajs.dev).
`<script type="speculationrules">{"prefetch":[{"source":"document","where":{"and":[{"href_matches":"/foo/bar/*"},{"not":{"href_matches":["/foo/bar/admin/*","/foo/bar/logout"]}}]},"eagerness":"moderate"}]}</script>␊
`

## with a path prefix with stronger escaping needs

> Snapshot 1
`<script type="speculationrules">{"prefetch":[{"source":"document","where":{"and":[{"href_matches":"{/zone\\\\:5}/*"},{"not":{"href_matches":["{/zone\\\\:5}/dynamic/*"]}}]},"eagerness":"moderate"}]}</script>␊
`

## with prerender

> Snapshot 1
Expand Down
Binary file modified test/snapshots/Test.js.snap
Binary file not shown.

0 comments on commit 49ed129

Please sign in to comment.