Sha256: 479ceabe6f316159d101b0a0c76fef0b91c4b4fb8eb648753b77dc4066b79139

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module Webmention
  module Verification
    module Verifiers
      class HtmlVerifier < BaseVerifier
        def self.mime_types
          ['text/html']
        end

        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

5 entries across 5 versions & 1 rubygems

Version Path
webmention-verification-2.0.0 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-1.2.0 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-1.1.1 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-1.1.0 lib/webmention/verification/verifiers/html_verifier.rb
webmention-verification-1.0.0 lib/webmention/verification/verifiers/html_verifier.rb