From eea8ceda63bf249dfe0e2d81a444783c5a06e4ec Mon Sep 17 00:00:00 2001 From: Molkobain Date: Mon, 15 Feb 2021 16:28:34 +0100 Subject: [PATCH] Refactor "sanitize_identifier" and "sanitize_variable_name"custom TWIG functions to "sanitize(FOO)" Note: FOO is one of \utils::ENUM_SANITIZATION_FILTER_XXX --- .../application/TwigBase/Twig/Extension.php | 23 +++-- .../layouts/tab-container/layout.html.twig | 84 ++++++++++--------- .../base/layouts/tab-container/layout.js.twig | 15 ++-- .../backoffice/ajaxpage/layout.html.twig | 2 +- 4 files changed, 64 insertions(+), 60 deletions(-) diff --git a/sources/application/TwigBase/Twig/Extension.php b/sources/application/TwigBase/Twig/Extension.php index a1200550c..b391e9ed5 100644 --- a/sources/application/TwigBase/Twig/Extension.php +++ b/sources/application/TwigBase/Twig/Extension.php @@ -86,19 +86,18 @@ class Extension }) ); - // Filter to sanitize an XML / HTML identifier - // Usage in twig: {{ 'identifier:to-sanitize'|sanitize_identifier }} - $oTwigEnv->addFilter(new Twig_SimpleFilter('sanitize_identifier', function ($sString) { - return utils::Sanitize($sString, '', utils::ENUM_SANITIZATION_FILTER_ELEMENT_IDENTIFIER); + /** + * Filter to sanitize a text + * Usage in twig: {{ 'variable_name:to-sanitize'|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) }} + * + * @uses \utils::Sanitize() + * @since 3.0.0 + */ + $oTwigEnv->addFilter(new Twig_SimpleFilter('sanitize', function (string $sString, string $sFilter) { + return utils::Sanitize($sString, '', $sFilter); }) ); - // Filter to sanitize a variable name - // Usage in twig: {{ 'variable_name:to-sanitize'|sanitize_variable_name }} - $oTwigEnv->addFilter(new Twig_SimpleFilter('sanitize_variable_name', function ($sString) { - return utils::Sanitize($sString, '', utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME); - }) - ); // Filter to add a parameter at the end of the URL to force cache invalidation after an upgrade. // Previously we put the iTop version but now it's the last setup/toolkit timestamp to avoid cache issues when building several times the same version during tests // @@ -106,9 +105,7 @@ class Extension $oTwigEnv->addFilter(new Twig_SimpleFilter('add_itop_version', function ($sUrl) { if (strpos($sUrl, '?') === false) { $sUrl = $sUrl."?t=".utils::GetCacheBusterTimestamp(); - } - else - { + } else { $sUrl = $sUrl."&t=".utils::GetCacheBusterTimestamp(); } diff --git a/templates/base/layouts/tab-container/layout.html.twig b/templates/base/layouts/tab-container/layout.html.twig index 96fa057e7..74d578a08 100644 --- a/templates/base/layouts/tab-container/layout.html.twig +++ b/templates/base/layouts/tab-container/layout.html.twig @@ -9,20 +9,23 @@ {% for oTab in oUIBlock.GetSubBlocks() %} {% block iboTabContainerTab %} {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_AJAX') %} -
  • - {{ oTab.GetTitle() }} -
  • - {% elseif oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} -
  • - - {{ oTab.GetTitle() }} - -
  • - {% endif %} +
  • + {{ oTab.GetTitle() }} +
  • + {% elseif oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} +
  • + + {{ oTab.GetTitle() }} + +
  • + {% endif %} {% endblock %} {% endfor %}
  • @@ -30,11 +33,12 @@
    - {% for oTab in oUIBlock.GetSubBlocks() %} - - {{ oTab.GetTitle() }} - - {% endfor %} + {% for oTab in oUIBlock.GetSubBlocks() %} + + {{ oTab.GetTitle() }} + + {% endfor %}
  • {% endblock %} @@ -45,30 +49,32 @@ {% if not aPage.isPrintable %}
    {% for oTab in oUIBlock.GetSubBlocks() %} - {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} -
    - {% if oUIBlock.GetIsScrollable() %} -
    {{ oTab.GetTitle() }}
    - {% endif %} - {{ render_block(oTab, {aPage: aPage}) }} -
    - {% endif %} + {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} +
    + {% if oUIBlock.GetIsScrollable() %} +
    {{ oTab.GetTitle() }}
    + {% endif %} + {{ render_block(oTab, {aPage: aPage}) }} +
    + {% endif %} {% endfor %}
    {% else %} - {% for oTab in oUIBlock.GetSubBlocks() %} -
    -
    - {{ oTab.GetTitle() }} -
    - {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} - {{ render_block(oTab, {aPage: aPage}) }} - {% else %} -
    - {% endif %} -
    - {% endfor %} + {% for oTab in oUIBlock.GetSubBlocks() %} +
    +
    + {{ oTab.GetTitle() }} +
    + {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_HTML') %} + {{ render_block(oTab, {aPage: aPage}) }} + {% else %} +
    + {% endif %} +
    + {% endfor %} {% endif %} {% endblock %} diff --git a/templates/base/layouts/tab-container/layout.js.twig b/templates/base/layouts/tab-container/layout.js.twig index 81283816b..82613cc15 100644 --- a/templates/base/layouts/tab-container/layout.js.twig +++ b/templates/base/layouts/tab-container/layout.js.twig @@ -5,13 +5,14 @@ $('#{{ oUIBlock.GetId() }}').tab_container(); {% else %} {% for oTab in oUIBlock.GetSubBlocks() %} - oHiddeableChapters['tab_{{ oTab.GetId()|sanitize_identifier }}'] = '{{ oTab.GetTitle()|escape('js') }}'; - {% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_AJAX') %} - $.post('{{ oTab.GetUrl()|raw}}', {printable: '1'}, function(data){ - $('#tab_{{ oTab.GetId()|sanitize_identifier }} > .printable-tab-content').append(data); - }); - {% endif %} - {% endfor %} +oHiddeableChapters['tab_{{ oTab.GetId()|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_ELEMENT_IDENTIFIER')) }}'] = '{{ oTab.GetTitle()|escape('js') }}'; +{% if oTab.GetType() == constant('TabManager::ENUM_TAB_TYPE_AJAX') %} +$.post('{{ oTab.GetUrl()|raw }}', {printable: '1'}, function (data) +{ + $('#tab_{{ oTab.GetId()|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_ELEMENT_IDENTIFIER')) }} > .printable-tab-content').append(data); +}); +{% endif %} +{% endfor %} {% endif %} diff --git a/templates/pages/backoffice/ajaxpage/layout.html.twig b/templates/pages/backoffice/ajaxpage/layout.html.twig index a35d2c4de..6a7b37bf3 100644 --- a/templates/pages/backoffice/ajaxpage/layout.html.twig +++ b/templates/pages/backoffice/ajaxpage/layout.html.twig @@ -22,7 +22,7 @@ {% endblock %} {% if aPage.aJsFiles is not empty %} - {% set sId = oUIBlock.GetId() | sanitize_variable_name %} + {% set sId = oUIBlock.GetId() | sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) %} {% block iboPageJsFiles %}