diff --git a/CHANGELOG.md b/CHANGELOG.md index 14c0ac1..c31546b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/oga/xml/doctype.rb b/lib/oga/xml/doctype.rb index c5f5bba..be9006a 100644 --- a/lib/oga/xml/doctype.rb +++ b/lib/oga/xml/doctype.rb @@ -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 diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index fd4c0ee..78b81d8 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -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 diff --git a/spec/oga/xml/parser/doctype_spec.rb b/spec/oga/xml/parser/doctype_spec.rb index 44ea223..1461f4b 100644 --- a/spec/oga/xml/parser/doctype_spec.rb +++ b/spec/oga/xml/parser/doctype_spec.rb @@ -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('') + 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