From 0d0deb3dab009f65acad4d8789deafc4be9e2328 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Thu, 5 Sep 2024 15:00:57 +0800 Subject: [PATCH] feat(element.hpp): add first_cdata method --- docs/examples.md | 13 +++++++++ include/myxml/buffer.hpp | 8 +++--- include/myxml/cdata.hpp | 3 +- include/myxml/document.hpp | 4 +-- include/myxml/element.hpp | 6 ++-- include/myxml/node.hpp | 10 +++---- include/myxml/printable.hpp | 2 +- include/myxml/text.hpp | 5 ---- include/myxml/xmlfile.hpp | 19 ++++++------- src/buffer.cpp | 44 ++++++++++++++--------------- src/cdata.cpp | 6 ++-- src/document.cpp | 26 ++++++++--------- src/element.cpp | 21 ++++++++------ src/node.cpp | 10 +++---- src/parser.cpp | 8 +++--- src/printable.cpp | 4 +-- src/text.cpp | 8 +++--- src/xmlfile.cpp | 30 ++++++++++---------- tests/element_test.cpp | 20 ++++++------- tests/parser_test.cpp | 56 ++++++++++++++++++------------------- 20 files changed, 159 insertions(+), 144 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index e26d8af..88785a5 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -76,3 +76,16 @@ fmt::println(txt.trimmed()); // yet it will modify. return a new text fmt::println(txt.trim()); ``` + +### CData + +```C++ +// similar to text +using namespace myxml; +// create from string +cdata txt = "Hello"; +// or from query +cdata txt = root.first_cdata(); +// print raw +fmt::println(txt); +``` diff --git a/include/myxml/buffer.hpp b/include/myxml/buffer.hpp index 9631799..20eeb05 100644 --- a/include/myxml/buffer.hpp +++ b/include/myxml/buffer.hpp @@ -14,9 +14,9 @@ namespace myxml class buffer { private: - std::size_t offset = 0; - std::size_t line = 0; - std::size_t column = 0; + std::size_t _offset = 0; + std::size_t _line = 0; + std::size_t _column = 0; // @returns {pointer to data, data length} virtual std::tuple base() const = 0; @@ -40,7 +40,7 @@ namespace myxml class string_buffer : public buffer { private: - std::variant inner; + std::variant _inner; std::string_view view() const; /** Implement Buffer */ diff --git a/include/myxml/cdata.hpp b/include/myxml/cdata.hpp index 4e635e4..a2fcab4 100644 --- a/include/myxml/cdata.hpp +++ b/include/myxml/cdata.hpp @@ -8,6 +8,7 @@ namespace myxml class cdata : public printable { + friend class element; private: std::shared_ptr _impl; @@ -25,7 +26,7 @@ namespace myxml class cdata_impl : public node { public: - std::string inner; + std::string _inner; cdata_impl() {}; explicit cdata_impl(std::string_view); diff --git a/include/myxml/document.hpp b/include/myxml/document.hpp index 3c0d667..f991c5c 100644 --- a/include/myxml/document.hpp +++ b/include/myxml/document.hpp @@ -27,8 +27,8 @@ namespace myxml class document : public printable { private: - declaration decl; - element root; + declaration _decl; + element _root; public: /* Manipulate */ diff --git a/include/myxml/element.hpp b/include/myxml/element.hpp index 936d751..c49a72a 100644 --- a/include/myxml/element.hpp +++ b/include/myxml/element.hpp @@ -5,6 +5,7 @@ #include #include "myxml/text.hpp" +#include "myxml/cdata.hpp" #include "myxml/printable.hpp" namespace myxml @@ -46,6 +47,7 @@ namespace myxml element first_elem(); element first_elem(std::string_view); text first_text(); + cdata first_cdata(); /* Implement printable */ virtual void print(std::ostream &) const override; @@ -56,8 +58,8 @@ namespace myxml struct element_impl : public composite_node // public std::enable_shared_from_this, public Node { public: - std::string name; - std::map> attributes; + std::string _name; + std::map> _attributes; /* Set initializer as private to avoid using Element without share_ptr*/ diff --git a/include/myxml/node.hpp b/include/myxml/node.hpp index ea24b65..36a48ca 100644 --- a/include/myxml/node.hpp +++ b/include/myxml/node.hpp @@ -18,6 +18,11 @@ namespace myxml class node : public std::enable_shared_from_this, public printable { public: + virtual ~node() = default; + std::shared_ptr _parent; + std::shared_ptr _prev; + std::shared_ptr _next; + template >> std::shared_ptr next() { @@ -44,11 +49,6 @@ namespace myxml return nullptr; } - virtual ~node() = default; - std::shared_ptr _parent; - std::shared_ptr _prev; - std::shared_ptr _next; - template >> std::shared_ptr as() { diff --git a/include/myxml/printable.hpp b/include/myxml/printable.hpp index 72b9579..4c3354a 100644 --- a/include/myxml/printable.hpp +++ b/include/myxml/printable.hpp @@ -24,7 +24,7 @@ namespace myxml class printable { protected: - print_config config; + print_config _config; public: virtual ~printable() = default; diff --git a/include/myxml/text.hpp b/include/myxml/text.hpp index a5f20f2..0c79ef2 100644 --- a/include/myxml/text.hpp +++ b/include/myxml/text.hpp @@ -33,12 +33,7 @@ namespace myxml virtual ~text_impl() = default; - // may used in Export - bool IsAllSpace() const; - /* Implment Exportable*/ - // virtual std::string ExportRaw() const override; - // virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override; virtual void print(std::ostream &) const override; }; diff --git a/include/myxml/xmlfile.hpp b/include/myxml/xmlfile.hpp index 31dca07..e6fdc9d 100644 --- a/include/myxml/xmlfile.hpp +++ b/include/myxml/xmlfile.hpp @@ -6,26 +6,25 @@ namespace myxml { - class XMLFile : public buffer + class xml_file : public buffer { private: - XMLFile(); + xml_file(); /* mmap related*/ - int fd; - std::size_t fileSize; - char *inner; - - std::size_t offset; + int _fd; + std::size_t _size; + char *_mapped; + std::size_t _offset; /* Implement buffer*/ virtual std::tuple base() const; public: - static std::shared_ptr Open(std::string_view fpath); + static std::shared_ptr open(std::string_view fpath); // RAII - ~XMLFile(); + ~xml_file(); // always copy shared_ptr instead of XMLFile - XMLFile(const XMLFile &) = delete; + xml_file(const xml_file &) = delete; }; } \ No newline at end of file diff --git a/src/buffer.cpp b/src/buffer.cpp index 3a51084..9768b60 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -6,12 +6,12 @@ namespace myxml { if (ch == '\n') { - this->column = 0; - this->line++; + this->_column = 0; + this->_line++; } else { - this->column++; + this->_column++; } } @@ -26,51 +26,51 @@ namespace myxml std::optional buffer::peek() const { auto [ptr, len] = this->base(); - if (this->offset >= len) + if (this->_offset >= len) { return std::nullopt; } - return ptr[this->offset]; + return ptr[this->_offset]; } std::optional buffer::peek_n(int n) const { auto [ptr, len] = this->base(); - if (this->offset >= len) + if (this->_offset >= len) { return std::nullopt; } - return std::string_view(ptr + this->offset, n); + return std::string_view(ptr + this->_offset, n); } std::optional buffer::after_n(int n) const { auto [ptr, len] = this->base(); - if (this->offset + n > len) + if (this->_offset + n > len) { return std::nullopt; } - return ptr[this->offset + n]; + return ptr[this->_offset + n]; } std::optional buffer::after_n_m(int n, int m) const { auto [ptr, len] = this->base(); - if (this->offset + n + m > len) + if (this->_offset + n + m > len) { return std::nullopt; } - return std::string_view(ptr + this->offset + n, m); + return std::string_view(ptr + this->_offset + n, m); } std::optional buffer::take() { auto [ptr, len] = this->base(); - if (this->offset >= len) + if (this->_offset >= len) { return std::nullopt; } - auto ch = ptr[this->offset++]; + auto ch = ptr[this->_offset++]; this->update_loc(ch); return ch; } @@ -78,28 +78,28 @@ namespace myxml std::optional buffer::take_n(int n) { auto [ptr, len] = this->base(); - if (offset + n >= len) + if (_offset + n >= len) { return std::nullopt; } - std::string_view strv(ptr + this->offset, n); + std::string_view strv(ptr + this->_offset, n); this->update_loc(strv); - offset += n; + _offset += n; return strv; } std::tuple buffer::cur_loc() { - return {this->line, this->column}; + return {this->_line, this->_column}; } string_buffer::string_buffer(std::string_view inner) - : inner(inner) + : _inner(inner) { } string_buffer::string_buffer(std::string &&inner) - : inner(inner) + : _inner(inner) { } @@ -116,13 +116,13 @@ namespace myxml std::string_view string_buffer::view() const { - if (std::holds_alternative(this->inner)) + if (std::holds_alternative(this->_inner)) { - return std::string_view(std::get(this->inner)); + return std::string_view(std::get(this->_inner)); } else { - return std::get(this->inner); + return std::get(this->_inner); } } } diff --git a/src/cdata.cpp b/src/cdata.cpp index f79542c..f8d417a 100644 --- a/src/cdata.cpp +++ b/src/cdata.cpp @@ -25,17 +25,17 @@ namespace myxml } cdata_impl::cdata_impl(std::string_view str) - : inner(str) + : _inner(str) { } cdata_impl::cdata_impl(std::string &&str) - : inner(str) + : _inner(str) { } void cdata_impl::print(std::ostream &os) const { - os << "inner << "]]>\n"; + os << "_inner << "]]>\n"; } } diff --git a/src/document.cpp b/src/document.cpp index 519e04d..1bfc4a2 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -9,47 +9,47 @@ namespace myxml { void document::set_declaration(const declaration &decl) { - this->decl = decl; + this->_decl = decl; } void document::set_root(std::shared_ptr root) { - this->root = root; + this->_root = root; } const declaration &document::get_declaration() const { - return this->decl; + return this->_decl; } declaration &document::get_declaration() { - return this->decl; + return this->_decl; } const element &document::get_root() const { - return this->root; + return this->_root; } element &document::get_root() { - return this->root; + return this->_root; } element document::first_elem(std::string_view name) { - return this->root.first_elem(name); + return this->_root.first_elem(name); } element document::first_elem() { - return this->root.first_elem(); + return this->_root.first_elem(); } text document::first_text() { - return this->root.first_text(); + return this->_root.first_text(); } document document::parse(std::string_view input) @@ -59,7 +59,7 @@ namespace myxml document document::load(std::string fileName) { - auto f = XMLFile::Open(fileName); + auto f = xml_file::open(fileName); return parser(f).parse_document(); } // std::string document::ExportRaw() const @@ -69,7 +69,7 @@ namespace myxml void document::print(std::ostream &os) const { - os << this->decl << this->root; + os << this->_decl << this->_root; } // std::string document::ExportFormatted(int indentLevel, int indentSize) const // { @@ -78,12 +78,12 @@ namespace myxml void document::entity_encoding(bool flag) { - this->root.entity_encoding(flag); + this->_root.entity_encoding(flag); } void document::platform_specific_newline(bool flag) { - this->root.platform_specific_newline(flag); + this->_root.platform_specific_newline(flag); } std::optional declaration::from_attrs(std::map attrs) diff --git a/src/element.cpp b/src/element.cpp index b33e45c..c573fdb 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -17,7 +17,7 @@ namespace myxml std::string_view element::name() { - return this->_impl->name; + return this->_impl->_name; } element element::parse(std::string_view xml) @@ -50,6 +50,11 @@ namespace myxml return _impl->first(); } + cdata element::first_cdata() + { + return _impl->first(); + } + void element::print(std::ostream &os) const { _impl->print(os); @@ -66,7 +71,7 @@ namespace myxml } element_impl::element_impl(std::string_view name) - : name(name) {} + : _name(name) {} std::shared_ptr element_impl::_new(std::string_view name) { @@ -85,18 +90,18 @@ namespace myxml std::shared_ptr element_impl::load(std::string_view path) { - auto f = XMLFile::Open(path); + auto f = xml_file::open(path); return parser(f).parse_element(); } void element_impl::extend_attributes(std::map attris) { - this->attributes.insert(attris.begin(), attris.end()); + this->_attributes.insert(attris.begin(), attris.end()); } std::string &element_impl::operator[](const std::string &key) { - return this->attributes[key]; + return this->_attributes[key]; } // std::string element_impl::ExportRaw() const @@ -123,8 +128,8 @@ namespace myxml void element_impl::print(std::ostream &os) const { - os << "<" << this->name; - for (const auto &[key, value] : this->attributes) + os << "<" << this->_name; + for (const auto &[key, value] : this->_attributes) { os << "" << key << "=\"" << value << "\""; } @@ -138,7 +143,7 @@ namespace myxml { node->print(os); } - os << "name << ">"; + os << "_name << ">"; } namespace literals diff --git a/src/node.cpp b/src/node.cpp index c3d69ba..5aa1903 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -6,12 +6,12 @@ namespace myxml { void node::entity_encoding(bool flag) { - this->config.entity_encoding = flag; + this->_config.entity_encoding = flag; } void node::platform_specific_newline(bool flag) { - this->config.platform_specific_newline = flag; + this->_config.platform_specific_newline = flag; } std::shared_ptr node::next_sibiling() @@ -60,7 +60,7 @@ namespace myxml } for (auto child = this->firstChild; child != nullptr; child = child->_next) { - if (auto elem = child->as(); elem && elem->name == name) + if (auto elem = child->as(); elem && elem->_name == name) { this->nameToElemBuffer.emplace(name, elem); return elem; @@ -140,7 +140,7 @@ namespace myxml void composite_node::entity_encoding(bool flag) { - this->config.entity_encoding = flag; + this->_config.entity_encoding = flag; for (auto it = this->first_child(); it != nullptr; it = it->_next) { it->entity_encoding(flag); @@ -149,7 +149,7 @@ namespace myxml void composite_node::platform_specific_newline(bool flag) { - this->config.platform_specific_newline = flag; + this->_config.platform_specific_newline = flag; for (auto it = this->first_child(); it != nullptr; it = it->_next) { it->platform_specific_newline(flag); diff --git a/src/parser.cpp b/src/parser.cpp index af8d028..4d765ec 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -209,7 +209,7 @@ namespace myxml std::shared_ptr parser::parse_element_with_header(element_tag header) { auto elem = element_impl::_new(); - elem->name = header.name; + elem->_name = header.name; while (auto ch = this->peek()) { switch (*ch) @@ -234,7 +234,7 @@ namespace myxml case element_tag::closing_type::Closed: { auto child = element_impl::_new(); - child->name = tag->name; + child->_name = tag->name; if (!tag->attrs.empty()) { child->extend_attributes(tag->attrs); @@ -243,7 +243,7 @@ namespace myxml break; } case element_tag::closing_type::Closing: - if (tag->name != elem->name) + if (tag->name != elem->_name) { auto [line, col] = this->cur_loc(); throw syntax_error(fmt::format("elem name in closing tag is mismatched with the header"), line, col); @@ -276,7 +276,7 @@ namespace myxml if (tag->type == element_tag::closing_type::Closed) { auto elem = element_impl::_new(); - elem->name = tag->name; + elem->_name = tag->name; if (!tag->attrs.empty()) { elem->extend_attributes(tag->attrs); diff --git a/src/printable.cpp b/src/printable.cpp index 23350a6..d4c58d7 100644 --- a/src/printable.cpp +++ b/src/printable.cpp @@ -18,12 +18,12 @@ namespace myxml void printable::entity_encoding(bool flag) { - this->config.entity_encoding = flag; + this->_config.entity_encoding = flag; } void printable::platform_specific_newline(bool flag) { - this->config.platform_specific_newline = flag; + this->_config.platform_specific_newline = flag; } std::string printable::str() diff --git a/src/text.cpp b/src/text.cpp index 37b5a39..8bdf709 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -38,7 +38,7 @@ namespace myxml text_impl::text_impl(std::string_view input) { - if (config.entity_encoding) + if (_config.entity_encoding) { // entity encoding static std::map> entityMap = { @@ -95,7 +95,7 @@ namespace myxml void text_impl::print(std::ostream &os) const { - if (!this->config.entity_encoding && !this->config.platform_specific_newline) + if (!this->_config.entity_encoding && !this->_config.platform_specific_newline) { os << this->inner; } @@ -112,7 +112,7 @@ namespace myxml std::size_t len = this->inner.length(); for (std::size_t i = 0; i < len; i++) { - if (this->config.entity_encoding) + if (this->_config.entity_encoding) { if (auto it = entityMap.find(this->inner[i]); it != entityMap.end()) { @@ -121,7 +121,7 @@ namespace myxml start = i + 1; } } - if (this->config.platform_specific_newline) + if (this->_config.platform_specific_newline) { if (this->inner[i] == '\n') { diff --git a/src/xmlfile.cpp b/src/xmlfile.cpp index 207635e..9467414 100644 --- a/src/xmlfile.cpp +++ b/src/xmlfile.cpp @@ -10,43 +10,43 @@ namespace myxml { - XMLFile::XMLFile() - : offset(0) + xml_file::xml_file() + : _offset(0) { } - std::shared_ptr XMLFile::Open(std::string_view fpath) + std::shared_ptr xml_file::open(std::string_view fpath) { // can't use make_shared because XMLFile() is private - auto xfile = std::shared_ptr(new XMLFile()); - xfile->fd = open(fpath.data(), O_RDONLY); - if (xfile->fd == -1) + auto xfile = std::shared_ptr(new xml_file()); + xfile->_fd = open(fpath.data(), O_RDONLY); + if (xfile->_fd == -1) { throw io_error(fmt::format("failed to open file: {}", fpath)); } struct stat fileInfo; - if (fstat(xfile->fd, &fileInfo) == -1) + if (fstat(xfile->_fd, &fileInfo) == -1) { throw io_error(fmt::format("failed to get info of file: {}", fpath)); } - xfile->fileSize = fileInfo.st_size; - void *mappedRegion = mmap(nullptr, xfile->fileSize, PROT_READ, MAP_PRIVATE, xfile->fd, 0); + xfile->_size = fileInfo.st_size; + void *mappedRegion = mmap(nullptr, xfile->_size, PROT_READ, MAP_PRIVATE, xfile->_fd, 0); if (mappedRegion == MAP_FAILED) { throw io_error(fmt::format("failed to map memory for file: {}", fpath)); } - xfile->inner = static_cast(mappedRegion); + xfile->_mapped = static_cast(mappedRegion); return xfile; } - XMLFile::~XMLFile() + xml_file::~xml_file() { - close(this->fd); - munmap(static_cast(this->inner), this->fileSize); + close(this->_fd); + munmap(static_cast(this->_mapped), this->_size); } - std::tuple XMLFile::base() const + std::tuple xml_file::base() const { - return {this->inner, this->fileSize}; + return {this->_mapped, this->_size}; } } \ No newline at end of file diff --git a/tests/element_test.cpp b/tests/element_test.cpp index f5686d7..b0c7e2d 100644 --- a/tests/element_test.cpp +++ b/tests/element_test.cpp @@ -11,39 +11,39 @@ TEST_CASE("Element Impl", "[element]") SECTION("GetName") { - REQUIRE(root->name == "root"); + REQUIRE(root->_name == "root"); } SECTION("Basic Insertion") { root->push_front(child); - REQUIRE(root->first()->name == "child"); - REQUIRE(root->last_child()->as()->name == "child"); + REQUIRE(root->first()->_name == "child"); + REQUIRE(root->last_child()->as()->_name == "child"); } SECTION("Get child by name after insert it") { root->push_front(child); // Unbuffered - REQUIRE(root->first_elem("child")->name == "child"); + REQUIRE(root->first_elem("child")->_name == "child"); // Buffered - REQUIRE(root->first_elem("child")->name == "child"); + REQUIRE(root->first_elem("child")->_name == "child"); } SECTION("Multi child") { root->push_back(child); root->push_back(sibiling); - REQUIRE(root->first_elem("child")->name == "child"); - REQUIRE(root->first_elem("child")->next()->name == "sibiling"); - REQUIRE(root->first_elem("sibiling")->prev()->name == "child"); + REQUIRE(root->first_elem("child")->_name == "child"); + REQUIRE(root->first_elem("child")->next()->_name == "sibiling"); + REQUIRE(root->first_elem("sibiling")->prev()->_name == "child"); } SECTION("Overload []") { - root->attributes["hello"] = "world"; + root->_attributes["hello"] = "world"; REQUIRE((*root)["hello"] == "world"); - REQUIRE(root->attributes["hello"] == "world"); + REQUIRE(root->_attributes["hello"] == "world"); (*root)["hello"] = "bar"; REQUIRE((*root)["hello"] == "bar"); } diff --git a/tests/parser_test.cpp b/tests/parser_test.cpp index 53004bd..92b324c 100644 --- a/tests/parser_test.cpp +++ b/tests/parser_test.cpp @@ -30,7 +30,7 @@ TEST_CASE("Parsing simple xml elements", "[parser]") std::string tooEasy = R"( )"; auto elem = element_impl::parse(tooEasy); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); } SECTION("Text") @@ -39,9 +39,9 @@ TEST_CASE("Parsing simple xml elements", "[parser]") Hello, world! )"; auto elem = element_impl::parse(withText); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n "); - REQUIRE(elem->first()->name == "child"); + REQUIRE(elem->first()->_name == "child"); REQUIRE(elem->first()->first()->str() == "Hello, world!"); } @@ -53,13 +53,13 @@ TEST_CASE("Parsing simple xml elements", "[parser]") )"; auto elem = element_impl::parse(nested); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n "); auto parent = elem->first(); - REQUIRE(parent->name == "parent"); + REQUIRE(parent->_name == "parent"); REQUIRE(parent->first()->str() == "\n "); auto child = parent->first(); - REQUIRE(child->name == "child"); + REQUIRE(child->_name == "child"); REQUIRE(child->first_child() == nullptr); } @@ -71,21 +71,21 @@ TEST_CASE("Parsing simple xml elements", "[parser]") Third )"; auto elem = element_impl::parse(multiLevel); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n "); auto item1 = elem->first()->as(); - REQUIRE(item1->name == "item"); + REQUIRE(item1->_name == "item"); REQUIRE(item1->first()->str() == "First"); // 验证第二个 节点 auto item2 = item1->next(); - REQUIRE(item2->name == "item"); + REQUIRE(item2->_name == "item"); REQUIRE(item2->first()->str() == "Second"); // 验证第三个 节点 auto item3 = item2->next(); - REQUIRE(item3->name == "item"); + REQUIRE(item3->_name == "item"); REQUIRE(item3->first()->str() == "Third"); // 验证 root 节点的最后文本 @@ -98,12 +98,12 @@ TEST_CASE("Parsing simple xml elements", "[parser]") )"; auto elem = element_impl::parse(closed); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n "); // 验证 节点 auto emptyElement = elem->first_child()->next(); - REQUIRE(emptyElement->name == "empty"); + REQUIRE(emptyElement->_name == "empty"); REQUIRE(emptyElement->first_child() == nullptr); // 自闭合标签没有子节点 // 验证 root 节点的最后文本 @@ -117,12 +117,12 @@ TEST_CASE("Parsing simple xml elements", "[parser]") )"; auto elem = element_impl::parse(mixed); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n hello\n "); // 验证 节点 auto child = elem->first_child()->next(); - REQUIRE(child->name == "child"); + REQUIRE(child->_name == "child"); REQUIRE(child->first_child() == nullptr); // 是空的,没有文本子节点 // 验证 root 节点的最后文本 @@ -133,16 +133,16 @@ TEST_CASE("Parsing simple xml elements", "[parser]") { std::string attri = R"()"; auto elem = element_impl::parse(attri); - REQUIRE(elem->name == "root"); - REQUIRE(elem->attributes["hello"] == "world"); + REQUIRE(elem->_name == "root"); + REQUIRE(elem->_attributes["hello"] == "world"); } SECTION("Empty With Attributes") { std::string attri = R"()"; auto elem = element_impl::parse(attri); - REQUIRE(elem->name == "root"); - REQUIRE(elem->attributes["hello"] == "world"); + REQUIRE(elem->_name == "root"); + REQUIRE(elem->_attributes["hello"] == "world"); } SECTION("Multiple Attributes") @@ -150,18 +150,18 @@ TEST_CASE("Parsing simple xml elements", "[parser]") std::string multipleAttributes = R"()"; auto elem = element_impl::parse(multipleAttributes); - REQUIRE(elem->name == "root"); - REQUIRE(elem->attributes["attr1"] == "value1"); - REQUIRE(elem->attributes["attr2"] == "value2"); - REQUIRE(elem->attributes["attr3"] == "value3"); + REQUIRE(elem->_name == "root"); + REQUIRE(elem->_attributes["attr1"] == "value1"); + REQUIRE(elem->_attributes["attr2"] == "value2"); + REQUIRE(elem->_attributes["attr3"] == "value3"); } SECTION("Empty Attributes") { std::string emptyAttribute = R"()"; auto elem = element_impl::parse(emptyAttribute); - REQUIRE(elem->name == "root"); - REQUIRE(elem->attributes["attr"] == ""); + REQUIRE(elem->_name == "root"); + REQUIRE(elem->_attributes["attr"] == ""); } SECTION("Attributes And Children") @@ -170,12 +170,12 @@ TEST_CASE("Parsing simple xml elements", "[parser]") auto elem = element_impl::parse(attributesAndChildren); // 检查根元素的名称和属性 - REQUIRE(elem->name == "root"); - REQUIRE(elem->attributes["attr"] == "value"); + REQUIRE(elem->_name == "root"); + REQUIRE(elem->_attributes["attr"] == "value"); // 检查根元素的第一个子元素 auto child = elem->first(); - REQUIRE(child->name == "child"); + REQUIRE(child->_name == "child"); // 检查子元素的文本内容 REQUIRE(child->first()->str() == "Text"); @@ -189,7 +189,7 @@ TEST_CASE("Parsing simple xml elements", "[parser]") auto elem = element_impl::parse(root); elem->entity_encoding(false); - REQUIRE(elem->name == "root"); + REQUIRE(elem->_name == "root"); REQUIRE(elem->first()->str() == "\n <>\n"); elem->entity_encoding(true);