Welcome to mirror list, hosted at ThFree Co, Russian Federation.

element.rb « docs « gitlab « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aea7ce76af0c827904b9f6f8cb64089ed607703c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

require 'cgi'

module Gitlab
  module Docs
    class Element
      def initialize(name, attributes)
        @name = name
        @attributes = attributes
      end

      def link?
        @name == 'a' && !href.to_s.empty?
      end

      def has_id?
        !id.to_s.empty?
      end

      def href
        @href ||= self.class.decode(attribute('href'))
      end

      def 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.to_a
          .find { |attr| attr.first == name }
          .to_a.last
      end
    end
  end
end