Skip to content

Commit

Permalink
Fix XML::Node#errors to return nil when empty (#12795)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Nov 30, 2022
1 parent 5cc9eff commit a83537e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions spec/std/xml/xml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ describe XML do
xml = XML.parse(%(<people></foo>))
xml.root.not_nil!.name.should eq("people")
xml.errors.try(&.map(&.to_s)).should eq ["Opening and ending tag mismatch: people line 1 and foo"]

xml = XML.parse(%(<foo></foo>))
xml.errors.should be_nil
end

describe "#namespace" do
Expand Down
6 changes: 4 additions & 2 deletions src/xml/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class XML::Node
end

# :ditto:
def initialize(node : LibXML::Doc*, @errors = nil)
def initialize(node : LibXML::Doc*, @errors : Array(XML::Error)? = nil)
initialize(node.as(LibXML::Node*))
end

Expand Down Expand Up @@ -578,7 +578,9 @@ class XML::Node

# Returns the list of `XML::Error` found when parsing this document.
# Returns `nil` if no errors were found.
getter errors : Array(XML::Error)?
def errors : Array(XML::Error)?
return @errors unless @errors.try &.empty?
end

private def check_no_null_byte(string)
if string.includes? Char::ZERO
Expand Down

0 comments on commit a83537e

Please sign in to comment.