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
- Install Drupal
- Create a view with a block, adding
<b></b>tags around the view title - Confirm the view title is bold in the preview
- Save
- Add the block to the layout
- 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
- Write a patch, with tests
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | after_patch.jpg | 32.02 KB | gaurav-mathur |
| #48 | before_patch.jpg | 17.97 KB | gaurav-mathur |
| #46 | After-Patch-2825683-44.png | 206.47 KB | Manibharathi E R |
| #46 | Before-Patch-2825683-44.png | 213.34 KB | Manibharathi E R |
| #44 | 2825683-44.patch | 3.39 KB | ranjith_kumar_k_u |
Comments
Comment #2
dawehnerI recommend you to use CSS to do those styles changes
Comment #5
pameeela commentedUpdated IS for clarity around the issue.
Comment #6
pameeela commentedComment #7
pameeela commentedComment #8
ramya balasubramanian commentedHi @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.
Comment #9
abhijith s commentedApplied patch #8. It works fine. The markup of the view title in preview will be removed after this patch
Screenshots:

before patch
After patch

Comment #10
abhijith s commentedComment #12
lendudeThis 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
Comment #13
NitinLama commentedupdating patch as per #12 suggestion.
Comment #14
NitinLama commentedComment #15
acbramley commented#13 is missing tests entirely.
Comment #16
abrammWhat is the purpose of having HTML tags in a view title if they are always stripped?
It doesn't make much sense to me.
Comment #17
NitinLama commentedAgreed with @abramm
Comment #18
acbramley commentedHere'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:
The tags are NOT stripped from the display when the view is actually rendered (not in preview). So is this actually a valid bug?
Comment #19
acbramley commentedScreenshots to show the above:
Before patch:
After patch:
HTML of Views output with HTML included in title:
Comment #22
acbramley commentedThis strips the HTML tags from the query info section as well.
Comment #23
pameeela commentedSo, 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?
Comment #24
pameeela commentedUpdated IS to reflect the latest findings.
Comment #25
lendudeSo 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.
Comment #26
ayushmishra206 commentedMade the changes suggested in #25. Please review.
Comment #27
lendude@ayushmishra206 You've only updated it in one spot and not with the method we were talking about.....
Comment #28
ayushmishra206 commentedComment #29
lendudeIt's still the wrong method, we were talking about Xss::filter().....
Comment #30
ayushmishra206 commentedSorry for the mistakes.
Comment #34
acbramley commentedComment #35
govind.maloo commentedPatch looks good and applied cleanly. moving to RTBC.
Comment #36
naveenvalechaLooks good too. RTBC +1
Comment #37
lendudeThis looks good but couldn't we re-use the test_preview View for this instead of adding a whole new View?
Comment #38
acbramley commented@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.
Comment #39
alexpottAtm these are both admin XSS filtered - all markup is. This could be achieved with
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...
One funny thing is that $this->view->getTitle() will Xss admin filter the title already...
Comment #40
mohit_aghera commentedComment #41
mohit_aghera commented- Remove the additional view.
- Perform the relevant changes in test cases, so we can test it with
test_previewview.- Add
'#allowed_tags' => Xss::getHtmlTagList(),at both the places.- Test cases are passing on local with
test_previewview.Comment #42
mitthukumawat commentedThe 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.
Comment #44
ranjith_kumar_k_u commentedRerolled #41 for 9.4.
Comment #46
Manibharathi E R commentedPatch #44 Applied and Tested successfully on Drupal 9.4.x.
Before patch

After Patch

Comment #47
gaurav-mathur commentedComment #48
gaurav-mathur commentedApplied 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
Comment #50
smustgrave commentedThank 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
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!
Comment #51
xjmComment #53
mohit_aghera commentedPicking 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.
Comment #54
alexpottCommitted and pushed f953b423 to 10.1.x and 463dbb6b43 to 10.0.x and 1ae1111127 to 9.5.x. Thanks!