Problem/Motivation

If you have a token like [term:name] and term name like First & Second then you get First & Second.

Proposed resolution

Using tokens in HTML context is currently a pain, I'm not sure yet sure what to do about. I guess the problem is that the output of the tokens is somewhere converted and because it's not marked as safe, the same happens again when printing it out with auto escape.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Berdir created an issue. See original summary.

berdir’s picture

Title: Certain chracters in token page titles are double-encoded » Certain characters in token page titles are double-encoded
tduong’s picture

Status: Active » Needs review
StatusFileSize
new1.02 KB

It seems like it displays the title wrong already when creating a simple node with special chars in the title. Uploaded an edited test form an existing one just as starting point. Will fail.

Status: Needs review » Needs work

The last submitted patch, 3: title_double_encoded-2668980-3.patch, failed testing.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new724 bytes
new1.85 KB

Just for now we've tried to fix the page title rendering with the change in interdiff, but the wrong encoding at the beginning of the test still exists.

Status: Needs review » Needs work

The last submitted patch, 5: title_double_encoded-2668980-5.patch, failed testing.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB
new2.45 KB

Test fails because in that node view it's just on first encoding and it displays the special & as &. Test updated.

tim.plunkett’s picture

  1. +++ b/src/Tests/PageNodeSelectionTest.php
    @@ -52,8 +52,10 @@ class PageNodeSelectionTest extends WebTestBase {
    +    // Updated test for first encoding assertion.
    +    // See https://www.drupal.org/node/2668980.
    

    You don't need a comment like this

  2. +++ b/src/Tests/PageNodeSelectionTest.php
    @@ -118,6 +120,7 @@ class PageNodeSelectionTest extends WebTestBase {
         $this->assertTitle($node2->label() . ' | Drupal');
    ...
    +    $this->assertRaw($node2->getTitle());
    

    What's the difference between getTitle() and label()?

tduong’s picture

Assigned: Unassigned » tduong
StatusFileSize
new868 bytes
new2.35 KB

#8.1 done
#8.2 no difference actually, but maybe would be more sensitive to make both of them getTitle() and specify (on node creation) that we are testing for title with special HTML characters (?). Changed it in label() for now.

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Tests/PageNodeSelectionTest.php
@@ -52,8 +52,8 @@ class PageNodeSelectionTest extends WebTestBase {
-    $this->assertText($node2->label());
-    $this->assertTitle($node2->label() . ' | Drupal');
+    $this->assertRaw('First & Second');
+    $this->assertTitle('First & Second | Drupal');

@@ -118,6 +118,7 @@ class PageNodeSelectionTest extends WebTestBase {
     $this->assertText($node2->body->value);
+    $this->assertRaw($node2->label());

This seems to indicate that our fix is not consistent with the default node page title.

For core, it is encoded once. In our case, it's not.

Please expand the test title to something like First & <Second>. Those should be displayed on the page and not treated as an HTML tag.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new2.2 KB
new1.67 KB

Berdir on skype:
I'm worried that we don't escape the tags anymore at all so I want to see that tested [...]
if it *visually* looks ok, then the raw markup for it is fine as well and you should adjust the test

Adjusted test and reverted fix in PageBlockDisplayVariant::renderPageTitle().

tduong’s picture

StatusFileSize
new1.18 KB
new1.75 KB
new2.58 KB

Discussed with @berdir and the last assertRaw needs to be more specific. Readd fix in PageBlockDisplayVariant::renderPageTitle(). What we expect is both test and test_only patches to fail.

The last submitted patch, 12: title_double_encoded-2668980-12-test_only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 12: title_double_encoded-2668980-12.patch, failed testing.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new915 bytes
new1.75 KB
new2.97 KB

@berdir suggested to put Markup::create(Html::escape( .. )) around the previous return value in PageBlockDisplayVariant::renderPageTitle(). This should make the test to pass.

The last submitted patch, 15: title_double_encoded-2668980-15-test_only.patch, failed testing.

berdir’s picture

tim.plunkett’s picture

Issue tags: +beta blocker triage
arla’s picture

Issue summary: View changes
  1. Don't Html::escape and PlainTextOutput::renderFromHtml cancel each other out? At least for the characters in the test.
  2. If it is only the long line with nested calls that is ugly, we could just break it down to:
    $title = $this->token->replace($page_title, $data);
    return Markup::create(Html::escape(PlainTextOutput::renderFromHtml($title)));
    

    And maybe add a comment excusing the nesting like "Special characters in token values are encoded during replacement, so recode them and mark it as safe to avoid autoescape which would result in double encoding."

  3. Perhaps it would be beneficial to add actual HTML tag usage to the test case, more to illustrate the functionality than make sure that it works. (If I understand correctly, actual HTML will be stripped by PlainTextOutput?) So <em>First</em> & <Second> would become First & <Second>?
berdir’s picture

Status: Needs review » Needs work

1. I think they don't, this was the only thing that worked for us.
2. Yes, lets split it up.
3. Agree with adding that. would get escaped as well I *think*, we'll see.

arla’s picture

Status: Needs work » Needs review
StatusFileSize
new2.34 KB
new3.04 KB
arla’s picture

Them cancelling each other out.

berdir’s picture

They don't really cancel themself out in general. renderFromHtml() first strips HTML tags.

The reason those here are actually kept is because token replace does weird things to the replacement values and enforces them to get escaped. Then they're put into the string and at that point, are being escaped. And then later, escaped again.

The problem is, that only happens for token replacements. What we need to do in the test is extend it and once also use that node title directly in page manager. Then you can see that your latest patch doesn't escape anymore at all.

But neither does the previous patch work, at least not in a consistent way, as it actually removes the tag then.

I found a new approach that seems to be working consistently, by escaping the input text first as well and then creating a Markup object. I think token should offer something like that as an API.

berdir’s picture

Added a @todo on the core issue

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Looks great! Thanks for adding the @todo.

arla’s picture

Assigned: tduong » Unassigned

Looks good to me too!

arla’s picture

Status: Reviewed & tested by the community » Needs review

Looks like with the latest patch, the HTML <title> content is broken.

arla’s picture

Status: Needs review » Needs work

Ah, what I was seeing was that $page_title was "", in which case replace() returned the unmodified HtmlEscapedText object. Later, in template_preprocess_html(), the if (!empty($variables['page']['#title'])) { condition passed, resulting in the empty "" being used as title rather than the default "Home".

Not sure what the most technically correct solution to that would be, but a simple option is to cast the replace() result to string.

arla’s picture

Added the string cast and created a test to prove it's needed. The test-only patch adds that test to the previous patch, but does not add the string cast.

The last submitted patch, 30: title_double_encoded-2668980-30-TEST_ONLY.patch, failed testing.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Bump :)

japerry’s picture

Status: Reviewed & tested by the community » Fixed

This looks good and tested good by me. Fixed!

  • japerry committed e175811 on 8.x-1.x authored by Berdir
    Issue #2668980 by tduong, Arla, Berdir: Certain characters in token page...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.