class Rack::Relations::Anchor

Attributes

node[RW]
safelist_domains[RW]
uri[RW]

Public Class Methods

new(node, safelist_domains:) click to toggle source
# File lib/rack/relations/anchor.rb, line 8
def initialize(node, safelist_domains:)
  @node = node
  @safelist_domains = safelist_domains
  @uri = URI(node.attr("href")) rescue nil
end

Public Instance Methods

modified_rel() click to toggle source
# File lib/rack/relations/anchor.rb, line 14
def modified_rel
  "#{node.attr("rel")} nofollow noopener noreferrer".strip
end
safe?() click to toggle source
# File lib/rack/relations/anchor.rb, line 18
def safe?
  return true if uri.relative?

  safelist_domains.any? { |matcher| match?(matcher: matcher, host: uri.host) }
end

Private Instance Methods

match?(matcher:, host:) click to toggle source
# File lib/rack/relations/anchor.rb, line 24
        def match?(matcher:, host:)
  case matcher
  when String
    host.end_with?(matcher)
  when Regexp
    host =~ matcher
  end
end