Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ tempfile = "3"

[features]
default = ["builtins"]
builtins = ["slug", "percent-encoding", "humansize", "chrono", "chrono-tz", "rand"]
builtins = ["builtins_urlencode", "slug", "humansize", "chrono", "chrono-tz", "rand"]
builtins_urlencode = ["percent-encoding"]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just name the feature urlencode?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I renamed the feature to just urlencode

preserve_order = ["serde_json/preserve_order"]

[badges]
Expand Down
18 changes: 9 additions & 9 deletions src/builtins/filters/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use regex::{Captures, Regex};
use serde_json::value::{to_value, Value};
use unic_segment::GraphemeIndices;

#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
use percent_encoding::{percent_encode, AsciiSet, NON_ALPHANUMERIC};

use crate::errors::{Error, Result};
use crate::utils;

/// https://url.spec.whatwg.org/#fragment-percent-encode-set
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
const FRAGMENT_ENCODE_SET: &AsciiSet =
&percent_encoding::CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');

/// https://url.spec.whatwg.org/#path-percent-encode-set
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
const PATH_ENCODE_SET: &AsciiSet = &FRAGMENT_ENCODE_SET.add(b'#').add(b'?').add(b'{').add(b'}');

/// https://url.spec.whatwg.org/#userinfo-percent-encode-set
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
const USERINFO_ENCODE_SET: &AsciiSet = &PATH_ENCODE_SET
.add(b'/')
.add(b':')
Expand All @@ -38,7 +38,7 @@ const USERINFO_ENCODE_SET: &AsciiSet = &PATH_ENCODE_SET
/// Same as Python quote
/// https://github.com/python/cpython/blob/da27d9b9dc44913ffee8f28d9638985eaaa03755/Lib/urllib/parse.py#L787
/// with `/` not escaped
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
const PYTHON_ENCODE_SET: &AsciiSet = &USERINFO_ENCODE_SET
.remove(b'/')
.add(b':')
Expand Down Expand Up @@ -214,15 +214,15 @@ pub fn capitalize(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
}

/// Percent-encodes reserved URI characters
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
pub fn urlencode(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("urlencode", "value", String, value);
let encoded = percent_encode(s.as_bytes(), &PYTHON_ENCODE_SET).to_string();
Ok(Value::String(encoded))
}

/// Percent-encodes all non-alphanumeric characters
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
pub fn urlencode_strict(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("urlencode_strict", "value", String, value);
let encoded = percent_encode(s.as_bytes(), &NON_ALPHANUMERIC).to_string();
Expand Down Expand Up @@ -596,7 +596,7 @@ mod tests {
}
}

#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
#[test]
fn test_urlencode() {
let tests = vec![
Expand All @@ -620,7 +620,7 @@ mod tests {
}
}

#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
#[test]
fn test_urlencode_strict() {
let tests = vec![
Expand Down
4 changes: 2 additions & 2 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ impl Tera {
self.register_filter("linebreaksbr", string::linebreaksbr);
self.register_filter("striptags", string::striptags);
self.register_filter("spaceless", string::spaceless);
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
self.register_filter("urlencode", string::urlencode);
#[cfg(feature = "builtins")]
#[cfg(feature = "builtins_urlencode")]
self.register_filter("urlencode_strict", string::urlencode_strict);
self.register_filter("escape", string::escape_html);
self.register_filter("escape_xml", string::escape_xml);
Expand Down