Sha256: 972f40cfb2c668cf7aa47b13ad3340c0c69cf6cfb69352227d1f9a5cce7efda9

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Webmention
  module Verification
    module Verifiers
      class HtmlVerifier < BaseVerifier
        @mime_types = ['text/html']

        Verifiers.register(self)

        HTML_ATTRIBUTE_MAP = {
          cite:   %w[blockquote del ins q],
          data:   %w[object],
          href:   %w[a area],
          poster: %w[video],
          src:    %w[audio embed img source track video],
          srcset: %w[img source]
        }.freeze

        private

        def doc
          @doc ||= Nokogiri::HTML(response_body)
        end

        def parse_response_body
          HTML_ATTRIBUTE_MAP.each_with_object([]) { |(*args), matches| matches << search_doc(*args) }.flatten
        end

        def search_doc(attribute, elements)
          regexp = attribute == :srcset ? srcset_attribute_regexp : target_regexp

          doc.css(*elements.map { |element| "#{element}[#{attribute}]" }).find_all do |node|
            node[attribute].match?(regexp)
          end
        end

        def srcset_attribute_regexp
          @srcset_attribute_regexp ||= /(?:^|\b)#{target_regexp_str}\s/
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webmention-verification-5.0.0 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-4.0.0 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-3.0.0 lib/webmention/verification/verifiers/html_verifier.rb