Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 68 lines (62 sloc) 1.408 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
module CapybaraExtensions
  def data
    top_level.merge collections(self)
  end
  
  private
  
  def top_level
    matches = all('body > *')
    result = {}
    matches.each do |match|
      result.merge! data_attributes(match)
    end
    result
  end
  
  def collections(node)
    collection_nodes = node.all('*[@data-collection]')
    collections = {}
    collection_nodes.each do |collection_node|
      collection_name = collection_node['data-collection'].to_sym
      members = []
      node.all("*[data-collection='#{collection_name}'] > *").each do |child|
        members << data_attributes(child)
      end
      collections[collection_name] = members
    end
    collections
  end
  
end

def data_attributes(node)
  result = {}
  name = nil
  node.native.attributes.each_pair do |key, v|
    next unless key.start_with?('data-')
    next if key == 'data-collection'
    if key == 'data-name'
      name = node.text.strip
    else
      sym_key = key.gsub('data-', '').gsub('-', '_').to_sym
      if v.value != ""
        result[sym_key] = v.value
      else
        result[sym_key] = node.text
      end
    end
  end
  if name
    if result == {}
      name
    else
      {name => result}
    end
  else
    result
  end
end

# TODO not sure if including in both modules if the best approach
module Capybara
  module Node
    module Finders
      include CapybaraExtensions
    end
  end
end
Something went wrong with that request. Please try again.