summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/shuriken25
-rw-r--r--src/lib/template.source.sh25
-rwxr-xr-xtests/cli.sh29
3 files changed, 45 insertions, 34 deletions
diff --git a/bin/shuriken b/bin/shuriken
index 376ee4b..6ce7a2f 100755
--- a/bin/shuriken
+++ b/bin/shuriken
@@ -486,15 +486,11 @@ resolve_sync_destinations() {
# Inlined from src/lib/template.source.sh
_html_escape() {
- local text="$1"; shift
-
- text=${text//&/\&}
- text=${text//</\&lt;}
- text=${text//>/\&gt;}
- text=${text//\"/\&quot;}
- text=${text//\'/\&#39;}
+ local -r text="$1"; shift
+ local escaped_text
- printf '%s\n' "$text"
+ html_escape_to escaped_text "$text"
+ printf '%s\n' "$escaped_text"
}
html_escape_to() {
@@ -511,16 +507,11 @@ html_escape_to() {
}
_css_string_escape() {
- local text="$1"; shift
-
- text=${text//\\/\\\\}
- text=${text//&/\\000026}
- text=${text//</\\00003c}
- text=${text//>/\\00003e}
- text=${text//\"/\\000022}
- text=${text//\'/\\000027}
+ local -r text="$1"; shift
+ local escaped_text
- printf '%s\n' "$text"
+ css_string_escape_to escaped_text "$text"
+ printf '%s\n' "$escaped_text"
}
css_string_escape_to() {
diff --git a/src/lib/template.source.sh b/src/lib/template.source.sh
index 90e0ebd..0f038e4 100644
--- a/src/lib/template.source.sh
+++ b/src/lib/template.source.sh
@@ -1,13 +1,9 @@
_html_escape() {
- local text="$1"; shift
-
- text=${text//&/\&amp;}
- text=${text//</\&lt;}
- text=${text//>/\&gt;}
- text=${text//\"/\&quot;}
- text=${text//\'/\&#39;}
+ local -r text="$1"; shift
+ local escaped_text
- printf '%s\n' "$text"
+ html_escape_to escaped_text "$text"
+ printf '%s\n' "$escaped_text"
}
html_escape_to() {
@@ -24,16 +20,11 @@ html_escape_to() {
}
_css_string_escape() {
- local text="$1"; shift
-
- text=${text//\\/\\\\}
- text=${text//&/\\000026}
- text=${text//</\\00003c}
- text=${text//>/\\00003e}
- text=${text//\"/\\000022}
- text=${text//\'/\\000027}
+ local -r text="$1"; shift
+ local escaped_text
- printf '%s\n' "$text"
+ css_string_escape_to escaped_text "$text"
+ printf '%s\n' "$escaped_text"
}
css_string_escape_to() {
diff --git a/tests/cli.sh b/tests/cli.sh
index 747cd6c..ddf4bf2 100755
--- a/tests/cli.sh
+++ b/tests/cli.sh
@@ -3156,6 +3156,32 @@ test_generate_missing_imagemagick_fails() {
test::teardown
}
+test_template_stdout_escape_helpers_match_nameref_helpers() {
+ local css_text
+ local css_to
+ local css_stdout
+ local html_text
+ local html_to
+ local html_stdout
+
+ # shellcheck source=src/lib/template.source.sh
+ source "$TEST_REPO_ROOT/src/lib/template.source.sh"
+
+ html_text=$'A & "quoted" <title> \'ok\''
+ html_escape_to html_to "$html_text"
+ html_stdout=$(_html_escape "$html_text")
+ test "$html_stdout" = "$html_to"
+ test "$html_stdout" = \
+ 'A &amp; &quot;quoted&quot; &lt;title&gt; &#39;ok&#39;'
+
+ css_text=$'path\\kid\'s_"<tag>&.jpg'
+ css_string_escape_to css_to "$css_text"
+ css_stdout=$(_css_string_escape "$css_text")
+ test "$css_stdout" = "$css_to"
+ test "$css_stdout" = \
+ 'path\\kid\000027s_\000022\00003ctag\00003e\000026.jpg'
+}
+
test_generate_escapes_html_values() {
local config_file
local css_photo
@@ -3925,6 +3951,9 @@ main() {
'--generate fails when ImageMagick is missing' \
test_generate_missing_imagemagick_fails
test::run_case \
+ 'template stdout escapers match nameref helpers' \
+ test_template_stdout_escape_helpers_match_nameref_helpers
+ test::run_case \
'--generate escapes generated HTML values' \
test_generate_escapes_html_values
test::run_case \