15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/parser_girl/parser.rb', line 15
def find(needle)
haystack_base = @xml
haystack = haystack_base
pos = 0
result = []
@stack = []
while true do
break unless haystack =~ /\<([^!][^\>]*)\>/i
content = $1 b = $`.length + 1 e = content.length + b + 1
if content =~ /^([^\s]+)/ && $1.downcase == "script" &&
haystack =~ /\<\/script(\s[^\>]*)?\>/i
e = $`.length + $&.length
end
if content =~ /^#{needle}(\s.*)?$/i
@stack << { :position => pos+e, :attrs => split_attr($1) }
elsif content =~ /^\/#{needle}(\s.*)?$/i
hash = pop(haystack_base, b+pos-1)
result << Parser.new(hash[:content], hash[:attrs]) if hash
end
pos += e
haystack = haystack_base[pos, haystack_base.length-pos]
end
result += @stack.map{ |hash| Parser.new("", hash[:attrs]) }
return Proxy.new(result)
end
|