From 14e1b0f188d302b69816ddf12f5ac591fd76b91d Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 4 Mar 2022 20:07:50 +0800 Subject: ... --- src/xml/XmlNode.cpp | 16 ++++++++++++++++ src/xml/XmlParser.cpp | 21 ++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'src/xml') diff --git a/src/xml/XmlNode.cpp b/src/xml/XmlNode.cpp index cfa87dd6..41bbed4d 100644 --- a/src/xml/XmlNode.cpp +++ b/src/xml/XmlNode.cpp @@ -9,6 +9,10 @@ XmlElementNode* XmlNode::AsElement() { XmlTextNode* XmlNode::AsText() { return static_cast(this); } +XmlCommentNode* XmlNode::AsComment() { + return static_cast(this); +} + const XmlElementNode* XmlNode::AsElement() const { return static_cast(this); } @@ -17,6 +21,10 @@ const XmlTextNode* XmlNode::AsText() const { return static_cast(this); } +const XmlCommentNode* XmlNode::AsComment() const { + return static_cast(this); +} + XmlElementNode::~XmlElementNode() { for (auto child : children_) { delete child; @@ -57,4 +65,12 @@ XmlNode* XmlElementNode::Clone() const { return node; } + +XmlCommentNode::~XmlCommentNode() {} + +XmlNode* XmlCommentNode::Clone() const { + XmlCommentNode* node = new XmlCommentNode(text_); + + return node; +} } // namespace cru::xml diff --git a/src/xml/XmlParser.cpp b/src/xml/XmlParser.cpp index babb6b00..313015d5 100644 --- a/src/xml/XmlParser.cpp +++ b/src/xml/XmlParser.cpp @@ -22,7 +22,7 @@ char16_t XmlParser::Read1() { String XmlParser::ReadWithoutAdvance(int count) { if (current_position_ + count > xml_.size()) { - return u""; + count = xml_.size() - current_position_; } return xml_.substr(current_position_, count); } @@ -108,6 +108,19 @@ XmlElementNode* XmlParser::DoParse() { } current_ = current_->GetParent(); + } else if (ReadWithoutAdvance(3) == u"!--") { + current_position_ += 3; + + String text; + while (true) { + auto str = ReadWithoutAdvance(3); + if (str == u"-->") break; + if (str.empty()) throw XmlParsingException(u"Unexpected end of xml"); + text += Read1(); + } + + current_position_ += 3; + current_->AddChild(new XmlCommentNode(text.Trim())); } else { ReadSpacesAndDiscard(); @@ -173,10 +186,8 @@ XmlElementNode* XmlParser::DoParse() { throw XmlParsingException(u"Unexpected end of xml"); } - if (pseudo_root_node_->GetChildren().size() != 1 || - pseudo_root_node_->GetChildren()[0]->GetType() != - XmlNode::Type::Element) { - throw XmlParsingException(u"Expected 1 element node as root."); + if (pseudo_root_node_->GetChildren().size() != 1) { + throw XmlParsingException(u"Expected 1 node as root."); } return static_cast(pseudo_root_node_->GetChildren()[0]); -- cgit v1.2.3