Skip to content
Browse files

Allowing seamless chaining of tags and filters through the ninja-like…

… awesomeness of Nokogiri NodeSet.

(I just like leaving around commit messages containing the word "Ninja". Also "Monkey". Whoa, did I just use both?)
  • Loading branch information...
1 parent e372f72 commit d7c9c860a1d1507dc25de6bc73cd3a674553e199 @flavorjones flavorjones committed
Showing with 15 additions and 7 deletions.
  1. +8 −4 lib/elementor/element_set.rb
  2. +2 −2 lib/elementor/result.rb
  3. +5 −1 spec/elementor_spec.rb
View
12 lib/elementor/element_set.rb
@@ -7,8 +7,8 @@ def self.included(base)
end
end
- def initialize(array)
- @node_set = Nokogiri::XML::NodeSet.new(nil, array)
+ def initialize(document, array)
+ @node_set = Nokogiri::XML::NodeSet.new(document, array)
end
def length
@@ -21,7 +21,11 @@ def select(&block)
end
def replace(array)
- @node_set = Nokogiri::XML::NodeSet.new(nil, array)
+ @node_set = Nokogiri::XML::NodeSet.new(@node_set.document, array)
+ end
+
+ def search(selector)
+ @node_set.search(selector)
end
end
@@ -63,7 +67,7 @@ def with_attrs(options={})
alias_method :attrs, :with_attrs
def method_missing(sym, *args, &block)
- result.respond_to?(sym) ? result.send(sym, doc, *args) : super
+ result.respond_to?(sym) ? result.send(sym, self, *args) : super
end
def respond_to?(sym)
View
4 lib/elementor/result.rb
@@ -81,7 +81,7 @@ def #{name}(*filters, &block)
end
def make_element_set(name, selector, *filters, &block)
- set = ElementSet.new scope(filters).search(selector)
+ set = ElementSet.new(doc, scope(filters).search(selector))
set.result = self
set.selector = selector
set = filters.empty? ? set : filters.inject(set) { |result, fn| fn[result] }
@@ -101,4 +101,4 @@ def content
@content ||= context.send(opts[:from] || :body)
end
end
-end
+end
View
6 spec/elementor_spec.rb
@@ -154,6 +154,10 @@
result.tags.with_text('zz').with_attrs(:class => /even/).should have(1).node
end
+ it "allows chaining with selector aliases" do
+ result.tag_clouds.with_attrs(:rel => 'other').tags.should have(2).nodes
+ end
+
it "coerces things that aren't strings or regexes into strings" do
result.versions.with_text(1).should have(1).node
end
@@ -314,4 +318,4 @@
end
end
end
-end
+end

0 comments on commit d7c9c86

Please sign in to comment.
Something went wrong with that request. Please try again.