Problem/Motivation
It is currently not possible to output anything that resembles an HTML tag in an attribute using ui_patterns.
Looking at \Drupal\ui_patterns\Plugin\UiPatterns\PropType\AttributesPropType::normalizeAttrValue, the value goes through strip_tags but right after that, it goes in \Drupal\Core\Template\AttributeString::__toString to be HTML escaped.
On a larger scope, we should review all calls to strip_tags in the module and assess if it is appropriate.
Steps to reproduce
Using ui_suite_bootstrap, consider the following twig:
{# Case 1: using drupal attributes. #}
{% set foo_attr = 'Hello <em>world</em> and <<not-a-tag>> and <123> and <em2>OK</em2>.' %}
{% set btn_attrs = create_attribute({'data-foo': foo_attr}) %}
<button{{ btn_attrs }}>test</button>
{# Case 2: using SDC and ui_patterns. #}
{{ include('ui_suite_bootstrap:button', {
label: 'test',
attributes: {'data-foo': foo_attr}
}, with_context: false) }}
Raw output:
// Case 1: OK the attribute is html-escaped
<button data-foo="Hello <em>world</em> and <<not-a-tag>> and <123> and <em2>OK</em2>.">test</button>
// Case 2: NOK tags are stripped
<button data-foo="Hello world and and and OK." data-component-id="ui_suite_bootstrap:button" type="button">test</button>
Proposed resolution
Possibly remove strip_tags entirely since attributes are already escaped in \Drupal\Core\Template\AttributeString::__toString
Remaining tasks
todo
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Issue fork ui_patterns-3558573
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
herved commentedComment #3
just_like_good_vibesi will take that one
Comment #4
just_like_good_vibesComment #5
just_like_good_vibesif i understand well, you want to store HTML in a data-* attribute ? le me to gently remind that while it is not strictly forbidden, it is usually discouraged and not recommended.
Those attributes values hold strings, so yes you can stuff HTML markup in there, and yes it needs to be properly escaped.
But we originally decided to strip tags for attribute values and that was a design choice.
i will talk with the other maintainers of the module to check if we will support the usecase you are mentioning and come back to the issue.
Comment #6
herved commentedUsing a small amount of HTML in data-* attributes is generally acceptable, but as stated in the IS, this filters anything that resembles an HTML tag, but isn't necessarily.
Some example strings and their output after strip_tags: https://3v4l.org/NoIPv
Comment #9
just_like_good_vibes