Skip to content

How to make a custom class

William Entriken edited this page May 2, 2017 · 1 revision

This page is an more detailed version of the instructions on the project README. You will learn to create a class that inherits from HTMLProofer::Check. And more importantly, we'll help you prepare your project to publish on Ruby Gems and promote it here on our extensions list wiki page.

Want to write your own test? Sure, that's possible!

Just create a class that inherits from HTMLProofer::Check. This subclass must define one method called run. This is called on your content, and is responsible for performing the validation on whatever elements you like. When you catch a broken issue, call add_issue(message, line: line, content: content) to explain the error. line refers to the line numbers, and content is the node content of the broken element.

If you're working with the element's attributes (as most checks do), you'll also want to call create_element(node) as part of your suite. This constructs an object that contains all the attributes of the HTML element you're iterating on.

Here's an example custom test demonstrating these concepts. It reports mailto links that point to [email protected]:

class MailToOctocat < ::HTMLProofer::Check
  def mailto?
    return false if @link.data_ignore_proofer || @link.href.nil?
    @link.href.match /mailto/
  end

  def octocat?
    @link.href.match /[email protected]/
  end

  def run
    @html.css('a').each do |node|
      @link = create_element(node)
      line = node.line

      if mailto? && octocat?
        return add_issue("Don't email the Octocat directly!", line: line)
      end
    end
  end
end

TODO:

  1. Add notes here from https://github.com/fulldecent/html-proofer-mailto-awesome/issues/2