Update lib/node.rb

Grab some fixes from html_scanner, and add few of our own.
This commit is contained in:
Jacques Distler 2009-10-10 03:52:33 -05:00
parent d5e35d2861
commit dd8c912c6c

View file

@ -77,9 +77,9 @@ module XHTML #:nodoc:
# Return a textual representation of the node. # Return a textual representation of the node.
def to_s def to_s
s = "" s = []
@children.each { |child| s << child.to_s } @children.each { |child| s << child.to_s }
s s.join
end end
# Return false (subclasses must override this to provide specific matching # Return false (subclasses must override this to provide specific matching
@ -150,13 +150,19 @@ module XHTML #:nodoc:
end end
if scanner.skip(/!\[CDATA\[/) if scanner.skip(/!\[CDATA\[/)
scanner.scan_until(/\]\]>/) unless scanner.skip_until(/\]\]>/)
if strict
raise "expected ]]> (got #{scanner.rest.inspect} for #{content})"
else
scanner.skip_until(/\Z/)
end
end
return CDATA.new(parent, line, pos, scanner.pre_match.gsub(/<!\[CDATA\[/, '')) return CDATA.new(parent, line, pos, scanner.pre_match.gsub(/<!\[CDATA\[/, ''))
end end
closing = ( scanner.scan(/\//) ? :close : nil ) closing = ( scanner.scan(/\//) ? :close : nil )
return Text.new(parent, line, pos, content) unless name = scanner.scan(/[\w:-]+/) return Text.new(parent, line, pos, content) unless name = scanner.scan(/[\w:-]+/)
name
unless closing unless closing
scanner.skip(/\s*/) scanner.skip(/\s*/)
@ -165,17 +171,18 @@ module XHTML #:nodoc:
value = true value = true
if scanner.scan(/\s*=\s*/) if scanner.scan(/\s*=\s*/)
if delim = scanner.scan(/['"]/) if delim = scanner.scan(/['"]/)
value = "" v = []
while text = scanner.scan(/[^#{delim}\\]+|./) while text = scanner.scan(/[^#{delim}\\]+|./)
case text case text
when "\\" then when "\\" then
value << text v << text
value << scanner.getch v << scanner.getch
when delim when delim
break break
else value << text else v << text
end end
end end
value = v.join
else else
value = scanner.scan(/[^\s>\/]+/) value = scanner.scan(/[^\s>\/]+/)
end end
@ -265,7 +272,7 @@ module XHTML #:nodoc:
# itself. # itself.
class CDATA < Text #:nodoc: class CDATA < Text #:nodoc:
def to_s def to_s
"<![CDATA[#{super}]>" "<![CDATA[#{super}]]>"
end end
end end
@ -309,22 +316,20 @@ module XHTML #:nodoc:
# Returns a textual representation of the node # Returns a textual representation of the node
def to_s def to_s
s = ''
if @closing == :close if @closing == :close
s = "</#{@name}>" unless self.childless? "</#{@name}>" unless self.childless?
else else
s = "<#{@name}" s = ["<#{@name}"]
atlist = @attributes.sort @attributes.sort.each do |k,v|
atlist.each do |att| s << " #{k}"
s << " #{att[0]}" s << "='#{v}'" if String === v
s << "='#{att[1]}'" if String === att[1]
end end
s << "/" if (@children.empty? && @closing == :self) or self.childless? s << "/" if (@children.empty? && @closing == :self) or self.childless?
s << ">" s << ">"
@children.each { |child| s << child.to_s } @children.each { |child| s << child.to_s }
s << "</#{@name}>" if @closing != :self && !@closing.nil? && !@children.empty? s << "</#{@name}>" if @closing != :self && !@closing.nil? && !@children.empty?
s.join
end end
s
end end
# If either the node or any of its children meet the given conditions, the # If either the node or any of its children meet the given conditions, the