Follow-up to #2506133: Replace SafeMarkup::set() in \Drupal\Core\Template\Attribute
Problem/Motivation
#2506133: Replace SafeMarkup::set() in \Drupal\Core\Template\Attribute has overlooked a fairly common use case, which now gets double escaped when translated titles are used as attributes.
Translated attribute values.
Example in core: feed_icon
$variables['attributes']['title'] = t('Message: @escaped', ['@escaped' => '<>']);
Will escape it once in t, if not marked safe, once in attributes, then once more in twig because the string changed in attributes.
If we mark it safe it still gets escaped at least twice...
Proposed resolution
Avoid htmlspecialchars() escaping in attribute if it's marked safe?
Remaining tasks
Review & commit
User interface changes
None
API changes
N/A
Comments
Comment #1
joelpittetComment #2
joelpittetAttributeValueBase also needs to be implementing SafeStringInterface because it's values can be printed directly. This should get a test to ensure this.
Comment #3
joelpittetHere's the fix, now needs some tests.
Comment #5
joelpittetI expect this will still fail but now it has tests to help aspire to (may need love).
Comment #6
star-szrComment #9
joelpittetComment #11
joelpittetComment #12
alexpottThis is deprecated - just need to check if it is MarkupInterface
Comment #14
nikunjkotechaComment #15
nikunjkotechaComment #25
smustgrave commentedIs this still relevant? I see htmlspecialchars was already replaced by something like Html::escape((string) $this->value);
Comment #26
smustgrave commentedBased on comment #25
Comment #28
quietone commentedThe patch is added tests for escaping Attributes. Since this was create the two test \Drupal\Tests\Core\Template\AttributeTest::testAttributeValues and \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeStringEscaping have been added which are doing the same testing.
Based on that and there has been no response for more information, I am closing this issue.
#2506133: Replace SafeMarkup::set() in \Drupal\Core\Template\Attribute
#2571673: Convert Views t() usage where it is used as an attribute value