@@ -367,21 +367,27 @@ defmodule URI do
367
367
@ doc """
368
368
Percent-encodes all characters that require escaping in `string`.
369
369
370
- By default, this function is meant to escape the whole URI, and
371
- therefore it will only escape characters which are foreign in
372
- all parts of a URI. Reserved characters (such as `:` and `/`)
373
- or unreserved (such as letters and numbers) are not escaped.
374
-
375
- Because different components of a URI require different escaping
376
- rules, this function also accepts a `predicate` function as an optional
377
- argument. If passed, this function will be called with each byte
378
- in `string` as its argument and should return a truthy value (anything other
379
- than `false` or `nil`) if the given byte should be left as is, or
380
- return a falsy value (`false` or `nil`) if the character should be
381
- escaped. Defaults to `URI.char_unescaped?/1`.
382
-
383
- See `encode_www_form/1` if you are interested in escaping reserved
384
- characters too.
370
+ The optional `predicate` argument specifies a function used to detect whether
371
+ a byte in the `string` should be escaped:
372
+
373
+ * if the function returns a truthy value, the byte should be kept as-is.
374
+ * if the function returns a falsy value, the byte should be escaped.
375
+
376
+ The `predicate` argument can use some built-in functions:
377
+
378
+ * `URI.char_unescaped?/1` (default) - reserved characters (such as `:`
379
+ and `/`) or unreserved (such as letters and numbers) are kept as-is.
380
+ It's typically used to encode the whole URI.
381
+ * `URI.char_unreserved?/1` - unreserved characters (such as letters and
382
+ numbers) are kept as-is. It's typically used to encode components in
383
+ a URI, such as query or fragment.
384
+ * `URI.char_reserved?/1` - Reserved characters (such as `:` and `/`) are
385
+ kept as-is.
386
+
387
+ And, you can also use custom functions.
388
+
389
+ See `encode_www_form/1` if you are interested in encoding `string` as
390
+ "x-www-form-urlencoded".
385
391
386
392
## Examples
387
393
0 commit comments