diff --git a/cocos/ui/UIRichText.cpp b/cocos/ui/UIRichText.cpp index 824468f04a..e3708c6364 100644 --- a/cocos/ui/UIRichText.cpp +++ b/cocos/ui/UIRichText.cpp @@ -526,12 +526,14 @@ bool RichText::initWithXML(const std::string& origxml) // - makes sure that the xml well formed and starts with an element auto xml = "" + origxml + ""; - if (document.Parse(xml.c_str(), xml.length()) == tinyxml2::XML_SUCCESS) + auto error = document.Parse(xml.c_str(), xml.length()); + if (error == tinyxml2::XML_SUCCESS) { MyXMLVisitor visitor(this); document.Accept(&visitor); return true; } + CCLOG("cocos2d: UI::RichText: Error parsing xml: %s, %s", document.GetErrorStr1(), document.GetErrorStr2()); } return false; } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp index 0cc13bc70e..a75d34c22e 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp @@ -18,6 +18,7 @@ UIRichTextTests::UIRichTextTests() ADD_TEST_CASE(UIRichTextXMLUrl); ADD_TEST_CASE(UIRichTextXMLFace); ADD_TEST_CASE(UIRichTextXMLBR); + ADD_TEST_CASE(UIRichTextXMLInvalid); } @@ -797,3 +798,41 @@ void UIRichTextXMLBR::touchEvent(Ref *pSender, Widget::TouchEventType type) break; } } + +// +// UIRichTextXMLInvalid +// +bool UIRichTextXMLInvalid::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize(); + + // Add the alert + Text *alert = Text::create("Invalid XML test", "fonts/Marker Felt.ttf", 30); + alert->setColor(Color3B(159, 168, 176)); + alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125)); + _widget->addChild(alert); + + + // RichText + _richText = RichText::createWithXML("this is an invalid xml. no closing tag"); + if (_richText) + { + _richText->ignoreContentAdaptWithSize(false); + _richText->setContentSize(Size(100, 100)); + + _richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2)); + _richText->setLocalZOrder(10); + + + _widget->addChild(_richText); + + // test remove all children, this call won't effect the test + _richText->removeAllChildren(); + } + return true; + } + return false; +} + diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h index bca79d5654..d6c6264381 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h @@ -139,6 +139,17 @@ protected: cocos2d::ui::RichText* _richText; }; +class UIRichTextXMLInvalid : public UIScene +{ +public: + CREATE_FUNC(UIRichTextXMLInvalid); + + bool init() override; + +protected: + cocos2d::ui::RichText* _richText; +}; + #endif /* defined(__TestCpp__UIRichTextTest__) */