Skip to content

Commit

Permalink
feat: doc forward some root method
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 18, 2024
1 parent a85bec7 commit 2a5b0f8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
16 changes: 13 additions & 3 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ namespace myxml
/* Query */
declaration &get_declaration();
element &root();
std::optional<element> first_elem(std::string_view);
std::optional<element> first_elem();
std::optional<text> first_text();

/* Wrapped Element method*/
std::optional<element> first_elem(std::string_view name) { return _root.first_elem(name); }
std::optional<element> first_elem() { return _root.first_elem(); }
std::optional<text> first_text() { return _root.first_text(); }
std::optional<cdata> first_cdata() { return _root.first_cdata(); }
std::optional<element> parent() { return _root.parent(); }
template <typename T, typename SFINAE = std::enable_if_t<std::is_base_of_v<interface, T>>>
void push_front(T child) { _root.push_front(child); }
void pop_front() { _root.pop_front(); }
template <typename T, typename SFINAE = std::enable_if_t<std::is_base_of_v<interface, T>>>
void remove(T child) { _root.remove(child); }
void remove_first_element(std::string name) { return _root.remove_first_element(name); }

/** Load */
static document parse(std::string_view);
Expand Down
8 changes: 7 additions & 1 deletion include/myxml/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,19 @@ namespace myxml
_impl->push_front(child.impl());
}

void pop_front()
{
auto first = _impl->first_child();
_impl->unlink(first);
}

template <typename T, typename SFINAE = std::enable_if_t<std::is_base_of_v<interface, T>>>
void remove(T child)
{
_impl->unlink(child.impl());
}
// remove first element whose name is `name`
void remove_first(std::string name);
void remove_first_element(std::string name);

/* Implement printable */
virtual void print(std::ostream &) const override;
Expand Down
15 changes: 0 additions & 15 deletions src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ namespace myxml
return this->_root;
}

std::optional<element> document::first_elem(std::string_view name)
{
return this->_root.first_elem(name);
}

std::optional<element> document::first_elem()
{
return this->_root.first_elem();
}

std::optional<text> document::first_text()
{
return this->_root.first_text();
}

document document::parse(std::string_view input)
{
return parser(input).parse_document();
Expand Down
2 changes: 1 addition & 1 deletion src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace myxml
}
}

void element::remove_first(std::string name)
void element::remove_first_element(std::string name)
{
for (auto child = _impl->first_child(); child != nullptr; child = _impl->next_sibiling())
{
Expand Down
13 changes: 13 additions & 0 deletions tests/document_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ TEST_CASE("Simple document", "[document]")
REQUIRE(doc.first_elem("child").value().first_text().value().str() == "Value");
}

SECTION("Manipulate")
{
using namespace myxml::literals;
auto doc = "<root/>"_doc;
doc.push_front("<child></child>"_elem);
REQUIRE(doc.first_elem().value().name() == "child");
doc.pop_front();
REQUIRE(doc.first_elem() == std::nullopt);
doc.push_front("<child></child>"_elem);
doc.remove_first_element("child");
REQUIRE(doc.first_elem() == std::nullopt);
}

SECTION("With decl")
{
std::string input = R"(<?xml version="1.0" encoding="UTF-8"?>
Expand Down
2 changes: 1 addition & 1 deletion tests/element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ TEST_CASE("Element Interface", "[element]")
REQUIRE(child.parent() == std::nullopt);
REQUIRE(root.first_elem() == std::nullopt);
root.push_back(child);
root.remove_first("child");
root.remove_first_element("child");
REQUIRE(child.parent() == std::nullopt);
REQUIRE(root.first_elem() == std::nullopt);
}
Expand Down

0 comments on commit 2a5b0f8

Please sign in to comment.