static public Method

Node.parse(parent, line, pos, content, strict=true)

There's no documentation for this item.

Source Code

# File action_controller/vendor/html-scanner/html/node.rb, line 138
def parse(parent, line, pos, content, strict=true)
  if content !~ /^<\S/
    Text.new(parent, line, pos, content)
  else
    scanner = StringScanner.new(content)

    unless scanner.skip(/</)
      if strict
        raise "expected <"
      else
        return Text.new(parent, line, pos, content)
      end
    end

    if scanner.skip(/!\[CDATA\[/)
      scanner.scan_until(/\]\]>/)
      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.downcase!

    unless closing
      scanner.skip(/\s*/)
      attributes = {}
      while attr = scanner.scan(/[-\w:]+/)
        value = true
        if scanner.scan(/\s*=\s*/)
          if delim = scanner.scan(/['"]/)
            value = ""
            while text = scanner.scan(/[^#{delim}\\]+|./)
              case text
                when "\\" then
                  value << text
                  value << scanner.getch
                when delim
                  break
                else value << text
              end
            end
          else
            value = scanner.scan(/[^\s>\/]+/)
          end
        end
        attributes[attr.downcase] = value
        scanner.skip(/\s*/)
      end

      closing = ( scanner.scan(/\//) ? :self : nil )
    end

    unless scanner.scan(/\s*>/)
      if strict
        raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" 
      else
        # throw away all text until we find what we're looking for
        scanner.skip_until(/>/) or scanner.terminate
      end
    end

    Tag.new(parent, line, pos, name, attributes, closing)
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.