]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxMarkupParser::Strip().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:46:48 +0000 (12:46 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:46:48 +0000 (12:46 +0000)
This helper function strips all markup tags from the string (and also decodes
the XML entities in it).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67049 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/private/markupparser.h
src/common/markupparser.cpp
tests/controls/markuptest.cpp

index e0eb0a80999d4ee11031edf9d4b016accb25ba1f..400cbe9d3cd55af33a35a63e42eb5cdfa8e54ef4 100644 (file)
@@ -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 "&amp;" 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
index 535e62bade558b3009e2c6988648efc9fe0ef1c8..8320e35eca8f2784dcbaf4bc710934aebd52dbf2 100644 (file)
@@ -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();
+}
index 789598b946df237bfacbf860c47d7518972e09a4..3ebc3142aef8db41f758fe739901cd641842f1d6 100644 (file)
@@ -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&amp;B", wxMarkupParser::Quote("B&B") );
     CPPUNIT_ASSERT_EQUAL( "&quot;&quot;", 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( "&lt;foo&gt;", "<foo>" );
+    CHECK_STRIP( "<b>Big</b> problem", "Big problem" );
+    CHECK_STRIP( "<span foreground=\"red\">c</span>"
+                 "<span background=\"green\">o</span>"
+                 "<span background=\"blue\">l</span>"
+                 "<span background=\"green\">o</span>"
+                 "<span foreground=\"yellow\">u</span>"
+                 "<span background=\"green\">r</span>",
+                 "colour" );
+
+    #undef CHECK_STRIP
+}