Evas textblock: Added evas_textblock_markup_to_plain.
authortasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 8 Dec 2011 15:12:25 +0000 (15:12 +0000)
committertasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 8 Dec 2011 15:12:25 +0000 (15:12 +0000)
This function converts a textblock markup to plain text.
It converts for example <br/> to \n and a lot more.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/evas@66034 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

ChangeLog
src/lib/Evas.h
src/lib/canvas/evas_object_textblock.c
src/tests/evas_test_textblock.c

index 05b7c12..8a411ff 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        * Textblock markup: Support self closing format tags, i.e <br/>.
        You should use <br/> and not <br>. The latter still works but it's use
        is discouraged.
+       * Textblock: Added evas_textblock_markup_to_plain.
+       This lets you convert textblock markup to plain text.
+       This converts formats and everything correctly.
index 13df7e8..e726ad0 100644 (file)
@@ -8101,6 +8101,19 @@ EAPI const char                  *evas_textblock_string_escape_get(const char *s
  */
 EAPI const char                  *evas_textblock_escape_string_range_get(const char *escape_start, const char *escape_end) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2) EINA_PURE;
 
+/**
+ * Return the plain version of the markup.
+ *
+ * Works as if you set the markup to a textblock and then retrieve the plain
+ * version of the text. i.e: <br> and <\n> will be replaced with \n, &...; with
+ * the actual char and etc.
+ *
+ * @param obj the textblock object to work with.
+ * @param text the markup text
+ * @return an allocated plain text version of the markup
+ * @since 1.2.0
+ */
+EAPI char                        *evas_textblock_markup_to_plain(const Evas_Object *obj, const char *text) EINA_WARN_UNUSED_RESULT EINA_MALLOC EINA_ARG_NONNULL(1, 2);
 
 /**
  * Creates a new textblock style.
index 65be79a..cfc4753 100644 (file)
@@ -5084,6 +5084,36 @@ evas_object_textblock_text_markup_get(const Evas_Object *obj)
    return o->markup_text;
 }
 
+EAPI char *
+evas_textblock_markup_to_plain(const Evas_Object *obj, const char *text)
+{
+   /* FIXME: Can be done better, this is the least redundant way of doing it,
+    * but by far the slowest, when the time comes, this should be
+    * re-implemented. */
+   char *ret;
+   Evas_Object *obj2;
+   Evas_Textblock_Cursor *cur1, *cur2;
+   obj2 = evas_object_textblock_add(evas_object_evas_get(obj));
+   /* Shouldn't have been const, casting is ok, or at least conforms
+    * with the rest of the ugliness in this func*/
+   evas_object_textblock_style_set(obj2,
+         (Evas_Textblock_Style *) evas_object_textblock_style_get(obj));
+   evas_object_textblock_legacy_newline_set(obj2,
+         evas_object_textblock_legacy_newline_get(obj));
+
+   evas_object_textblock_text_markup_set(obj2, text);
+   cur1 = evas_object_textblock_cursor_get(obj2);
+   cur2 = evas_object_textblock_cursor_new(obj2);
+   evas_textblock_cursor_paragraph_first(cur1);
+   evas_textblock_cursor_paragraph_last(cur2);
+   ret = evas_textblock_cursor_range_text_get(cur1, cur2,
+         EVAS_TEXTBLOCK_TEXT_PLAIN);
+   evas_textblock_cursor_free(cur2);
+   evas_object_del(obj2);
+
+   return ret;
+}
+
 /* cursors */
 
 /**
index 33fff72..b8526b8 100644 (file)
@@ -1492,6 +1492,21 @@ START_TEST(evas_textblock_text_getters)
    fail_if(strcmp(evas_textblock_cursor_range_text_get(cur, main_cur,
             EVAS_TEXTBLOCK_TEXT_MARKUP), "aaa"));
 
+   /* Markup to plain */
+     {
+        char *tmp = evas_textblock_markup_to_plain(tb, "<br/>aa<\n/>bb<\t/>");
+        fail_if(strcmp(tmp, "\naa\nbb\t"));
+        free(tmp);
+
+        tmp = evas_textblock_markup_to_plain(tb, "a<item></item>");
+        fail_if(strcmp(tmp, "a\xEF\xBF\xBC"));
+        free(tmp);
+
+        tmp = evas_textblock_markup_to_plain(tb, "a&nbsp;");
+        fail_if(strcmp(tmp, "a\xC2\xA0"));
+        free(tmp);
+     }
+
    END_TB_TEST();
 }
 END_TEST