class Foxy::Html
Public Class Methods
new(html = nil)
click to toggle source
Calls superclass method
# File lib/foxy/html.rb, line 14 def initialize(html = nil) if html.nil? super([]) elsif html.is_a? self.class super(html.__getobj__) elsif html.respond_to?(:to_str) super(html.to_str.scan(RE_HTML).map { |args| Node.build(*args) }) elsif html.respond_to?(:read) super(html.read.scan(RE_HTML).map { |args| Node.build(*args) }) else super(html) end end
Public Instance Methods
attr(name)
click to toggle source
# File lib/foxy/html.rb, line 98 def attr(name) first.attr(name) end
clean(**kws)
click to toggle source
# File lib/foxy/html.rb, line 28 def clean(**kws) Html.new(map { |node| node.clean(**kws) }) end
comments()
click to toggle source
# File lib/foxy/html.rb, line 90 def comments each_with_object([]) { |node, acc| acc << node.content.sub(/^<!--/, "").sub(/-->$/, "") if node.type == :comment } end
css(query)
click to toggle source
# File lib/foxy/html.rb, line 73 def css(query) Collection.new([self]).css(query) end
find(**kws)
click to toggle source
# File lib/foxy/html.rb, line 77 def find(**kws) isearch(**kws) { |val| return val unless val.empty? } nil end
id()
click to toggle source
# File lib/foxy/html.rb, line 102 def id first.id end
isearch(tagname: nil, id: nil, cls: nil, fun: nil, css: nil) { |html| ... }
click to toggle source
# File lib/foxy/html.rb, line 32 def isearch(tagname: nil, id: nil, cls: nil, fun: nil, css: nil) cls = Array(cls) tagname &&= tagname.downcase y = 0 buff = [] close_tagname = nil each do |node| # [1:-1]: # El orden de los if es importante para que devuelva el # primer y el ultimo nodo if y.zero? && node.tag? && (!tagname || node.tagname! == tagname) && (!id || node.id! == id) && (cls - node.cls!).empty? && (!fun || fun.call(node)) # Guardamos porque pudiera ser que el parametro # tagname fuera nil close_tagname = node.tagname! y += 1 elsif y && node.tag? && node.tagname! == close_tagname y += 1 end buff << node if y > 0 y -= 1 if y > 0 && node.closetag? && node.tagname! == close_tagname next unless buff && y.zero? yield Html.new(buff) buff = [] close_tagname = nil end end
joinedtexts()
click to toggle source
# File lib/foxy/html.rb, line 94 def joinedtexts texts.join.gsub(/[\r\n\s]+/, " ").strip end
rebuild()
click to toggle source
# File lib/foxy/html.rb, line 82 def rebuild map(&:content).join end
search(**kws)
click to toggle source
# File lib/foxy/html.rb, line 66 def search(**kws) return Collection.new([self]).search(kws) if kws[:css] list = [] isearch(**kws) { |val| list << val unless val.empty? } Collection.new(list) end
texts()
click to toggle source
# File lib/foxy/html.rb, line 86 def texts each_with_object([]) { |node, acc| acc << node.content if node.type == :notag } end
to_s()
click to toggle source
# File lib/foxy/html.rb, line 106 def to_s rebuild end
try(m, *a, &b)
click to toggle source
# File lib/foxy/html.rb, line 10 def try(m, *a, &b) public_send(m, *a, &b) if respond_to?(m) end