Problem/Motivation
The template file links.html.twig has a lot of whitespace modifiers. These dashes here, {%- if heading -%}
They were originally added when the template was converted, but don't seem to serve a purpose and can be removed. They might have been there originally for tests but joelpittet confirmed they aren't needed for that now. See the patch here, https://www.drupal.org/node/2465399#comment-9829099
If they can be removed, remove them from the template in the System module, and the template in Classy. To determine if they can be removed we need to look for any instances where links might be rendered inline. That is a place where whitespace could be an issue.
Also, this template has a wrapping if statement:
{% if links -%}
...
{%- endif %}
The if was a direct copy of functionality from when this was a theme function. We cannot verify why it is still needed. Investigate whether it can be removed, and, if so, remove it.
If this issue is worked on after #2455211: Comment field displayed last regardless of assigned weight gets committed, investigate removing the if from the links template in Bartik, as well.
Update : Made the changes in the core links.html.twig template to fix the white-spaces issue as mentioned by Cottser
https://www.drupal.org/node/2472591#comment-10878510
Steps to reproduce
Proposed resolution
Remaining tasks
See #127
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Beta phase evaluation
| Issue category | Task because it's not broken, just minor cleanup to the template |
|---|---|
| Issue priority | Normal, this only changes the appearance of the template code and whitespace in markup. |
| Unfrozen changes | Unfrozen because it only changes templates. |
| Disruption | Not disruptive at all. |
| Comment | File | Size | Author |
|---|---|---|---|
| #118 | interdiff-117_118.txt | 2.21 KB | gauravvvv |
| #118 | 2472591-118.patch | 6.76 KB | gauravvvv |
| #117 | 2472591-117.patch | 6.76 KB | gauravvvv |
| #115 | 2472591-nr-bot.txt | 173 bytes | needs-review-queue-bot |
| #103 | 2472591-103-interdiff.txt | 5.04 KB | thehuffman |
Comments
Comment #1
nlisgo commentedThe purpose is to remove whitespace.
http://twig.sensiolabs.org/doc/templates.html#whitespace-control
If there is a desire to remove these modifiers from the twig templates then we will have to change many more templates than this one.
There are currently 139 whitespace modifiers across 41 template files:
Comment #2
davidhernandezThere is no particular desire to remove them everywhere. They are needed in many places, especially in places where the resultant markup will definitely be used inline. Just to clarify, I opened the issue about the links template, in particular, because it is being used heavily (on almost every line) and it may not be necessary. I'm also a bit more interested in this 'if' business, if someone tracks that down.
Comment #3
disasm commentedWhat's the reason for removing them at all? Who wants a whole bunch of empty line breaks from branches and loops in the outputted file?
Comment #4
mark.labrecqueComment #5
mark.labrecqueComment #6
mark.labrecqueComment #8
mark.labrecqueThese failing tests are confusing to me. Why would such a simple change cause MySQL errors? I re-created the patch after a merge from 8.0.x. Hopefully, this will pass...
Comment #9
mark.labrecqueComment #10
mark.labrecqueComment #12
mark.labrecqueTestbot doesn't seem to like my patch for some odd reason. This patch is from @joelpittet, which is shown in the issue description.
Comment #13
mark.labrecqueComment #14
cilefen commentedDoes this interdiff make it clearer?
interdiff remove-excess-whitespace-modifiers_0.patch drupal-2472591-remove-link-template-whitespace-8.patch > interdiff-0-8.txtComment #15
star-szrThe question of whether we can remove the 'if' should be a separate issue IMO.
This needs to update the Classy template as well.
Comment #16
mark.labrecqueComment #17
mark.labrecqueComment #19
mark.labrecqueRe-writing tests to be more flexible and allow for "non-hardcoded" HTML output.
Comment #20
mark.labrecqueI wasn't able to complete this one yet, but I will re-visit it in a couple days if no one else has completed it. The latest patch actually does work, but the test needs to be rewritten in order for it to pass. Currently the template's output is being compared against a hardcoded HTML output string, and it should be converted to something like an xpath pattern in order to make this particular test more flexible.
The test in question can be found at /core/modules/block/src/Tests/Views/DisplayBlockTest.php::testBlockContextualLinks()
The last two lines:
Should be changed to something like this:
The above xpath asssertion does not actually work, but I think it's on the right track. I would be interested in seeing what others are able to come up with.
Comment #21
mark.labrecqueComment #22
mark.labrecqueComment #23
lauriiiCould we write somewhere down that people don't write this kind of tests anymore because they are PITA to test.
Comment #24
connorwk commentedAt the LA extended sprints, going to work on this issue.
Comment #25
connorwk commentedAdding an interdiff for the patch in comment 16 as there wasn't one.
Comment #26
mark.labrecque@laurii which type of tests? Xpath assertions? Is there another form of testing that would serve us here? I am fairly green to writing tests with simplytest, so I am open to suggestions.
Thanks!
Comment #27
lauriii@mark.labrecque sorry for being a little unfinished on my comment. So I mean its good idea to replace this kind of tests where we test hard coded markup and we should document that its problematic so we don't get any more of this kind of tests. Xpath + assertions way to go!!
Comment #28
mark.labrecqueDon't you mean un-Finnish in your comment? Sorry, I couldn't resist :)
Thanks for your response
Comment #29
lauriiiI hope someone would teach me to un-Finnish my comments so they would be easier to read ;)
Comment #30
jaredsmith commentedI'm working with @connorwk at the DrupalCon Los Angeles extended sprints, and this is our first attempt at doing a patch using XPath to avoid whitespace problems in the test assertions. This should make the ExpandDrupal\views_ui\Tests\DisplayTest test no longer fail. (This obviously isn't a complete patch -- I'm just attempting to mentor @connorwk and see if this method might work.)
Comment #31
connorwk commentedThe patch @jaredsmith submitted is not working. I will continue to work on this to figure out what is needed to be done.
Comment #32
yesct commentedComment #34
connorwk commentedSo I fixed the first failed test in Drupal\block\Tests\Views\DisplayBlockTest
I was posting my progress to allow others to review my work and let me know if what I am doing seems correct.
I replaced assertIdentical with xpath to fix the problem we have when we remove the whitespace modifiers.
I will continue to work on the other tests in the mean time.
Comment #35
star-szrThanks @connorwk, good to see you again at DCLA. Setting to needs review.
Comment #37
lauriiithe test looks _way better_ to be! Good work on that! Hopefully we can still make rest of the tests pass
Comment #38
jaredsmith commentedGood work @connorwk. Thanks for participating in the extended sprint at DrupalCon Los Angeles. Now that you've got one test passing, you can use that same technique to fix up the other failing tests.
Keep up the great work!
Comment #39
mark.labrecqueComment #40
mark.labrecqueComment #41
mark.labrecqueComment #42
mark.labrecqueCurrently, I am blocked on this, as when the latest patch is applied to my local install, the tests expected to pass are failing due to the following message:
These same tests are passed by the testbot, so this could simply be due to some local setting/extension needing to be enabled/installed, so I will look into this when I get some time.
Comment #43
connorwk commentedAlright this patch should finally fix all the tests that were previously failing.
Most of it was switching over from using assertIdentical over to xpath.
Using assertIdentical is a problem because if we just rewrote the string to work with assertIdentical if one white-space is added into the html it again breaks the test, so using xpath should prevent such problems again.
But for Drupal\system\Tests\Theme\FunctionsTest I had to do some other things.
First off we were using assertThemeOutput for testing the output, upon further inspection of why this function was failing it also used assertIdentical which we know was broken by removing the whitespace modifiers.
Since I couldn't simply rewrite assertThemeOutput as it was used in other places I created a new test function which is a slight rewrite of it called assertThemeOutputByXPath. It takes in the same info but the $expected variable shall now be formatted for the xpath function.
Also in the later tests (also in Drupal\system\Tests\Theme\FunctionsTest) which where failing the strings fed into assertEqual needed to be trimmed with the trim function because there was some extra whitespace around it now.
On another note maybe we should change the other locations that use assertThemeOutput with my new function assertThemeOutputByXPath to prevent other tests breaking in the future by white-spaces? Xpath seems to be the more proper way to test for nodes like this instead of seeing if two strings are identical. Maybe I should open another issue for this? Let me know what you guys think, assertThemeOutput isn't used in to many other locations so doing so wouldn't be too much of a tremendous task. I'd even do it if everyone thinks it should be done.
I should also note I removed checking of class information as per a suggestion from @davidhernandez so if you ask why that is gone I can't answer you other then I was told to avoid checking them :)
Comment #44
snetcher commentedComment #45
lauriiiI'm wondering if this is actually a BC breaking change because of display: inline-block; is being messed in case list has spaces between its items.
Comment #46
vpanicke commentedComment #47
vpanicke commentedForgot to assign. Working on this issue.
Comment #48
star-szrAt this point we should probably leave Classy and Stable alone and just make this change in the core links.html.twig template, then we don't need to worry about BC :)
Comment #49
vpanicke commentedMade the changes in the core links.html.twig template @Cottser
Comment #50
vpanicke commentedChanged the status to needs review.
Comment #51
vpanicke commentedComment #52
star-szrThanks @vpanicke that's looking better! Setting to needs work for the issue summary update.
Comment #53
vpanicke commentedThanks @Cottser , Updated the Summary as well.
Comment #56
vpanicke commentedCorrected the Version number
Comment #57
vpanicke commentedUpdated the tests
Comment #58
star-szrThis needs peer review, @vpanicke generally we don't RTBC our own patches unless it's a trivial fix or something which is sometimes done as part of a patch review. Thanks!
Comment #59
vpanicke commentedThanks @Cottser ,was not aware of that. Please can you help review close the same.
Comment #60
emma.mariaComment #62
joelpittetDidn't know about this, thanks this looks great. There are a bunch of block level elements so the whitespace shouldn't be hurt from this change, the only span in there didn't change, heading.level could be an inline element potentially, but not realistically and also no whitespace elements needed on it.
Comment #63
alexpottAre we sure that we should be adding all this whitespace inside the li's?
It would be great to have a comparison between what this template currently outputs and what it will with all the changes.
Comment #64
joelpittetHere's the output before and after the patch. And the patch only affects core/system and not stable.
http://twigfiddle.com/68oi2r
Before:
After:
I can see a scenario where the LI's are changed to inline elements and the whitespace could matter then for the inside. We could mitigate this by just removing the whitespace inside the
<li>elements. I don't think the rest would matter in any practical sense.Second opinions?
Comment #65
amit.drupal commentedlinks.html.twig File in two place.
core/modules/system/templates/links.html.twig
core/themes/classy/templates/navigation/links.html.twig
Comment #67
amit.drupal commentedPlease suggest where me wrong.
Comment #68
lomasr commentedI agree with joelpittet's comment , we just need to remove white space inside the 'li' elements.
Comment #69
lnadella commentedUnable to apply patch on latest 8.3.x branch output is here for git apply:
Comment #70
felribeiro commentedReroll to 8.3.
Comment #72
manuel garcia commentedComment #73
imshivani commentedI have attached patch for the issue. Please review it.
Comment #74
imshivani commentedComment #75
Anonymous (not verified) commentedI can not understand why you are so eager to get these terrible holes between a tags? I have a real case when empty tags must be empty #2789909: Remove spaces around row.content (in shot, css
:empty- not working with holes). Twig is still not able to beautifully arrange spaces (see meta-tags output for example :)). A browser editor (see commandcode element) are displayed well no matter spaces. Therefore, improvement in this issue do not make sense to me.Comment #77
manjit.singh@vaplas I am adding this as related issue.
Comment #79
aditya.ghan commentedWe will work on this issue in DrupalMumbaiCodeSprint
Comment #80
pk188 commentedComment #82
rajeshwari10 commentedComment #83
rajeshwari10 commentedRemoved whitespace in links.html.twig also added interdiff
Thanks!!
Comment #84
rajeshwari10 commentedComment #86
brahmjeet789 commented@all i have removed the white space please check the patch.
Comment #87
brahmjeet789 commentedComment #90
brahmjeet789 commentedAdded patch for remove whitespace we can use {% spaceless %} {% endspaceless %}for remove whitespace in twig
Comment #91
brahmjeet789 commentedComment #94
spitzialist commentedLast provided patch "node-2472591-90.patch" is empty. What should be reviewed here?
Also, for the patch before that ("2472591-86.patch") the interdiff is missing.
Comment #95
bbarunte commentedRemoved dashes and added spaceless in core/modules/system/templates/links.html.twig, core/themes/classy/templates/navigation/links.html.twig, core/themes/stable/templates/navigation/links.html.twig, test is passing
Comment #96
bbarunte commented...sorry, i don't get it - please someone take over...
Comment #97
cleverington commentedDavid notes on #2 (https://www.drupal.org/project/drupal/issues/2472591#comment-9834761), we do not need to remove the whitespace modifiers everywhere. Only in places where it is simply unnecesary.
Patch #83 and #86 both failed due to a large addition of unexpected white space created by this.
Cottser notes in #48 that we should only be updating core
links.html.twigto preserve backwards compatibility.Using the twigfiddle from #64 ( http://twigfiddle.com/68oi2r ), the solution for #95 actually creates unnecessary whitespace around each list item, due to the reduced whitespace modifiers. This is what is failing the tests.
The cleanest code appears to be a mix of
{% spaceless %}and-as on the attached patch.However, does having whitespace between the
<li> </li>go against Drupal Coding Stanards as noted at https://www.drupal.org/docs/develop/coding-standards/twig-coding-standar... ? Not sure.Comment #98
WidgetsBurritos commented+1 we've reviewed this at the 2019 Austin Contribution Weekend. The use of the whitespace modified within the
<li>tags makes sense to me.Comment #99
mithun sRemoved dashes in core/modules/system/templates/links.html.twig
Comment #100
nlisgo commented@Mithun S thanks for the contribution. Could you upload an interdiff also please. Makes it easier to review.
https://www.drupal.org/documentation/git/interdiff
Comment #101
mithun sDon't consider the patch in #99. Patch in #97 only addresses links.html.twig in stable theme. What about the other links.html.twig in classy theme and system module as shown? Aren't they also part of core folder? Could somebody guide me on this?
Comment #103
thehuffman commentedWorking on this issue at Friday contribution sprints at DrupalCon Seattle 2019.
Patch includes changes for Classy links.html.twig file, and core links.html.twig file. The spaceless tag now wraps the output of all markup, but the shorthand spaceless dashes remain in the unordered list markup.
Comment #104
sastha commentedComment #105
adrian_s_m commentedI'm reviewing this at Drupalcon Seattle 2019!
Comment #106
mithun sDoes anyone have any updates on this issue?
Comment #107
mithun sComment #115
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #116
Aadhar_Gupta commentedI have tested the patch provided in comment #103 and it is successfully applied in drupal 10.1.
Comment #117
gauravvvv commentedPatch #103, no longer applies to 10.1.x. I have re-rolled the patch. Please review
Comment #118
gauravvvv commentedComment #119
smustgrave commentedThis is the outcome using patch #118 is that desired?
Comment #120
smustgrave commentedThink this will need a different solution.
https://twig.symfony.com/doc/2.x/tags/spaceless.html
Not sure that's the approach we want to take
Comment #121
mukhtarm commentedApplied the patch on #18 cleanly.
Comment #122
rohan-sinha commentedAddressed #119 and #120, used https://twig.symfony.com/doc/2.x/tags/spaceless.html
Comment #123
smustgrave commented120 was not addressed.
Comment #124
smustgrave commentedAnd what was addressed for 119? Didn’t say anything was off? Just stating what I saw.
Relooking at this and 118 May actually be correct. Moving review for 118
Comment #125
rohan-sinha commented@smustgrave yeah now i noticed spaceless filter
{{
"
"|spaceless }}
is #118 good or should I make a patch to spaceless filter
Comment #126
smustgrave commentedThink 118 is fine.
Comment #127
catchThis needs an issue summary update:
1. Do we actually need spaceless, or could we just remove the modifiers with no replacement? It adds an extra level of indentation and we don't tend to strip whitespace everywhere else.
2. What is/was the outcome of this from the issue summary? We should either try to figure that out, or open a follow-up.
Comment #130
quietone commentedBased on #127 I am removing the 'Novice tag'