Skip to content

Commit

Permalink
Merge branch 'new_expressions'
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 13, 2011
2 parents 754b810 + 7033f91 commit d262efb
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 390 deletions.
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ PLATFORMS
ruby

DEPENDENCIES
nokogiri (~> 1.3)
rspec (~> 2.0)
xpath!
yard (>= 0.5.8)
64 changes: 7 additions & 57 deletions lib/xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,17 @@

module XPath
autoload :Expression, 'xpath/expression'
autoload :Literal, 'xpath/literal'
autoload :Convertable, 'xpath/convertable'
autoload :Union, 'xpath/union'
autoload :Renderer, 'xpath/renderer'
autoload :HTML, 'xpath/html'
autoload :DSL, 'xpath/dsl'

extend self
extend XPath::DSL::TopLevel
include XPath::DSL::TopLevel

def self.generate
yield(Expression::Self.new)
end

def current
Expression::Self.new
end

def name
Expression::Name.new(current)
end

def descendant(*expressions)
Expression::Descendant.new(current, expressions)
end

def child(*expressions)
Expression::Child.new(current, expressions)
end

def anywhere(expression)
Expression::Anywhere.new(expression)
end

def attr(expression)
Expression::Attribute.new(current, expression)
end

def contains(expression)
Expression::Contains.new(current, expression)
end

def text
Expression::Text.new(current)
end

def var(name)
Expression::Variable.new(name)
end

def string
Expression::StringFunction.new(current)
end

def tag(name)
Expression::Tag.new(name)
end

def css(selector)
paths = Nokogiri::CSS.xpath_for(selector).map do |selector|
Expression::CSS.new(current, Expression::Literal.new(selector))
end
Union.new(*paths)
end

def varstring(name)
var(name).string_literal
yield(Expression.new(:this_node))
end
end
15 changes: 15 additions & 0 deletions lib/xpath/convertable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module XPath
module Convertable
def to_s
to_xpaths.join(' | ')
end

def to_xpaths
[to_xpath(:exact), to_xpath(:fuzzy)].uniq
end

def to_xpath(predicate=nil)
Renderer.render(predicate, self)
end
end
end
116 changes: 116 additions & 0 deletions lib/xpath/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module XPath
module DSL
module TopLevel
def current
Expression.new(:this_node)
end

def name
Expression.new(:node_name, current)
end

def descendant(*expressions)
Expression.new(:descendant, current, expressions)
end

def child(*expressions)
Expression.new(:child, current, expressions)
end

def anywhere(expression)
Expression.new(:anywhere, expression)
end

def attr(expression)
Expression.new(:attribute, current, expression)
end

def contains(expression)
Expression.new(:contains, current, expression)
end

def text
Expression.new(:text, current)
end

def var(name)
Expression.new(:variable, name)
end

def string
Expression.new(:string_function, current)
end

def tag(name)
Expression.new(:tag, name)
end

def css(selector)
Expression.new(:css, current, Literal.new(selector))
end

def varstring(name)
var(name).string_literal
end
end

module ExpressionLevel
include XPath::DSL::TopLevel

def where(expression)
Expression.new(:where, current, expression)
end
alias_method :[], :where

def next_sibling(*expressions)
Expression.new(:next_sibling, current, expressions)
end

def one_of(*expressions)
Expression.new(:one_of, current, expressions)
end

def equals(expression)
Expression.new(:equality, current, expression)
end
alias_method :==, :equals

def is(expression)
Expression.new(:is, current, expression)
end

def or(expression)
Expression.new(:or, current, expression)
end
alias_method :|, :or

def and(expression)
Expression.new(:and, current, expression)
end
alias_method :&, :and

def union(*expressions)
Union.new(*[self, expressions].flatten)
end
alias_method :+, :union

def inverse
Expression.new(:inverse, current)
end
alias_method :~, :inverse

def string_literal
Expression.new(:string_literal, self)
end

def apply(variables={})
Expression.new(:applied, current, Literal.new(variables))
end

def normalize
Expression.new(:normalized_space, current)
end
alias_method :n, :normalize
end
end
end
Loading

0 comments on commit d262efb

Please sign in to comment.