diff options
-rw-r--r-- | lib/gitlab/docs/element.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/docs/link.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/docs/page.rb | 6 | ||||
-rw-r--r-- | spec/gitlab/docs/page_spec.rb | 21 |
4 files changed, 33 insertions, 12 deletions
diff --git a/lib/gitlab/docs/element.rb b/lib/gitlab/docs/element.rb index a78b9a01..4d58edee 100644 --- a/lib/gitlab/docs/element.rb +++ b/lib/gitlab/docs/element.rb @@ -1,3 +1,5 @@ +require 'cgi' + module Gitlab module Docs class Element @@ -15,17 +17,25 @@ module Gitlab end def href - @href ||= attribute('href') + @href ||= self.class.decode(attribute('href')) end def id - @id ||= attribute('id') + @id ||= attribute('id')&.downcase + end + + def self.decode(name) + return if name.to_s.empty? + + CGI.unescape(name) end private def attribute(name) - @attributes.find { |attr| attr.first == name }&.last + @attributes.to_a + .find { |attr| attr.first == name } + .to_a.last end end end diff --git a/lib/gitlab/docs/link.rb b/lib/gitlab/docs/link.rb index 059ebaf1..23460f88 100644 --- a/lib/gitlab/docs/link.rb +++ b/lib/gitlab/docs/link.rb @@ -15,7 +15,7 @@ module Gitlab def anchor_name return unless @href.include?('#') - @href.partition('#').last.downcase + @href.partition('#').last end def internal_anchor? diff --git a/lib/gitlab/docs/page.rb b/lib/gitlab/docs/page.rb index 8bd5e9b9..463e3f36 100644 --- a/lib/gitlab/docs/page.rb +++ b/lib/gitlab/docs/page.rb @@ -6,8 +6,8 @@ module Gitlab def initialize(file) @file = file - @hrefs = [] - @ids = [] + @hrefs = Set.new + @ids = Set.new return unless exists? @@ -31,7 +31,7 @@ module Gitlab end def has_anchor?(name) - @ids.include?(name) + @ids.include?(Docs::Element.decode(name.downcase)) end def self.build(path) diff --git a/spec/gitlab/docs/page_spec.rb b/spec/gitlab/docs/page_spec.rb index 64cc98da..a9ee3264 100644 --- a/spec/gitlab/docs/page_spec.rb +++ b/spec/gitlab/docs/page_spec.rb @@ -25,8 +25,11 @@ describe Gitlab::Docs::Page do <a href="../link.html#my-anchor">See external file</a> <a href="#internal-anchor">See internal section</a> <a href="#internal-anchor-2">See internal section broken</a> + <a href="#utf-8-id-✔">See utf-8 anchor</a> + <a href="#utf-8-id-%E2%9C%94">See utf-8 anchor 2</a> <h1 id="internal-anchor">Some section</h1> + <h1 id="utf-8-id-✔">Some section 2</h1> </body </html> HTML @@ -34,7 +37,7 @@ describe Gitlab::Docs::Page do describe '#links' do it 'collects links on a page' do - expect(subject.links.count).to eq 3 + expect(subject.links.count).to eq 4 end end @@ -42,23 +45,31 @@ describe Gitlab::Docs::Page do it 'collects all hrefs' do expect(subject.hrefs).to match_array %w[../link.html#my-anchor #internal-anchor - #internal-anchor-2] + #internal-anchor-2 + #utf-8-id-✔] end end describe '#ids' do it 'collects all ids' do - expect(subject.ids).to match_array %w[internal-anchor] + expect(subject.ids).to match_array %w[internal-anchor + utf-8-id-✔] end end describe '#has_anchor?' do it 'returns true when anchor exists on a page' do - expect(subject.has_anchor?('internal-anchor')).to be true + expect(subject).to have_anchor('internal-anchor') end it 'returns false when anchors does not exist' do - expect(subject.has_anchor?('internal-anchor-2')).to be false + expect(subject).not_to have_anchor('internal-anchor-2') + end + + it 'returns true when UTF-8 encoded anchors are compared' do + expect(subject).to have_anchor('utf-8-id-✔') + expect(subject).to have_anchor('utf-8-id-%E2%9C%94') + expect(subject).to have_anchor('utf-8-id-%e2%9c%94') end end end |