From 1a47d22143d50b66c9c30160807657af8f2082c5 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 11 Jul 2023 16:21:56 +0200 Subject: [PATCH] Ensure the str helper is url/href compliant So we are: - replacing the square brackets by parenthesis (allowed in URLs). - urlenconding the str contents so they are also allowed links. Backed by tests. Fixes #270 --- mustache_lint/mustache_lint.php | 2 +- tests/1-mustache_lint.bats | 15 +++++++++++ tests/fixtures/402-mustache_lint-str.patch | 30 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/402-mustache_lint-str.patch diff --git a/mustache_lint/mustache_lint.php b/mustache_lint/mustache_lint.php index 4a8a06fb..fb4e93e0 100644 --- a/mustache_lint/mustache_lint.php +++ b/mustache_lint/mustache_lint.php @@ -87,7 +87,7 @@ $mustache = new Mustache_Engine([ 'pragmas' => [Mustache_Engine::PRAGMA_BLOCKS], 'helpers' => [ // Emulate some helpers for html validation purposes. - 'str' => function($text) { return "[[$text]]"; }, + 'str' => function($text) { return '((' . urlencode($text) . '))'; }, 'pix' => function($text) { return "$text"; }, 'uniqid' => function() { return "would-be-a-uniqid"; }, 'quote' => function($text, $helper) { diff --git a/tests/1-mustache_lint.bats b/tests/1-mustache_lint.bats index 47babc9d..82f6da2f 100755 --- a/tests/1-mustache_lint.bats +++ b/tests/1-mustache_lint.bats @@ -132,6 +132,21 @@ setup () { assert_failure } +@test "mustache lint: Test str helper is working" { + # Set up. + create_git_branch MOODLE_402_STABLE v4.2.0 + git_apply_fixture 402-mustache_lint-str.patch + export GIT_PREVIOUS_COMMIT=$FIXTURE_HASH_BEFORE + export GIT_COMMIT=$FIXTURE_HASH_AFTER + + ci_run mustache_lint/mustache_lint.sh + + # Assert result + assert_success + assert_output --partial "lib/templates/test_str.mustache - OK: Mustache rendered html succesfully" + assert_output --partial "No mustache problems found" +} + @test "mustache_lint: Test quote and uniq helpers are working" { # Set up. git_apply_fixture 31-mustache_lint-quote_and_uniq.patch diff --git a/tests/fixtures/402-mustache_lint-str.patch b/tests/fixtures/402-mustache_lint-str.patch new file mode 100644 index 00000000..bd6abe57 --- /dev/null +++ b/tests/fixtures/402-mustache_lint-str.patch @@ -0,0 +1,30 @@ +From d64cf216e6f0cdfed617b538fde5b4e193e7da88 Mon Sep 17 00:00:00 2001 +From: "Eloy Lafuente (stronk7)" +Date: Tue, 11 Jul 2023 15:16:18 +0200 +Subject: [PATCH] MDL-12345: test str helper + +--- + lib/templates/test_str.mustache | 11 +++++++++++ + 1 file changed, 11 insertions(+) + create mode 100644 lib/templates/test_str.mustache + +diff --git a/lib/templates/test_str.mustache b/lib/templates/test_str.mustache +new file mode 100644 +index 00000000000..cb7a906deb9 +--- /dev/null ++++ b/lib/templates/test_str.mustache +@@ -0,0 +1,11 @@ ++{{! ++ @template core/test_str ++ ++ Example context (json): ++ { ++ } ++ ++}} ++

{{#str}} string, component {{/str}}

++ ++Link +-- +2.40.0 +