summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2021-10-27 20:37:54 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2021-10-27 20:37:54 -0400
commitde870554e43297e1db63b1d4e55f4c7a52a7ce9c (patch)
tree5c124ff57af74223c60c2e543f8e29b7aded8edd
parentd28f63b2cd5b99f1677fc7040fa27a8ba2c736cd (diff)
downloadorcus-de870554e43297e1db63b1d4e55f4c7a52a7ce9c.tar.gz
Add test case for utf-8 value parsing & implement move assignment.
-rw-r--r--include/orcus/css_document_tree.hpp4
-rw-r--r--src/liborcus/css_document_tree.cpp13
-rw-r--r--src/liborcus/css_document_tree_test.cpp79
-rw-r--r--test/css/utf8-1.css13
4 files changed, 79 insertions, 30 deletions
diff --git a/include/orcus/css_document_tree.hpp b/include/orcus/css_document_tree.hpp
index 76d924aa..abbc65a9 100644
--- a/include/orcus/css_document_tree.hpp
+++ b/include/orcus/css_document_tree.hpp
@@ -38,6 +38,8 @@ public:
css_document_tree(css_document_tree&& other);
~css_document_tree();
+ css_document_tree& operator=(css_document_tree&& other);
+
/**
* Load raw string stream containing CSS rules to populate the document
* tree.
@@ -87,6 +89,8 @@ public:
get_all_properties(const css_selector_t& selector) const;
void dump() const;
+
+ void swap(css_document_tree& other) noexcept;
};
}
diff --git a/src/liborcus/css_document_tree.cpp b/src/liborcus/css_document_tree.cpp
index 0cb0688e..5f84c013 100644
--- a/src/liborcus/css_document_tree.cpp
+++ b/src/liborcus/css_document_tree.cpp
@@ -541,6 +541,14 @@ css_document_tree::~css_document_tree()
{
}
+css_document_tree& css_document_tree::operator=(css_document_tree&& other)
+{
+ css_document_tree tmp(std::move(other));
+ swap(tmp);
+
+ return *this;
+}
+
void css_document_tree::load(std::string_view stream)
{
if (stream.empty())
@@ -633,6 +641,11 @@ void css_document_tree::dump() const
}
}
+void css_document_tree::swap(css_document_tree& other) noexcept
+{
+ mp_impl.swap(other.mp_impl);
+}
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/src/liborcus/css_document_tree_test.cpp b/src/liborcus/css_document_tree_test.cpp
index b3826f52..a49792c9 100644
--- a/src/liborcus/css_document_tree_test.cpp
+++ b/src/liborcus/css_document_tree_test.cpp
@@ -54,6 +54,22 @@ bool check_prop(const css_properties_t& props, std::string_view key, std::string
return true;
}
+using check_properties_type = std::vector<std::pair<std::string, std::string>>;
+
+bool check_props(const css_properties_t& props, check_properties_type expected)
+{
+ bool pass = true;
+
+ for (const auto& [key, value] : expected)
+ {
+ bool res = check_prop(props, key, value);
+ if (!res)
+ pass = false;
+ }
+
+ return pass;
+}
+
css_document_tree load_document(const fs::path& path)
{
std::cout << path << std::endl;
@@ -450,11 +466,7 @@ void test_css_parse_basic10()
void test_css_parse_basic11()
{
- fs::path path = SRCDIR"/test/css/basic11.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/basic11.css");
css_selector_t selector;
selector.first.classes.insert("callout");
@@ -471,11 +483,7 @@ void test_css_parse_basic11()
void test_css_parse_basic12()
{
- fs::path path = SRCDIR"/test/css/basic12.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/basic12.css");
css_selector_t selector;
selector.first.name = "div";
@@ -510,11 +518,7 @@ void test_css_parse_basic12()
void test_css_parse_basic13()
{
- fs::path path = SRCDIR"/test/css/basic13.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/basic13.css");
css_selector_t selector;
selector.first.id = "p1";
@@ -545,11 +549,7 @@ void test_css_parse_basic13()
void test_css_parse_basic14()
{
- fs::path path = SRCDIR"/test/css/basic14.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/basic14.css");
css_selector_t selector;
selector.first.id = "p1";
@@ -580,11 +580,7 @@ void test_css_parse_basic14()
void test_css_parse_chained1()
{
- fs::path path = SRCDIR"/test/css/chained1.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/chained1.css");
css_selector_t selector;
selector.first.name = "div";
@@ -615,11 +611,7 @@ void test_css_parse_chained1()
void test_css_parse_chained2()
{
- fs::path path = SRCDIR"/test/css/chained2.css";
- std::cout << path << std::endl;
- file_content content(path.string());
- css_document_tree doc;
- doc.load(content.str());
+ css_document_tree doc = load_document(SRCDIR"/test/css/chained2.css");
// Build selector '#id1 table.data td'.
css_selector_t selector;
@@ -643,7 +635,33 @@ void test_css_parse_chained2()
void test_css_parse_utf8_1()
{
+ css_document_tree doc = load_document(SRCDIR"/test/css/utf8-1.css");
+ css_document_tree doc2;
+ doc2 = std::move(doc); // test the move assignment operator.
+
+ css_selector_t selector;
+ selector.first.classes.insert("style17");
+
+ const css_properties_t* props = doc2.get_properties(selector, 0);
+ assert(props);
+ assert(props->size() == 11);
+
+ check_properties_type expected = {
+ { "mso-pattern", "auto none" },
+ { "background", "#EDEDED" },
+ { "mso-style-name", "20% - 强调文字颜色 3" },
+ { "color", "#000000" },
+ { "font-size", "11.0pt" },
+ { "font-weight", "400" },
+ { "font-style", "normal" },
+ { "font-family", "宋体" },
+ { "text-decoration", "none" },
+ { "mso-generic-font-family", "auto" },
+ { "mso-font-charset", "0" },
+ };
+
+ assert(check_props(*props, expected));
}
int main()
@@ -667,6 +685,7 @@ int main()
test_css_parse_basic14();
test_css_parse_chained1();
test_css_parse_chained2();
+ test_css_parse_utf8_1();
return EXIT_SUCCESS;
}
diff --git a/test/css/utf8-1.css b/test/css/utf8-1.css
new file mode 100644
index 00000000..1a509eb0
--- /dev/null
+++ b/test/css/utf8-1.css
@@ -0,0 +1,13 @@
+
+.style17
+ {mso-pattern:auto none;
+ background:#EDEDED;
+ color:#000000;
+ font-size:11.0pt;
+ font-weight:400;
+ font-style:normal;
+ text-decoration:none;
+ font-family:宋体;
+ mso-generic-font-family:auto;
+ mso-font-charset:0;
+ mso-style-name:"20% - 强调文字颜色 3";}