diff options
-rw-r--r-- | assets/cru/ui/DefaultResources.xml | 4 | ||||
-rw-r--r-- | include/cru/xml/XmlNode.h | 25 | ||||
-rw-r--r-- | src/theme_builder/components/conditions/CompoundConditionEditor.cpp | 4 | ||||
-rw-r--r-- | src/ui/ThemeResourceDictionary.cpp | 2 | ||||
-rw-r--r-- | src/xml/XmlNode.cpp | 16 | ||||
-rw-r--r-- | src/xml/XmlParser.cpp | 21 | ||||
-rw-r--r-- | test/xml/ParserTest.cpp | 13 |
7 files changed, 73 insertions, 12 deletions
diff --git a/assets/cru/ui/DefaultResources.xml b/assets/cru/ui/DefaultResources.xml index c779a5b9..795ef1a0 100644 --- a/assets/cru/ui/DefaultResources.xml +++ b/assets/cru/ui/DefaultResources.xml @@ -1,10 +1,10 @@ <Theme> + <!-- Following icons are from bootstrap icons.--> <Resource key="icon.close"> <String value="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z" /> </Resource> - <Resource key="icon.plus-square"> - <String value="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/> + <String value="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z" /> </Resource> <Resource key="text.font"> diff --git a/include/cru/xml/XmlNode.h b/include/cru/xml/XmlNode.h index 737a2d87..2f46459a 100644 --- a/include/cru/xml/XmlNode.h +++ b/include/cru/xml/XmlNode.h @@ -12,12 +12,13 @@ namespace cru::xml { class XmlElementNode; class XmlTextNode; +class XmlCommentNode; class CRU_XML_API XmlNode { friend XmlElementNode; public: - enum class Type { Text, Element }; + enum class Type { Text, Element, Comment }; protected: explicit XmlNode(Type type) : type_(type) {} @@ -35,11 +36,14 @@ class CRU_XML_API XmlNode { bool IsTextNode() const { return type_ == Type::Text; } bool IsElementNode() const { return type_ == Type::Element; } + bool IsCommentNode() const { return type_ == Type::Comment; } XmlElementNode* AsElement(); XmlTextNode* AsText(); + XmlCommentNode* AsComment(); const XmlElementNode* AsElement() const; const XmlTextNode* AsText() const; + const XmlCommentNode* AsComment() const; private: const Type type_; @@ -132,4 +136,23 @@ class CRU_XML_API XmlElementNode : public XmlNode { std::vector<XmlNode*> children_; }; +class CRU_XML_API XmlCommentNode : public XmlNode { + public: + XmlCommentNode() : XmlNode(Type::Comment) {} + explicit XmlCommentNode(String text) + : XmlNode(Type::Comment), text_(std::move(text)) {} + + CRU_DELETE_COPY(XmlCommentNode) + CRU_DELETE_MOVE(XmlCommentNode) + + ~XmlCommentNode() override; + + String GetText() const { return text_; } + void SetText(String text) { text_ = std::move(text); } + + XmlNode* Clone() const override; + + private: + String text_; +}; } // namespace cru::xml diff --git a/src/theme_builder/components/conditions/CompoundConditionEditor.cpp b/src/theme_builder/components/conditions/CompoundConditionEditor.cpp index a0985eff..01c5db4c 100644 --- a/src/theme_builder/components/conditions/CompoundConditionEditor.cpp +++ b/src/theme_builder/components/conditions/CompoundConditionEditor.cpp @@ -24,7 +24,7 @@ CompoundConditionEditorChild::CompoundConditionEditorChild( remove_button_.SetIconWithSvgPathDataStringResourceKey(u"icon.close", {0, 0, 16, 16}); remove_button_.SetPreferredSize({24, 24}); - remove_button_.SetPadding(ui::Thickness(4)); + remove_button_.SetPadding(ui::Thickness(2)); remove_button_.SetIconFillColor(ui::colors::red); container_.AddChild(condition_editor_->GetRootControl()); @@ -50,7 +50,7 @@ CompoundConditionEditor::CompoundConditionEditor() { add_child_button_.GetButton()->SetIconWithSvgPathDataStringResourceKey( u"icon.plus-square", {0, 0, 16, 16}); add_child_button_.GetButton()->SetPreferredSize({24, 24}); - add_child_button_.GetButton()->SetPadding(ui::Thickness(4)); + add_child_button_.GetButton()->SetPadding(ui::Thickness(2)); add_child_button_.GetButton()->SetIconFillColor(ui::colors::green); add_child_button_.SetMenuItems({u"And Condition", u"Or Condition", u"Click State Condition", u"Focus Condition", diff --git a/src/ui/ThemeResourceDictionary.cpp b/src/ui/ThemeResourceDictionary.cpp index d9483f6c..97b4a74a 100644 --- a/src/ui/ThemeResourceDictionary.cpp +++ b/src/ui/ThemeResourceDictionary.cpp @@ -50,7 +50,7 @@ void ThemeResourceDictionary::UpdateResourceMap(xml::XmlElementNode* xml_root) { CRU_LOG_DEBUG(u"Ignore unknown element {} of theme.", c->GetTag()); } } else { - CRU_LOG_WARN(u"Ignore text node of theme."); + CRU_LOG_DEBUG(u"Ignore text or comment node of theme."); } } } 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<XmlTextNode*>(this); } +XmlCommentNode* XmlNode::AsComment() { + return static_cast<XmlCommentNode*>(this); +} + const XmlElementNode* XmlNode::AsElement() const { return static_cast<const XmlElementNode*>(this); } @@ -17,6 +21,10 @@ const XmlTextNode* XmlNode::AsText() const { return static_cast<const XmlTextNode*>(this); } +const XmlCommentNode* XmlNode::AsComment() const { + return static_cast<const XmlCommentNode*>(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<XmlElementNode*>(pseudo_root_node_->GetChildren()[0]); diff --git a/test/xml/ParserTest.cpp b/test/xml/ParserTest.cpp index 04c25594..06536bfa 100644 --- a/test/xml/ParserTest.cpp +++ b/test/xml/ParserTest.cpp @@ -68,6 +68,14 @@ TEST(CruXmlParserTest, Whitespace) { delete n; } +TEST(CruXmlParserTest, Comment) { + XmlParser parser(u"<!-- comment -->"); + auto n = parser.Parse(); + ASSERT_TRUE(n->IsCommentNode()); + ASSERT_EQ(n->AsComment()->GetText(), u"comment"); + delete n; +} + TEST(CruXmlParserTest, Complex) { XmlParser parser( uR"( @@ -83,11 +91,12 @@ TEST(CruXmlParserTest, Complex) { <d3></d3> t2 </c2> + <!-- comment --> </root> )"); auto n = parser.Parse(); ASSERT_EQ(n->GetAttribute(u"a1"), u"v1"); - ASSERT_EQ(n->GetChildCount(), 2); + ASSERT_EQ(n->GetChildCount(), 3); ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetTag(), u"c1"); ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetChildCount(), 1); auto c2 = n->GetChildAt(1)->AsElement(); @@ -101,5 +110,7 @@ TEST(CruXmlParserTest, Complex) { ASSERT_EQ(c2->GetChildAt(2)->AsText()->GetText(), u"text test"); ASSERT_EQ(c2->GetChildAt(3)->AsElement()->GetTag(), u"d3"); ASSERT_EQ(c2->GetChildAt(4)->AsText()->GetText(), u"t2"); + ASSERT_TRUE(n->GetChildAt(2)->IsCommentNode()); + ASSERT_EQ(n->GetChildAt(2)->AsComment()->GetText(), u"comment"); delete n; } |