Problem/Motivation

The views title is run through Xss::filter on render, but not in the preview, so some tags (e.g. <b>) will work in the preview but will not be displayed.

The preview should get Xss::filter to match the display behaviour.

Steps to reproduce

  1. Install Drupal
  2. Create a view with a block, adding <b></b> tags around the view title
  3. Confirm the view title is bold in the preview
  4. Save
  5. Add the block to the layout
  6. View the block, see that the title is not bold

Proposed resolution

Ensure that Xss::filter is applied to the preview output to match the display.

Remaining tasks

  1. Write a patch, with tests
  2. Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Original report by jjgw

What are the steps required to reproduce the bug?

  • create a new view block
  • enter the block title like "Recent blog links " and set this title between html tags for bold (or any other)

What behavior were you expecting?

  • The title is showed in bold both in the preview as when published

What happened instead?

  • The title is showed in bold in the preview
  • The title is not showed in bold when published

see attached images

Comments

jjgw created an issue. See original summary.

dawehner’s picture

I recommend you to use CSS to do those styles changes

Version: 8.2.2 » 8.2.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.2.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pameeela’s picture

Title: Difference between views preview and published view » Strip HTML tags from view title in preview, to match display
Version: 8.9.x-dev » 9.2.x-dev
Issue summary: View changes
Issue tags: +Bug Smash Initiative

Updated IS for clarity around the issue.

pameeela’s picture

Priority: Normal » Minor
pameeela’s picture

Version: 9.2.x-dev » 8.9.x-dev
ramya balasubramanian’s picture

Status: Active » Needs review
StatusFileSize
new1.62 KB

Hi @pameeela,
Here I have changed the markup to plain text in the preview section. Now Whatever tags we will give in the text field, it will strip those tags and print as a plain text in Preview section. While viewing the page, it will come along with the h2 tag which is by default added in the twig file. Please let me know if there are any issues.

abhijith s’s picture

StatusFileSize
new58.2 KB
new78.24 KB

Applied patch #8. It works fine. The markup of the view title in preview will be removed after this patch

Screenshots:
before patch
before

After patch
after

abhijith s’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: 2825683-8.patch, failed testing. View results

lendude’s picture

Issue tags: +Needs tests
+++ b/core/modules/views_ui/tests/src/Functional/PreviewTest.php
@@ -163,4 +163,14 @@ public function testPreviewError() {
+  /**
+   * Tests view title in the preview
+   */
+  public function testPreviewTitle() {
+    $title = "<h2>View Preview Title</h2>";
+    $this->drupalGet('admin/structure/views/view/test_preview_title/edit');
+    $this->assertEquals("View Preview Title", strip_tags($title));
+
+  }

This is testing the strip_tags method (it works!), the drupalGet doesn't do anything to influence the test. We need to assert that the striped text appears on the page. And in this case, it won't, because the 'test_preview_title' View that is enabled doesn't actually exist, so the drupalGet will see a 404

NitinLama’s picture

StatusFileSize
new504 bytes
new504 bytes

updating patch as per #12 suggestion.

NitinLama’s picture

Status: Needs work » Needs review
acbramley’s picture

Status: Needs review » Needs work

#13 is missing tests entirely.

abramm’s picture

What is the purpose of having HTML tags in a view title if they are always stripped?
It doesn't make much sense to me.

NitinLama’s picture

Agreed with @abramm

acbramley’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new5.36 KB
new4.87 KB

Here's a test-only patch + a patch with the existing fix which will also fail.

There are 2 places that need tags stripped - the preview section and the query info section. The existing fix only removes it from the former.

However, in my testing the following statement is not true:

You can add HTML tags to a view title, and they work as expected in the view preview, but are stripped from the display.

The tags are NOT stripped from the display when the view is actually rendered (not in preview). So is this actually a valid bug?

acbramley’s picture

Screenshots to show the above:

Before patch:

Before patch

After patch:

After patch

HTML of Views output with HTML included in title:

HTML output

The last submitted patch, 18: 2825683-18.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 18: 2825683-18-test-only.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new5.88 KB
new532 bytes

This strips the HTML tags from the query info section as well.

pameeela’s picture

So, it works for <strong> tags, but not for <b> tags (just triple confirmed this and that was the original issue). I assume that this means some tags *are* allowed?

Any way to find out which and then evaluate whether this works as designed, or needs to change?

pameeela’s picture

Title: Strip HTML tags from view title in preview, to match display » Use Xss::filter for view title to ensure preview matches match display
Issue summary: View changes

Updated IS to reflect the latest findings.

lendude’s picture

Status: Needs review » Needs work

So this needs to be updated to use Xss::filter and not strip_tags and then we need to test that the right tags get stripped.

ayushmishra206’s picture

Status: Needs work » Needs review
StatusFileSize
new516 bytes
new5.88 KB

Made the changes suggested in #25. Please review.

lendude’s picture

Status: Needs review » Needs work
+++ b/core/modules/views_ui/src/ViewUI.php
@@ -690,7 +690,7 @@ public function renderPreview($display_id, $args = []) {
+                  '#markup' => strip_tags($executable->getTitle()),

+++ b/core/modules/views_ui/views_ui.module
@@ -134,7 +134,7 @@ function views_ui_preprocess_views_view(&$variables) {
+      '#markup' => filter_var($view->getTitle()),

@ayushmishra206 You've only updated it in one spot and not with the method we were talking about.....

ayushmishra206’s picture

Status: Needs work » Needs review
StatusFileSize
new544 bytes
new5.88 KB
lendude’s picture

Status: Needs review » Needs work
+++ b/core/modules/views_ui/src/ViewUI.php
@@ -690,7 +690,7 @@ public function renderPreview($display_id, $args = []) {
+                  '#markup' => filter_var($executable->getTitle()),

+++ b/core/modules/views_ui/views_ui.module
@@ -134,7 +134,7 @@ function views_ui_preprocess_views_view(&$variables) {
+      '#markup' => filter_var($view->getTitle()),

It's still the wrong method, we were talking about Xss::filter().....

ayushmishra206’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB
new5.88 KB

Sorry for the mistakes.

The last submitted patch, 26: 2825683-26.patch, failed testing. View results

The last submitted patch, 28: 2825683-28.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 30: 2825683-30.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.08 KB
new6.68 KB
govind.maloo’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks good and applied cleanly. moving to RTBC.

naveenvalecha’s picture

Looks good too. RTBC +1

lendude’s picture

+++ b/core/modules/views_ui/tests/src/Functional/PreviewTest.php
@@ -163,4 +170,23 @@ public function testPreviewError() {
+    \Drupal::configFactory()->getEditable('views.view.test_preview_title')
+      ->set('display.default.display_options.title', '<b>Test preview title</b>')
+      ->save();

This looks good but couldn't we re-use the test_preview View for this instead of adding a whole new View?

acbramley’s picture

@Lendude totally could, but I've done similar before and was told to create a dedicated config for the specific test. I guess it means that if the re-used view was changed in any way it wouldn't affect our test but given we are testing a pretty small part of the view it might be ok.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/views_ui/src/ViewUI.php
@@ -690,7 +691,7 @@ public function renderPreview($display_id, $args = []) {
-                  '#markup' => $executable->getTitle(),
+                  '#markup' => Xss::filter($executable->getTitle()),

+++ b/core/modules/views_ui/views_ui.module
@@ -134,7 +135,7 @@ function views_ui_preprocess_views_view(&$variables) {
-      '#markup' => $view->getTitle(),
+      '#markup' => Xss::filter($view->getTitle()),

Atm these are both admin XSS filtered - all markup is. This could be achieved with

  '#markup' => $view->getTitle(),
  '#allowed_tags' => Xss::getHtmlTagList(),

for less Xss filtering - atm this string is being filtered twice.

I think not adding a whole new view is okay here too.

One question I have is how are we doing the eventual Xss filtering here. And I've found it... it's in \Drupal\views\Plugin\views\display\Page::execute() where we do...

      $render += [
        '#title' => ['#markup' => $this->view->getTitle(), '#allowed_tags' => Xss::getHtmlTagList()],
      ];

One funny thing is that $this->view->getTitle() will Xss admin filter the title already...

mohit_aghera’s picture

Assigned: Unassigned » mohit_aghera
mohit_aghera’s picture

Assigned: mohit_aghera » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.38 KB
new6.21 KB

- Remove the additional view.
- Perform the relevant changes in test cases, so we can test it with test_preview view.
- Add '#allowed_tags' => Xss::getHtmlTagList(), at both the places.
- Test cases are passing on local with test_preview view.

mitthukumawat’s picture

StatusFileSize
new31.53 KB
new31.94 KB

The patch #41 Applied successfully and the view title not appearing in bold now. Also it is not showing in bold when block placed in any region.

Version: 9.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ranjith_kumar_k_u’s picture

StatusFileSize
new3.39 KB

Rerolled #41 for 9.4.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Manibharathi E R’s picture

StatusFileSize
new213.34 KB
new206.47 KB

Patch #44 Applied and Tested successfully on Drupal 9.4.x.

Before patch
Before-Patch

After Patch
After-patch

gaurav-mathur’s picture

Assigned: Unassigned » gaurav-mathur
gaurav-mathur’s picture

Assigned: gaurav-mathur » Unassigned
StatusFileSize
new17.97 KB
new32.02 KB

Applied patch #44 successfully on drupal version 9.4.x and working fine. The markup of the view title in preview will be removed after this patch.
Refer to screenshot

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @gaurav-mathur but screenshots were already done in #46 so that was duplicate effort

Reviewing patch #44

On Drupal 10.1 with a standard install

Ran tests locally without the fix as I didn't see a test-only

Failed asserting that actual size 2 matches expected size 0.

So that's good

Double checked #46 and confirmed the issue is fixed.

Reviewing the code nothing seems off. Since this is the preview only I can't imagine this will break anything existing.

Thanks!

xjm’s picture

Title: Use Xss::filter for view title to ensure preview matches match display » Use Xss::filter() for the view title to ensure that the preview matches the actual display

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2825683-44.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Reviewed & tested by the community

Picking up again.
I tested the patch on local and it is passing the test cases added in re-roll #44
Patch is also getting applied cleanly on 10.1.x branch and passing on local on 10.1.x branch.

I've triggered the test-bot again as previous run's failures were related to date module.
Marking it as RTBC again so bot can pick up on next cycle.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed f953b423 to 10.1.x and 463dbb6b43 to 10.0.x and 1ae1111127 to 9.5.x. Thanks!

  • alexpott committed f953b423 on 10.1.x
    Issue #2825683 by acbramley, ayushmishra206, NitinLama, mohit_aghera,...

  • alexpott committed 463dbb6b on 10.0.x
    Issue #2825683 by acbramley, ayushmishra206, NitinLama, mohit_aghera,...

  • alexpott committed 1ae11111 on 9.5.x
    Issue #2825683 by acbramley, ayushmishra206, NitinLama, mohit_aghera,...

Status: Fixed » Closed (fixed)

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