Skip to content

Commit

Permalink
Update lib/node.rb
Browse files Browse the repository at this point in the history
Grab some fixes from html_scanner, and add few of our own.
  • Loading branch information
distler committed Oct 10, 2009
1 parent d5e35d2 commit dd8c912
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions lib/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def initialize(parent, line=0, pos=0)

# Return a textual representation of the node.
def to_s
s = ""
s = []
@children.each { |child| s << child.to_s }
s
s.join
end

# Return false (subclasses must override this to provide specific matching
Expand Down Expand Up @@ -150,13 +150,19 @@ def parse(parent, line, pos, content, strict=true)
end

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\[/, ''))
end

closing = ( scanner.scan(/\//) ? :close : nil )
return Text.new(parent, line, pos, content) unless name = scanner.scan(/[\w:-]+/)
name

unless closing
scanner.skip(/\s*/)
Expand All @@ -165,17 +171,18 @@ def parse(parent, line, pos, content, strict=true)
value = true
if scanner.scan(/\s*=\s*/)
if delim = scanner.scan(/['"]/)
value = ""
v = []
while text = scanner.scan(/[^#{delim}\\]+|./)
case text
when "\\" then
value << text
value << scanner.getch
v << text
v << scanner.getch
when delim
break
else value << text
else v << text
end
end
value = v.join
else
value = scanner.scan(/[^\s>\/]+/)
end
Expand Down Expand Up @@ -265,7 +272,7 @@ def ==(node)
# itself.
class CDATA < Text #:nodoc:
def to_s
"<![CDATA[#{super}]>"
"<![CDATA[#{super}]]>"
end
end

Expand Down Expand Up @@ -309,22 +316,20 @@ def childless?(xml = false)

# Returns a textual representation of the node
def to_s
s = ''
if @closing == :close
s = "</#{@name}>" unless self.childless?
"</#{@name}>" unless self.childless?
else
s = "<#{@name}"
atlist = @attributes.sort
atlist.each do |att|
s << " #{att[0]}"
s << "='#{att[1]}'" if String === att[1]
s = ["<#{@name}"]
@attributes.sort.each do |k,v|
s << " #{k}"
s << "='#{v}'" if String === v
end
s << "/" if (@children.empty? && @closing == :self) or self.childless?
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
s
end

# If either the node or any of its children meet the given conditions, the
Expand Down

0 comments on commit dd8c912

Please sign in to comment.