From dc0b13755379c1cf6f98c067cb5dcc2fdc531dfb Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 22 Nov 2019 14:09:10 +0000
Subject: [PATCH] Issue #2999696 by Yaremchuk, mbovan, Berdir: Add
 rel="nofollow" to table sort headers

---
 core/lib/Drupal/Core/Utility/TableSort.php                  | 2 +-
 core/modules/views/templates/views-view-table.html.twig     | 4 ++--
 .../views/tests/src/Functional/Handler/FieldWebTest.php     | 6 +++++-
 .../classy/templates/views/views-view-table.html.twig       | 4 ++--
 .../stable/templates/views/views-view-table.html.twig       | 4 ++--
 5 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/core/lib/Drupal/Core/Utility/TableSort.php b/core/lib/Drupal/Core/Utility/TableSort.php
index 7e02c14c3259..b513a2b8a53b 100644
--- a/core/lib/Drupal/Core/Utility/TableSort.php
+++ b/core/lib/Drupal/Core/Utility/TableSort.php
@@ -81,7 +81,7 @@ public static function header(&$cell_content, array &$cell_attributes, array $he
         $image = '';
       }
       $cell_content = Link::createFromRoute(new FormattableMarkup('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), '<current>', [], [
-        'attributes' => ['title' => $title],
+        'attributes' => ['title' => $title, 'rel' => 'nofollow'],
         'query' => array_merge($context['query'], [
           'sort' => $context['sort'],
           'order' => $cell_content,
diff --git a/core/modules/views/templates/views-view-table.html.twig b/core/modules/views/templates/views-view-table.html.twig
index 0c0b4458363d..2bc1a9e4c51a 100644
--- a/core/modules/views/templates/views-view-table.html.twig
+++ b/core/modules/views/templates/views-view-table.html.twig
@@ -75,14 +75,14 @@
             {%- if column.wrapper_element -%}
               <{{ column.wrapper_element }}>
                 {%- if column.url -%}
-                  <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                  <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
                 {%- else -%}
                   {{ column.content }}{{ column.sort_indicator }}
                 {%- endif -%}
               </{{ column.wrapper_element }}>
             {%- else -%}
               {%- if column.url -%}
-                <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
               {%- else -%}
                 {{- column.content }}{{ column.sort_indicator }}
               {%- endif -%}
diff --git a/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php b/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php
index 25026c9a0772..0ad03f02f856 100644
--- a/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php
+++ b/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php
@@ -84,10 +84,14 @@ public function testClickSorting() {
 
     // Clicking a click sort should change the order.
     $this->clickLink(t('ID'));
-    $this->assertLinkByHref(Url::fromRoute('<none>', [], ['query' => ['order' => 'id', 'sort' => 'desc']])->toString());
+    $href = Url::fromRoute('<none>', [], ['query' => ['order' => 'id', 'sort' => 'desc']])->toString();
+    $this->assertLinkByHref($href);
     // Check that the output has the expected order (asc).
     $ids = $this->clickSortLoadIdsFromOutput();
     $this->assertEqual($ids, range(1, 5));
+    // Check that the rel attribute has the correct value.
+    $result = $this->xpath('//a[@href="' . $href . '"]');
+    $this->assertEquals('nofollow', $result[0]->getAttribute('rel'));
 
     $this->clickLink(t('ID Sort descending'));
     // Check that the output has the expected order (desc).
diff --git a/core/themes/classy/templates/views/views-view-table.html.twig b/core/themes/classy/templates/views/views-view-table.html.twig
index 8eccec0418aa..990ecb8b90a3 100644
--- a/core/themes/classy/templates/views/views-view-table.html.twig
+++ b/core/themes/classy/templates/views/views-view-table.html.twig
@@ -75,14 +75,14 @@
             {%- if column.wrapper_element -%}
               <{{ column.wrapper_element }}>
                 {%- if column.url -%}
-                  <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                  <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
                 {%- else -%}
                   {{ column.content }}{{ column.sort_indicator }}
                 {%- endif -%}
               </{{ column.wrapper_element }}>
             {%- else -%}
               {%- if column.url -%}
-                <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
               {%- else -%}
                 {{- column.content }}{{ column.sort_indicator }}
               {%- endif -%}
diff --git a/core/themes/stable/templates/views/views-view-table.html.twig b/core/themes/stable/templates/views/views-view-table.html.twig
index 6806eb589938..1f4910ab48f6 100644
--- a/core/themes/stable/templates/views/views-view-table.html.twig
+++ b/core/themes/stable/templates/views/views-view-table.html.twig
@@ -73,14 +73,14 @@
             {%- if column.wrapper_element -%}
               <{{ column.wrapper_element }}>
                 {%- if column.url -%}
-                  <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                  <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
                 {%- else -%}
                   {{ column.content }}{{ column.sort_indicator }}
                 {%- endif -%}
               </{{ column.wrapper_element }}>
             {%- else -%}
               {%- if column.url -%}
-                <a href="{{ column.url }}" title="{{ column.title }}">{{ column.content }}{{ column.sort_indicator }}</a>
+                <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
               {%- else -%}
                 {{- column.content }}{{ column.sort_indicator }}
               {%- endif -%}
-- 
GitLab