Added a convenience class for parsing HTML.
This removes the need for users having to set the `:html` option themselves.
This commit is contained in:
parent
58009614f6
commit
79818eb349
|
@ -1,5 +1,8 @@
|
|||
require 'ast'
|
||||
|
||||
require_relative 'oga/ast/node'
|
||||
|
||||
require_relative 'oga/xml/lexer'
|
||||
require_relative 'oga/xml/parser'
|
||||
|
||||
require_relative 'oga/html/parser'
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
module Oga
|
||||
module HTML
|
||||
##
|
||||
# Low level AST parser for parsing HTML documents. See {Oga::XML::Parser}
|
||||
# for more information.
|
||||
#
|
||||
class Parser < XML::Parser
|
||||
##
|
||||
# @param [Hash] options
|
||||
# @see Oga::XML::Parser#initialize
|
||||
#
|
||||
def initialize(options = {})
|
||||
options = options.merge(:html => true)
|
||||
|
||||
super(options)
|
||||
end
|
||||
end # Parser
|
||||
end # HTML
|
||||
end # Oga
|
|
@ -0,0 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::HTML::Parser do
|
||||
example 'parse an HTML void element' do
|
||||
parse_html('<meta>').should == s(
|
||||
:document,
|
||||
s(:element, nil, 'meta', nil, nil)
|
||||
)
|
||||
end
|
||||
end
|
|
@ -23,7 +23,7 @@ module Oga
|
|||
end
|
||||
|
||||
##
|
||||
# Parses the given HTML and returns an AST.
|
||||
# Parses the given XML and returns an AST.
|
||||
#
|
||||
# @param [String] input
|
||||
# @param [Hash] options
|
||||
|
@ -32,5 +32,14 @@ module Oga
|
|||
def parse(input, options = {})
|
||||
return Oga::XML::Parser.new(options).parse(input)
|
||||
end
|
||||
|
||||
##
|
||||
# Parses the given HTML and returns an AST.
|
||||
#
|
||||
# @see #parse
|
||||
#
|
||||
def parse_html(input, options = {})
|
||||
return Oga::HTML::Parser.new(options).parse(input)
|
||||
end
|
||||
end # ParsingHelpers
|
||||
end # Oga
|
||||
|
|
Loading…
Reference in New Issue