You are here

function locale_string_is_safe in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/locale.module \locale_string_is_safe()

Check that a string is safe to be added or imported as a translation.

This test can be used to detect possibly bad translation strings. It should not have any false positives. But it is only a test, not a transformation, as it destroys valid HTML. We cannot reliably filter translation strings on import because some strings are irreversibly corrupted. For example, a & in the translation would get encoded to & by \Drupal\Component\Utility\Xss::filter() before being put in the database, and thus would be displayed incorrectly.

The allowed tag list is like \Drupal\Component\Utility\Xss::filterAdmin(), but omitting div and img as not needed for translation and likely to cause layout issues (div) or a possible attack vector (img).

2 calls to locale_string_is_safe()
PoDatabaseWriter::importString in core/modules/locale/src/PoDatabaseWriter.php
Imports one string into the database.
TranslateEditForm::validateForm in core/modules/locale/src/Form/TranslateEditForm.php
Form validation handler.

File

core/modules/locale/locale.module, line 1037
Enables the translation of the user interface to languages other than English.

Code

function locale_string_is_safe($string) {
  return Html::decodeEntities($string) == Html::decodeEntities(Xss::filter($string, array(
    'a',
    'abbr',
    'acronym',
    'address',
    'b',
    'bdo',
    'big',
    'blockquote',
    'br',
    'caption',
    'cite',
    'code',
    'col',
    'colgroup',
    'dd',
    'del',
    'dfn',
    'dl',
    'dt',
    'em',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'hr',
    'i',
    'ins',
    'kbd',
    'li',
    'ol',
    'p',
    'pre',
    'q',
    'samp',
    'small',
    'span',
    'strong',
    'sub',
    'sup',
    'table',
    'tbody',
    'td',
    'tfoot',
    'th',
    'thead',
    'tr',
    'tt',
    'ul',
    'var',
  )));
}