module WabisabiTraverseModule

Public Instance Methods

get_a_path(path) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 40
def get_a_path(path)
  if /\A(.*)\[(.+)\]\z/ =~ path
    tag = $1
    select = $2
    attr, value = parse_attr_selector(select)
    if attr
      return get_has_attr(attr, value)
    end
  end
  return get_tag(path)
end
get_by_class(klass) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 26
def get_by_class(klass)
  get_has_attr(:class, klass)
end
get_path(path) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 30
def get_path(path)
  xml = self
  path.split('/').each {|pa|
    next unless 0 < pa.length
    return nil if xml.nil?
    xml = xml.get_a_path(pa)
  }
  xml
end
get_tag(tag) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 20
def get_tag(tag)
  tag = tag.to_s
  tag, num = split_name_num(tag)
  find_nth_element(num, tag.intern)
end
traverse_element(*tags) { |self| ... } click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 9
def traverse_element(*tags, &b)
  if tags.length == 0 || (self[0].is_a?(Symbol) && tags.include?(self[0]))
    yield(self)
  end

  self.each {|s|
    next unless s.is_a?(Array)
    s.traverse_element(*tags, &b)     # Recursive.
  }
end

Private Instance Methods

each_has_attr(key, value) { |e| ... } click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 87
def each_has_attr(key, value)
  traverse_element {|e|
    attr = e.attr
    if attr && attr[key] == value
      yield(e)
    end
  }
end
find_nth_element(num=1, *names) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 104
def find_nth_element(num=1, *names)
  i = 1
  traverse_element(*names) {|e|
    return e if i == num
    i += 1
  }
  return nil
end
get_has_attr(attr, value) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 80
def get_has_attr(attr, value)
  each_has_attr(attr, value) {|l|
    return l
  }
  return nil
end
parse_attr_selector(attr_selector) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 54
def parse_attr_selector(attr_selector)
  unless attr_selector.include?("=")
    return nil
  end

  left, right = attr_selector.split("=", 2)

  if /\A@([a-z:]+)\z/ =~ left
    attr_name = $1.intern
  else
    raise 'unknown attribute selector '+left
  end

  # FIXME: It's not right to this twice.
  right = $1 if /\A'(.+)'\z/ =~ right
  right = $1 if /\A"(.+)"\z/ =~ right

  if /\A(.*)\z/ =~ right
    value = $1
  else
    raise 'unknown attribute value '+right
  end

  return [attr_name, value]
end
split_name_num(name) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-traverse.rb, line 96
def split_name_num(name)
  name = name.sub(%r|(^//)|, '')
  if /^(\w+)\[(\d+)\]$/ =~ name
    return [$1, $2.to_i]
  end
  return [name, 1]
end