Evas textblock: Added evas_textblock_markup_to_plain.

This function converts a textblock markup to plain text.
It converts for example <br/> to \n and a lot more.

SVN revision: 66034
This commit is contained in:
Tom Hacohen 2011-12-08 15:12:25 +00:00
parent a0fa810e82
commit 346e25b031
4 changed files with 61 additions and 0 deletions

View File

@ -536,3 +536,6 @@
* 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.

View 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.

View 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 */
/**

View 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