Tree building of doctypes.
This commit is contained in:
parent
81b1155af3
commit
c077988dd6
|
@ -11,6 +11,7 @@ require_relative 'oga/xml/text'
|
||||||
require_relative 'oga/xml/comment'
|
require_relative 'oga/xml/comment'
|
||||||
require_relative 'oga/xml/cdata'
|
require_relative 'oga/xml/cdata'
|
||||||
require_relative 'oga/xml/xml_declaration'
|
require_relative 'oga/xml/xml_declaration'
|
||||||
|
require_relative 'oga/xml/doctype'
|
||||||
require_relative 'oga/xml/tree_builder'
|
require_relative 'oga/xml/tree_builder'
|
||||||
|
|
||||||
require_relative 'oga/html/parser'
|
require_relative 'oga/html/parser'
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
module Oga
|
||||||
|
module XML
|
||||||
|
##
|
||||||
|
# Class description
|
||||||
|
#
|
||||||
|
class Doctype
|
||||||
|
attr_accessor :name, :type, :public_id, :system_id
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Hash] options
|
||||||
|
#
|
||||||
|
def initialize(options = {})
|
||||||
|
options.each do |key, value|
|
||||||
|
instance_variable_set("@#{key}", value) if respond_to?(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml
|
||||||
|
segments = "<!DOCTYPE #{name}"
|
||||||
|
|
||||||
|
segments << " #{type}" if type
|
||||||
|
segments << %Q{ "#{public_id}"} if public_id
|
||||||
|
segments << %Q{ "#{system_id}"} if system_id
|
||||||
|
|
||||||
|
return segments + '>'
|
||||||
|
end
|
||||||
|
end # Doctype
|
||||||
|
end # XML
|
||||||
|
end # Oga
|
|
@ -4,11 +4,15 @@ module Oga
|
||||||
# Class description
|
# Class description
|
||||||
#
|
#
|
||||||
class Document < Node
|
class Document < Node
|
||||||
attr_accessor :xml_declaration
|
attr_accessor :doctype, :xml_declaration
|
||||||
|
|
||||||
def to_xml
|
def to_xml
|
||||||
xml = children.map(&:to_xml).join('')
|
xml = children.map(&:to_xml).join('')
|
||||||
|
|
||||||
|
if doctype
|
||||||
|
xml = doctype.to_xml + xml
|
||||||
|
end
|
||||||
|
|
||||||
if xml_declaration
|
if xml_declaration
|
||||||
xml = xml_declaration.to_xml + xml
|
xml = xml_declaration.to_xml + xml
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,6 +22,10 @@ module Oga
|
||||||
process_all(node).each do |child|
|
process_all(node).each do |child|
|
||||||
if child.is_a?(XmlDeclaration)
|
if child.is_a?(XmlDeclaration)
|
||||||
document.xml_declaration = child
|
document.xml_declaration = child
|
||||||
|
|
||||||
|
elsif child.is_a?(Doctype)
|
||||||
|
document.doctype = child
|
||||||
|
|
||||||
else
|
else
|
||||||
document.children << child
|
document.children << child
|
||||||
end
|
end
|
||||||
|
@ -44,6 +48,19 @@ module Oga
|
||||||
return XmlDeclaration.new(attributes)
|
return XmlDeclaration.new(attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Oga::AST::Node] node
|
||||||
|
# @return [Oga::XML::Doctype]
|
||||||
|
#
|
||||||
|
def on_doctype(node)
|
||||||
|
return Doctype.new(
|
||||||
|
:name => node.children[0],
|
||||||
|
:type => node.children[1],
|
||||||
|
:public_id => node.children[2],
|
||||||
|
:system_id => node.children[3]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Oga::AST::Node] node
|
# @param [Oga::AST::Node] node
|
||||||
# @return [Oga::XML::Comment]
|
# @return [Oga::XML::Comment]
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Oga::XML::Doctype do
|
||||||
|
context 'setting attributes' do
|
||||||
|
example 'set the name via the constructor' do
|
||||||
|
described_class.new(:name => 'html').name.should == 'html'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the name via a setter' do
|
||||||
|
instance = described_class.new
|
||||||
|
instance.name = 'html'
|
||||||
|
|
||||||
|
instance.name.should == 'html'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context '#to_xml' do
|
||||||
|
example 'generate a bare minimum representation' do
|
||||||
|
described_class.new(:name => 'html').to_xml.should == '<!DOCTYPE html>'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the type if present' do
|
||||||
|
instance = described_class.new(:name => 'html', :type => 'PUBLIC')
|
||||||
|
|
||||||
|
instance.to_xml.should == '<!DOCTYPE html PUBLIC>'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the public ID if present' do
|
||||||
|
instance = described_class.new(
|
||||||
|
:name => 'html',
|
||||||
|
:type => 'PUBLIC',
|
||||||
|
:public_id => 'foo'
|
||||||
|
)
|
||||||
|
|
||||||
|
instance.to_xml.should == '<!DOCTYPE html PUBLIC "foo">'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the system ID if present' do
|
||||||
|
instance = described_class.new(
|
||||||
|
:name => 'html',
|
||||||
|
:type => 'PUBLIC',
|
||||||
|
:public_id => 'foo',
|
||||||
|
:system_id => 'bar'
|
||||||
|
)
|
||||||
|
|
||||||
|
instance.to_xml.should == '<!DOCTYPE html PUBLIC "foo" "bar">'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -46,4 +46,20 @@ describe Oga::XML::Document do
|
||||||
.should == '<?xml version="5.0" encoding="UTF-8" ?><!--foo-->'
|
.should == '<?xml version="5.0" encoding="UTF-8" ?><!--foo-->'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#to_xml with doctypes' do
|
||||||
|
before do
|
||||||
|
doctype = Oga::XML::Doctype.new(:name => 'html', :type => 'PUBLIC')
|
||||||
|
children = [Oga::XML::Comment.new(:text => 'foo')]
|
||||||
|
|
||||||
|
@document = described_class.new(
|
||||||
|
:doctype => doctype,
|
||||||
|
:children => children
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the doctype' do
|
||||||
|
@document.to_xml.should == '<!DOCTYPE html PUBLIC><!--foo-->'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,6 +32,18 @@ describe Oga::XML::TreeBuilder do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#on_document with doctypes' do
|
||||||
|
before do
|
||||||
|
doctype = s(:doctype, 'html', 'PUBLIC', 'foo', 'bar')
|
||||||
|
node = s(:document, doctype)
|
||||||
|
@tag = @builder.process(node)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the doctype of the document' do
|
||||||
|
@tag.doctype.is_a?(Oga::XML::Doctype).should == true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#on_xml_decl' do
|
context '#on_xml_decl' do
|
||||||
before do
|
before do
|
||||||
node = s(:xml_decl, s(:attributes, s(:attribute, 'encoding', 'UTF-8')))
|
node = s(:xml_decl, s(:attributes, s(:attribute, 'encoding', 'UTF-8')))
|
||||||
|
@ -47,6 +59,33 @@ describe Oga::XML::TreeBuilder do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#on_doctype' do
|
||||||
|
before do
|
||||||
|
node = s(:doctype, 'html', 'PUBLIC', 'foo', 'bar')
|
||||||
|
@tag = @builder.process(node)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return a Doctype node' do
|
||||||
|
@tag.is_a?(Oga::XML::Doctype).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the doctype name' do
|
||||||
|
@tag.name.should == 'html'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the doctype type' do
|
||||||
|
@tag.type.should == 'PUBLIC'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the public ID' do
|
||||||
|
@tag.public_id.should == 'foo'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the system ID' do
|
||||||
|
@tag.system_id.should == 'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#on_comment' do
|
context '#on_comment' do
|
||||||
before do
|
before do
|
||||||
node = s(:comment, 'foo')
|
node = s(:comment, 'foo')
|
||||||
|
|
Loading…
Reference in New Issue