medialize / URI.js

Javascript URL mutation library
http://medialize.github.io/URI.js/
MIT License
6.26k stars 474 forks source link

segment(segments) error #382

Closed Fi1osof closed 5 years ago

Fi1osof commented 5 years ago
var url = "/foo/foo%foo"
var uri = new URI(url);
var segments = uri.segment()
// ["foo", "foo%foo"]
uri.segment(segments);

URI.min.js:36 Uncaught URIError: URI malformed
    at Function.decodeURIComponent [as decode] (<anonymous>)
    at m (URI.min.js:36)
    at Function.recodePath (URI.min.js:37)
    at b.e.pathname (URI.min.js:53)
    at b.e.segment (URI.min.js:69)
    at <anonymous>:1:5
rodneyrehm commented 5 years ago

Your input is invalid. % may not be used literally, but has to be encoded as %25.

segment() does not encode or decode the individual values. that's why you can read foo%foo, but not write that (because it expects properly encoded values.

segmentCoded() on the other hand won't allow you to read the invalid value, but would allow you to write it.

Either way, you'll have to find a way to deal with invalid input.

Fi1osof commented 5 years ago

So why i do not get error on uri = new URI("/foo/foo%foo"); ?

rodneyrehm commented 5 years ago

because it's not parsing the path content until you actually modify it.

Fi1osof commented 5 years ago

Main example in offdoc:

// mutating URLs
URI("http://example.org/foo.html?hello=world")
  .username("rodneyrehm")
    // -> http://rodneyrehm@example.org/foo.html?hello=world
  .username("")
    // -> http://example.org/foo.html?hello=world
  .directory("bar")
    // -> http://example.org/bar/foo.html?hello=world
  .suffix("xml")
    // -> http://example.org/bar/foo.xml?hello=world
  .query("")
    // -> http://example.org/bar/foo.xml
  .tld("com")
    // -> http://example.com/bar/foo.xml
  .query({ foo: "bar", hello: ["world", "mars"] });
    // -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars

URI("http://example.org/foo%foo.html?hello=world") works too. Is this correct initializing?