Skip to content

nakajima/elementor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Elementor

Prettier element traversal with Nokogiri.

Elementor lets you alias Nokogiri CSS searches with method
names, returning an extended Nokogiri document ,upon which you
can call these alias methods.

To use it, include the Elementor module, then call elements,
pass it options then a block in which you’ll specify your element
names. The only option you have to specify currently is :from,
which is the method that will be called to return a raw markup
string to be parsed by Nokogiri.

Usage

require 'rubygems'
require 'elementor'

include Elementor

def body
  <<-HTML
  <h1>This is the header</h1>
  <div id="detail-section">
    <span>This is a detail</span>
    <span>So is this</span>
    <span>Oh one more!</span>
  </div>
  HTML
end

doc = elements(:from => :body) do |tag|
  tag.headers "h1"
  tag.details "#detail-section span"
end

# The standard
p doc.headers # => ["This is the header"]
p doc.details # => ["This is a detail", "So is this", "Oh one more!"]

# Using the `with_text` filter
p doc.details.with_text("This is a detail") # => ["This is a detail"]

Useful Usage

(separate from above example)

require 'elementor'
require 'elementor/spec'
  
# I don't like testing views at this level from the
# controller spec, but this is just an example. I'd
# recommend using Elementor with whatever view test
# setup you prefer.
describe FoosController do
  include Elementor

  describe "#index" do
    before(:each) do
      @result = elements(:from => :body) do |doc|
        doc.tags "ul#tag-cloud li"
        doc.ajax_forms "form.ajaxified"
        doc.user_links "ul#users li a"
      end
    end

    it "renders tag cloud tags" do
      @result.should have(52).tags
    end
    
    it "renders ajax forms" do
      @result.should have(3).ajax_forms
    end

    it "renders user links" do
      @result.should have(6).user_links
    end
    
    # this one uses the `with_text` filter
    it "renders user link for Pat" do
      @result.should have(1).user_links.with_text("Pat")
    end
    
    # this one uses the `with_attrs` filter
    it "renders active user links" do
      @result.should have(1).user_links.with_attrs(:class => /active/)
    end
  end
end

Todo

  • Allow chaining of filter results
  • Maybe a better way of getting raw markup strings?

About

View testing that doesn't suck.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages