Skip to content
Snippets Groups Projects

Feature to create directly addressed Todos when mentioned in beginning

All threads resolved!
Compare and Show latest version
4 files
+ 279
33
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 19
11
module Banzai
module Querying
module_function
# Searches a Nokogiri document using a CSS query, optionally optimizing it
# whenever possible.
#
@@ -8,12 +9,12 @@ module Querying
# query - The CSS query to use.
# reference_options - A hash with nodes filter options
#
# Returns a Nokogiri::XML::NodeSet.
# Returns an array of Nokogiri::XML::Element objects if location is specified
# in reference_options. Otherwise it would a Nokogiri::XML::NodeSet.
def css(document, query, reference_options = {})
# When using "a.foo" Nokogiri compiles this to "//a[...]" but
# "descendant::a[...]" is quite a bit faster and achieves the same result.
xpath = Nokogiri::CSS.xpath_for(query)[0].gsub(%r{^//}, 'descendant::')
nodes = document.xpath(xpath)
filter_nodes(nodes, reference_options)
end
@@ -26,25 +27,32 @@ def filter_nodes(nodes, reference_options)
end
end
# Selects nodes if they are present in the beginning of the document.
# Selects child nodes if they are present in the beginning among other siblings.
#
# nodes - A Nokogiri::XML::NodeSet.
#
# Returns an array of Nokogiri::XML::Element objects.
def filter_nodes_at_beginning(nodes)
parent = nodes.first.parent
children = parent.children
nodes = nodes.to_a
parents_and_nodes = nodes.group_by(&:parent)
filtered_nodes = []
children.each do |child|
next if child.text.blank?
node = nodes.shift
break unless node == child
filtered_nodes << node
parents_and_nodes.each do |parent, nodes|
children = parent.children
nodes = nodes.to_a
children.each do |child|
next if can_be_skipped?(child)
node = nodes.shift
break unless node == child
filtered_nodes << node
end
end
filtered_nodes
end
def can_be_skipped?(node)
node.text.blank? || node.text.strip == ','
end
end
end
Loading