diff --git a/include/wx/private/markupparser.h b/include/wx/private/markupparser.h index e0eb0a8099..400cbe9d3c 100644 --- a/include/wx/private/markupparser.h +++ b/include/wx/private/markupparser.h @@ -136,6 +136,11 @@ public: // interpreted as tag opening characters. static wxString Quote(const wxString& text); + // Strip markup from a string, i.e. simply remove all tags and replace + // XML entities with their values (or with "&&" in case of "&" to + // prevent it from being interpreted as mnemonic marker). + static wxString Strip(const wxString& text); + private: // Simple struct combining the name of a tag and its attributes. struct TagAndAttrs diff --git a/src/common/markupparser.cpp b/src/common/markupparser.cpp index 535e62bade..8320e35eca 100644 --- a/src/common/markupparser.cpp +++ b/src/common/markupparser.cpp @@ -425,3 +425,51 @@ wxString wxMarkupParser::Quote(const wxString& text) return quoted; } + +/* static */ +wxString wxMarkupParser::Strip(const wxString& text) +{ + class StripOutput : public wxMarkupParserOutput + { + public: + StripOutput() { } + + const wxString& GetText() const { return m_text; } + + virtual void OnText(const wxString& text) { m_text += text; } + + virtual void OnBoldStart() { } + virtual void OnBoldEnd() { } + + virtual void OnItalicStart() { } + virtual void OnItalicEnd() { } + + virtual void OnUnderlinedStart() { } + virtual void OnUnderlinedEnd() { } + + virtual void OnStrikethroughStart() { } + virtual void OnStrikethroughEnd() { } + + virtual void OnBigStart() { } + virtual void OnBigEnd() { } + + virtual void OnSmallStart() { } + virtual void OnSmallEnd() { } + + virtual void OnTeletypeStart() { } + virtual void OnTeletypeEnd() { } + + virtual void OnSpanStart(const wxMarkupSpanAttributes& WXUNUSED(a)) { } + virtual void OnSpanEnd(const wxMarkupSpanAttributes& WXUNUSED(a)) { } + + private: + wxString m_text; + }; + + StripOutput output; + wxMarkupParser parser(output); + if ( !parser.Parse(text) ) + return wxString(); + + return output.GetText(); +} diff --git a/tests/controls/markuptest.cpp b/tests/controls/markuptest.cpp index 789598b946..3ebc3142ae 100644 --- a/tests/controls/markuptest.cpp +++ b/tests/controls/markuptest.cpp @@ -27,10 +27,12 @@ private: CPPUNIT_TEST_SUITE( MarkupTestCase ); CPPUNIT_TEST( RoundTrip ); CPPUNIT_TEST( Quote ); + CPPUNIT_TEST( Strip ); CPPUNIT_TEST_SUITE_END(); void RoundTrip(); void Quote(); + void Strip(); wxDECLARE_NO_COPY_CLASS(MarkupTestCase); }; @@ -196,3 +198,23 @@ void MarkupTestCase::Quote() CPPUNIT_ASSERT_EQUAL( "B&B", wxMarkupParser::Quote("B&B") ); CPPUNIT_ASSERT_EQUAL( """", wxMarkupParser::Quote("\"\"") ); } + +void MarkupTestCase::Strip() +{ + #define CHECK_STRIP( text, stripped ) \ + CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) ) + + CHECK_STRIP( "", "" ); + CHECK_STRIP( "foo", "foo" ); + CHECK_STRIP( "<foo>", "" ); + CHECK_STRIP( "Big problem", "Big problem" ); + CHECK_STRIP( "c" + "o" + "l" + "o" + "u" + "r", + "colour" ); + + #undef CHECK_STRIP +}