summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Balmant <willchb@gmail.com>2019-04-10 20:50:26 +0000
committerWillian Balmant <willchb@gmail.com>2019-04-10 20:50:26 +0000
commit2075ef7d071286ad156ca13640336572bb20bceb (patch)
tree6c69724887832bab8d34943f15a05889a7967948
parentb6ca523cf7b8f7339d13d730c9fa24e7cac3263b (diff)
downloadgitlab-ce-2075ef7d071286ad156ca13640336572bb20bceb.tar.gz
No leading/trailing spaces when generating heading ids (Fixes #57528)
Update based on comments in MR #27025
-rw-r--r--lib/banzai/filter/table_of_contents_filter.rb4
-rw-r--r--spec/lib/banzai/filter/table_of_contents_filter_spec.rb5
2 files changed, 7 insertions, 2 deletions
diff --git a/lib/banzai/filter/table_of_contents_filter.rb b/lib/banzai/filter/table_of_contents_filter.rb
index d4cf08be0ae..8d79f5bd58b 100644
--- a/lib/banzai/filter/table_of_contents_filter.rb
+++ b/lib/banzai/filter/table_of_contents_filter.rb
@@ -17,8 +17,8 @@ module Banzai
# :toc - String containing Table of Contents data as a `ul` element with
# `li` child elements.
class TableOfContentsFilter < HTML::Pipeline::Filter
- PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
- LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}|\p{Space}$/
+ PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze
+ LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}+|\p{Space}+$/.freeze
def call
return doc if context[:no_header_anchors]
diff --git a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb
index 7213cd58ea7..f776e21a89e 100644
--- a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb
+++ b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb
@@ -58,6 +58,11 @@ describe Banzai::Filter::TableOfContentsFilter do
expect(doc.css('h1 a').first.attr('href')).to eq '#this-header-is-filled-with-punctuation'
end
+ it 'removes any leading or trailing spaces' do
+ doc = filter(header(1, " \r\n \t Title with spaces \r\n\t "))
+ expect(doc.css('h1 a').first.attr('href')).to eq '#title-with-spaces'
+ end
+
it 'appends a unique number to duplicates' do
doc = filter(header(1, 'One') + header(2, 'One'))