mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-30 17:36:16 +01:00
28f781501e
2007-08-08 Matthias Clasen <mclasen@redhat.com> * glib/gmarkup.c (append_escaped_text): Handle restricted characters by converting them to numeric character entities. (#464145, Andreas Monitzer) * tests/markup-escape-test.c: Add tests for restricted characters and numeric character entities. svn path=/trunk/; revision=5684
133 lines
2.9 KiB
C
133 lines
2.9 KiB
C
#undef G_DISABLE_ASSERT
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <glib.h>
|
|
|
|
static void test_format (const gchar *format,
|
|
const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
|
|
|
|
static gboolean error = FALSE;
|
|
|
|
static void
|
|
test (const gchar *original,
|
|
const gchar *expected)
|
|
{
|
|
gchar *result = g_markup_escape_text (original, -1);
|
|
|
|
if (strcmp (result, expected) != 0)
|
|
{
|
|
g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
|
|
expected, result);
|
|
error = TRUE;
|
|
}
|
|
|
|
g_free (result);
|
|
}
|
|
|
|
static void
|
|
test_unichar (gunichar c,
|
|
gboolean entity)
|
|
{
|
|
gint len;
|
|
gchar outbuf[7], expected[12];
|
|
|
|
len = g_unichar_to_utf8 (c, outbuf);
|
|
outbuf[len] = 0;
|
|
|
|
if (entity)
|
|
g_snprintf (expected, 12, "&#x%x;", c);
|
|
else
|
|
strcpy (expected, outbuf);
|
|
|
|
test (outbuf, expected);
|
|
}
|
|
|
|
static void
|
|
test_format (const gchar *format,
|
|
const gchar *expected,
|
|
...)
|
|
{
|
|
gchar *result;
|
|
|
|
va_list args;
|
|
|
|
va_start (args, expected);
|
|
result = g_markup_vprintf_escaped (format, args);
|
|
va_end (args);
|
|
|
|
if (strcmp (result, expected) != 0)
|
|
{
|
|
g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
|
|
expected, result);
|
|
error = TRUE;
|
|
}
|
|
|
|
g_free (result);
|
|
}
|
|
|
|
int main (int argc, char **argv)
|
|
{
|
|
/* Tests for g_markup_escape_text() */
|
|
test ("&", "&");
|
|
test ("<", "<");
|
|
test (">", ">");
|
|
test ("'", "'");
|
|
test ("\"", """);
|
|
|
|
test ("", "");
|
|
test ("A", "A");
|
|
test ("A&", "A&");
|
|
test ("&A", "&A");
|
|
test ("A&A", "A&A");
|
|
test ("&&A", "&&A");
|
|
test ("A&&", "A&&");
|
|
test ("A&&A", "A&&A");
|
|
test ("A&A&A", "A&A&A");
|
|
test ("AA", "A&#23;A");
|
|
test ("A
A", "A&#xa;A");
|
|
test_unichar (0x1, TRUE);
|
|
test_unichar (0x8, TRUE);
|
|
test_unichar (0x9, FALSE);
|
|
test_unichar (0xa, FALSE);
|
|
test_unichar (0xb, TRUE);
|
|
test_unichar (0xc, TRUE);
|
|
test_unichar (0xd, FALSE);
|
|
test_unichar (0xe, TRUE);
|
|
test_unichar (0x1f, TRUE);
|
|
test_unichar (0x20, FALSE);
|
|
test_unichar (0x7e, FALSE);
|
|
test_unichar (0x7f, TRUE);
|
|
test_unichar (0x84, TRUE);
|
|
test_unichar (0x85, FALSE);
|
|
test_unichar (0x86, TRUE);
|
|
test_unichar (0x9f, TRUE);
|
|
test_unichar (0xa0, FALSE);
|
|
|
|
/* Tests for g_markup_printf_escaped() */
|
|
test_format ("A", "A");
|
|
test_format ("A%s", "A&", "&");
|
|
test_format ("%sA", "&A", "&");
|
|
test_format ("A%sA", "A&A", "&");
|
|
test_format ("%s%sA", "&&A", "&", "&");
|
|
test_format ("A%s%s", "A&&", "&", "&");
|
|
test_format ("A%s%sA", "A&&A", "&", "&");
|
|
test_format ("A%sA%sA", "A&A&A", "&", "&");
|
|
|
|
test_format ("%s", "<B>&",
|
|
"<B>&");
|
|
test_format ("%c%c", "<&",
|
|
'<', '&');
|
|
test_format (".%c.%c.", ".<.&.",
|
|
'<', '&');
|
|
test_format ("%s", "",
|
|
"");
|
|
test_format ("%-5s", "A ",
|
|
"A");
|
|
test_format ("%2$s%1$s", "B.A.",
|
|
"A.", "B.");
|
|
|
|
return error ? 1 : 0;
|
|
}
|