Skip to content

Add feature for just the urlencode builtin #704

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

Merged
merged 2 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 = ["urlencode", "slug", "humansize", "chrono", "chrono-tz", "rand"]
urlencode = ["percent-encoding"]
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 = "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 = "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 = "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 = "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 = "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 = "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 = "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 = "urlencode")]
#[test]
fn test_urlencode() {
let tests = vec![
Expand All @@ -620,7 +620,7 @@ mod tests {
}
}

#[cfg(feature = "builtins")]
#[cfg(feature = "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 = "urlencode")]
self.register_filter("urlencode", string::urlencode);
#[cfg(feature = "builtins")]
#[cfg(feature = "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