Doctype inherits from Node

This makes it possible to parse documents where a doctype resides in a
node, instead of being located at the root.

Fixes #169
This commit is contained in:
Yorick Peterse 2017-02-10 15:10:30 +01:00
parent b13cfdfea5
commit 131fba7aed
No known key found for this signature in database
GPG Key ID: EDD30D2BEB691AC9
4 changed files with 36 additions and 3 deletions

View File

@ -3,6 +3,15 @@
This document contains details of the various releases and their release dates.
Dates are in the format `yyyy-mm-dd`.
## 2.9 - 2017-02-10
### Doctypes are now Nodes
Each Doctype now inherits from `Oga::XML::Node`. This makes it possible to parse
documents where a doctype is located in a child node. However, in these cases
Oga will _not_ populate `Oga::XML::Document#doctype` as this can not be done in
an efficient way.
## 2.8 - 2017-01-04
Ruby 2.4 deprecates Fixnum in favour of Integer, producing warnings whenever

View File

@ -1,9 +1,7 @@
module Oga
module XML
# Class used for storing information about Doctypes.
class Doctype
include ToXML
class Doctype < Node
# The name of the doctype (e.g. "HTML").
# @return [String]
attr_accessor :name

View File

@ -7,6 +7,11 @@ module Oga
include Traversal
include ToXML
# The doctype of the document.
#
# When parsing a document this attribute will be set automatically if a
# doctype resides at the root of the document.
#
# @return [Oga::XML::Doctype]
attr_accessor :doctype

View File

@ -134,4 +134,25 @@ describe Oga::XML::Parser do
@document.doctype.inline_rules.should == 'rule1'
end
end
describe 'doctypes inside an element' do
before :all do
@document = parse('<foo><!DOCTYPE html><bar /></foo>')
end
it 'does not set the doctype of the document' do
# This is because the doctype does not reside at the root. Supporting
# doctypes at arbitrary locations would come at a hefty performance
# impact, and requires doctypes to reach back into their owning documents
# (which leads to ugly code).
@document.doctype.should be_nil
end
it 'sets the next node of the doctype' do
doctype = @document.children[0].children[0]
bar = @document.children[0].children[1]
doctype.next.should == bar
end
end
end