-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consider generating URL strings from a URLPattern #73
Comments
The reference library does have some behaviour we would probably want to smooth other, especially given people are likely to use this for user input. In particular path segments should probably be always validated and encoded by default, i.e. no attacks using slashes or relative path segments ( Also path-to-regexp docs don't specify any behaviour for non-named pattern segments, e.g. what would we do with const pattern = new URLPattern(`/foo/*`);
// Almost certainly "/foo/test" is the right behaviour here, as there's only
// a single pattern perhaps the array wrapper could be optional too
pattern.generate("pathname", ["test"]);
const pattern2 = new URLPattern("/images/*.jpg");
// Less clear as to which of these would be correct, it would make sense that people
// would want to include the full name, although others might expect that only the * is replaced
pattern2.generate("pathname", ["1.jpg"]);
pattern2.generate("pathname", ["1"]); It might also make more sense as a separate API as with path-to-regexp, especially if not all syntax is supported for the reason above. i.e. |
If you translate wildcard to a capturing group, the expectation should be that only that group is being replaced during compilation: const pattern = new URLPattern("/images/*.jpg");
pattern.generate('pathname', ['1']) // "images/1.jpg" I'd treat wildcards as non-named pattern segments, implying that the way they are compiled is the same as for the named segments. The only difference is the compilation input: // This is a named equivalent of the wildcard example above.
const pattern = new URLPattern("/images/:filename.jpg");
pattern.generate('pathname', { filename: "1" }) // "images/1.jpg" Replacing a fixed portion of the pattern (".jpg") would be unexpected. |
Wildcards are unnamed matching groups today. So they do indeed get an automatically assigned numeric name. |
I was mainly addressing the comment from @Jamesernator in regards to the ambiguous match of |
Just would like to chime in that this is very important, and that I can’t imagine a case where you read urls into objects without also turning objects into urls. Search pages for example have lots of generated links (think filters, pagination), but also read the url into local state. |
I were just merely considering if a result could be mutable... const pattern = new URLPattern({ pathname: ':vendor/categories/:category' })
// Get current url pattern
const result = pattern.exec(location.href)
result.pathname.groups.category = 'games' // Modify current url match result
location.href = result.toString() // change the url location Thinking this could be useful for a adv search with 2 way data binding or something. all doe a |
the pattern being mutable can make sense, as new URL is a precedence, but personally I would need to make a copy of the pattern for every change then, as you have many links with different state, as it's not the current state you want to show in the URL, but a possible future state |
Would be helpful for my use case. I use a URL pattern to
I'm sure this will be a common enough use case for URLPattern |
One of the most frequent pieces of feedback we have gotten is that developers would like to generate URL and component strings from a URLPattern. For example, something like:
There is a discussions thread about this in #41. I'm filing this issue to consider as a future enhancement.
The text was updated successfully, but these errors were encountered: