1 /* gmarkup.c - Simple XML-like parser
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
32 g_markup_error_quark ()
34 static GQuark error_quark = 0;
37 error_quark = g_quark_from_static_string ("g-markup-error-quark");
42 typedef struct _GMarkupAttribute GMarkupAttribute;
44 struct _GMarkupAttribute
50 static GMarkupAttribute*
51 attribute_new (const gchar *name, const gchar *value)
53 GMarkupAttribute *attr;
55 attr = g_new (GMarkupAttribute, 1);
57 /* name/value are allowed to be NULL */
58 attr->name = g_strdup (name);
59 attr->value = g_strdup (value);
65 attribute_free (GMarkupAttribute *attr)
75 STATE_AFTER_OPEN_ANGLE,
76 STATE_AFTER_CLOSE_ANGLE,
77 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
78 STATE_INSIDE_OPEN_TAG_NAME,
79 STATE_INSIDE_ATTRIBUTE_NAME,
80 STATE_BETWEEN_ATTRIBUTES,
81 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
82 STATE_INSIDE_ATTRIBUTE_VALUE,
84 STATE_AFTER_CLOSE_TAG_SLASH,
85 STATE_INSIDE_CLOSE_TAG_NAME,
86 STATE_INSIDE_PASSTHROUGH,
90 struct _GMarkupParseContext
92 const GMarkupParser *parser;
94 GMarkupParseFlags flags;
100 GDestroyNotify dnotify;
102 /* A piece of character data or an element that
103 * hasn't "ended" yet so we haven't yet called
104 * the callback for it.
106 GString *partial_chunk;
108 GMarkupParseState state;
112 const gchar *current_text;
113 gint current_text_len;
114 const gchar *current_text_end;
116 GString *leftover_char_portion;
118 /* used to save the start of the last interesting thingy */
123 guint document_empty : 1;
128 * g_markup_parse_context_new:
129 * @parser: a #GMarkupParser
130 * @flags: one or more #GMarkupParseFlags
131 * @user_data: user data to pass to #GMarkupParser functions
132 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
134 * Creates a new parse context. A parse context is used to parse
135 * marked-up documents. You can feed any number of documents into
136 * a context, as long as no errors occur; once an error occurs,
137 * the parse context can't continue to parse text (you have to free it
138 * and create a new parse context).
140 * Return value: a new #GMarkupParseContext
142 GMarkupParseContext *
143 g_markup_parse_context_new (const GMarkupParser *parser,
144 GMarkupParseFlags flags,
146 GDestroyNotify user_data_dnotify)
148 GMarkupParseContext *context;
150 g_return_val_if_fail (parser != NULL, NULL);
152 context = g_new (GMarkupParseContext, 1);
154 context->parser = parser;
155 context->flags = flags;
156 context->user_data = user_data;
157 context->dnotify = user_data_dnotify;
159 context->line_number = 1;
160 context->char_number = 1;
162 context->partial_chunk = NULL;
164 context->state = STATE_START;
165 context->tag_stack = NULL;
166 context->attributes = NULL;
168 context->current_text = NULL;
169 context->current_text_len = -1;
170 context->current_text_end = NULL;
171 context->leftover_char_portion = NULL;
173 context->start = NULL;
174 context->iter = NULL;
176 context->document_empty = TRUE;
177 context->parsing = FALSE;
183 * g_markup_parse_context_free:
184 * @context: a #GMarkupParseContext
186 * Frees a #GMarkupParseContext. Can't be called from inside
187 * one of the #GMarkupParser functions.
191 g_markup_parse_context_free (GMarkupParseContext *context)
193 g_return_if_fail (context != NULL);
194 g_return_if_fail (!context->parsing);
196 if (context->dnotify)
197 (* context->dnotify) (context->user_data);
199 g_slist_foreach (context->attributes, (GFunc)attribute_free, NULL);
200 g_slist_free (context->attributes);
202 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
203 g_slist_free (context->tag_stack);
205 if (context->partial_chunk)
206 g_string_free (context->partial_chunk, TRUE);
208 if (context->leftover_char_portion)
209 g_string_free (context->leftover_char_portion, TRUE);
215 attribute_list_to_arrays (GSList *attributes,
226 len = g_slist_length (attributes);
230 names = g_new (gchar*, len + 1);
238 values = g_new (gchar*, len + 1);
244 /* We want to reverse the list, since it's
245 * backward from the order the attributes appeared
249 tmp_list = attributes;
252 GMarkupAttribute *attr = tmp_list->data;
257 names[i] = g_strdup (attr->name);
260 values[i] = g_strdup (attr->value);
262 tmp_list = g_slist_next (tmp_list);
277 mark_error (GMarkupParseContext *context,
280 context->state = STATE_ERROR;
282 if (context->parser->error)
283 (*context->parser->error) (context, error, context->user_data);
287 set_error (GMarkupParseContext *context,
297 va_start (args, format);
298 s = g_strdup_vprintf (format, args);
301 tmp_error = g_error_new (G_MARKUP_ERROR,
303 _("Error on line %d char %d: %s"),
304 context->line_number,
305 context->char_number,
310 mark_error (context, tmp_error);
312 g_propagate_error (error, tmp_error);
316 is_name_start_char (gunichar c)
318 if (g_unichar_isalpha (c) ||
327 is_name_char (gunichar c)
329 if (g_unichar_isalnum (c) ||
341 char_str (gunichar c,
345 g_unichar_to_utf8 (c, buf);
350 utf8_str (const gchar *utf8,
353 char_str (g_utf8_get_char (utf8), buf);
358 set_unescape_error (GMarkupParseContext *context,
360 const gchar *remaining_text,
361 const gchar *remaining_text_end,
369 gint remaining_newlines;
372 remaining_newlines = 0;
374 while (p != remaining_text_end)
377 ++remaining_newlines;
381 va_start (args, format);
382 s = g_strdup_vprintf (format, args);
385 tmp_error = g_error_new (G_MARKUP_ERROR,
387 _("Error on line %d: %s"),
388 context->line_number - remaining_newlines,
393 mark_error (context, tmp_error);
395 g_propagate_error (error, tmp_error);
401 USTATE_AFTER_AMPERSAND,
402 USTATE_INSIDE_ENTITY_NAME,
403 USTATE_AFTER_CHARREF_HASH
407 unescape_text (GMarkupParseContext *context,
409 const gchar *text_end,
413 #define MAX_ENT_LEN 5
419 str = g_string_new ("");
421 state = USTATE_INSIDE_TEXT;
424 while (p != text_end && context->state != STATE_ERROR)
426 g_assert (p < text_end);
430 case USTATE_INSIDE_TEXT:
432 while (p != text_end && *p != '&')
433 p = g_utf8_next_char (p);
437 g_string_append_len (str, start, p - start);
442 if (p != text_end && *p == '&')
444 p = g_utf8_next_char (p);
445 state = USTATE_AFTER_AMPERSAND;
450 case USTATE_AFTER_AMPERSAND:
454 p = g_utf8_next_char (p);
457 state = USTATE_AFTER_CHARREF_HASH;
459 else if (!is_name_start_char (g_utf8_get_char (p)))
463 set_unescape_error (context, error,
465 G_MARKUP_ERROR_PARSE,
466 _("Empty entity '&;' seen; valid "
467 "entities are: & " < > '"));
473 set_unescape_error (context, error,
475 G_MARKUP_ERROR_PARSE,
476 _("Character '%s' is not valid at "
477 "the start of an entity name; "
478 "the & character begins an entity; "
479 "if this ampersand isn't supposed "
480 "to be an entity, escape it as "
488 state = USTATE_INSIDE_ENTITY_NAME;
494 case USTATE_INSIDE_ENTITY_NAME:
496 gchar buf[MAX_ENT_LEN+1] = {
497 '\0', '\0', '\0', '\0', '\0', '\0'
501 while (p != text_end)
505 else if (!is_name_char (*p))
509 set_unescape_error (context, error,
511 G_MARKUP_ERROR_PARSE,
512 _("Character '%s' is not valid "
513 "inside an entity name"),
518 p = g_utf8_next_char (p);
521 if (context->state != STATE_ERROR)
536 /* move to after semicolon */
537 p = g_utf8_next_char (p);
539 state = USTATE_INSIDE_TEXT;
541 if (strcmp (buf, "lt") == 0)
542 g_string_append_c (str, '<');
543 else if (strcmp (buf, "gt") == 0)
544 g_string_append_c (str, '>');
545 else if (strcmp (buf, "amp") == 0)
546 g_string_append_c (str, '&');
547 else if (strcmp (buf, "quot") == 0)
548 g_string_append_c (str, '"');
549 else if (strcmp (buf, "apos") == 0)
550 g_string_append_c (str, '\'');
553 set_unescape_error (context, error,
555 G_MARKUP_ERROR_PARSE,
556 _("Entity name '%s' is not known"),
562 set_unescape_error (context, error,
563 /* give line number of the & */
565 G_MARKUP_ERROR_PARSE,
566 _("Entity did not end with a semicolon; "
567 "most likely you used an ampersand "
568 "character without intending to start "
569 "an entity - escape ampersand as &"));
575 case USTATE_AFTER_CHARREF_HASH:
577 gboolean is_hex = FALSE;
581 p = g_utf8_next_char (p);
585 while (p != text_end && *p != ';')
586 p = g_utf8_next_char (p);
590 g_assert (*p == ';');
592 /* digit is between start and p */
596 gchar *digit = g_strndup (start, p - start);
599 gchar *digit_end = digit + (p - start);
603 l = strtoul (digit, &end, 16);
605 l = strtoul (digit, &end, 10);
607 if (end != digit_end || errno != 0)
609 set_unescape_error (context, error,
611 G_MARKUP_ERROR_PARSE,
612 _("Failed to parse '%s', which "
613 "should have been a digit "
614 "inside a character reference "
615 "(ê for example) - perhaps "
616 "the digit is too large"),
621 /* characters XML permits */
625 (l >= 0x20 && l <= 0xD7FF) ||
626 (l >= 0xE000 && l <= 0xFFFD) ||
627 (l >= 0x10000 && l <= 0x10FFFF))
630 g_string_append (str,
635 set_unescape_error (context, error,
637 G_MARKUP_ERROR_PARSE,
638 _("Character reference '%s' does not encode a permitted character"),
645 /* Move to next state */
646 p = g_utf8_next_char (p); /* past semicolon */
648 state = USTATE_INSIDE_TEXT;
652 set_unescape_error (context, error,
654 G_MARKUP_ERROR_PARSE,
655 _("Empty character reference; "
656 "should include a digit such as "
662 set_unescape_error (context, error,
664 G_MARKUP_ERROR_PARSE,
665 _("Character reference did not end with a "
667 "most likely you used an ampersand "
668 "character without intending to start "
669 "an entity - escape ampersand as &"));
675 g_assert_not_reached ();
680 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
681 g_assert (context->state == STATE_ERROR ||
682 state == USTATE_INSIDE_TEXT);
684 if (context->state == STATE_ERROR)
686 g_string_free (str, TRUE);
692 *unescaped = g_string_free (str, FALSE);
700 advance_char (GMarkupParseContext *context)
703 context->iter = g_utf8_next_char (context->iter);
704 context->char_number += 1;
705 if (*context->iter == '\n')
707 context->line_number += 1;
708 context->char_number = 1;
711 return context->iter != context->current_text_end;
715 skip_spaces (GMarkupParseContext *context)
719 if (!g_unichar_isspace (g_utf8_get_char (context->iter)))
722 while (advance_char (context));
726 advance_to_name_end (GMarkupParseContext *context)
730 if (!is_name_char (g_utf8_get_char (context->iter)))
733 while (advance_char (context));
737 add_to_partial (GMarkupParseContext *context,
738 const gchar *text_start,
739 const gchar *text_end)
741 if (context->partial_chunk == NULL)
742 context->partial_chunk = g_string_new ("");
744 if (text_start != text_end)
745 g_string_append_len (context->partial_chunk, text_start,
746 text_end - text_start);
748 /* Invariant here that partial_chunk exists */
752 free_partial (GMarkupParseContext *context)
754 if (context->partial_chunk != NULL)
756 g_string_free (context->partial_chunk, TRUE);
757 context->partial_chunk = NULL;
762 current_element (GMarkupParseContext *context)
764 return context->tag_stack->data;
768 current_attribute (GMarkupParseContext *context)
770 return ((GMarkupAttribute*)context->attributes->data)->name;
774 find_current_text_end (GMarkupParseContext *context)
776 /* This function must be safe (non-segfaulting) on invalid UTF8 */
777 const gchar *end = context->current_text + context->current_text_len;
783 p = context->current_text;
784 next = g_utf8_find_next_char (p, end);
789 next = g_utf8_find_next_char (p, end);
792 /* p is now the start of the last character or character portion. */
794 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
798 /* whole character */
799 context->current_text_end = end;
804 context->leftover_char_portion = g_string_new_len (p, end - p);
805 context->current_text_len -= (end - p);
806 context->current_text_end = p;
811 * g_markup_parse_context_parse:
812 * @context: a #GMarkupParseContext
813 * @text: chunk of text to parse
814 * @text_len: length of @text in bytes
815 * @error: return location for a #GError
817 * Feed some data to the #GMarkupParseContext. The data need not
818 * be valid UTF-8; an error will be signaled if it's invalid.
819 * The data need not be an entire document; you can feed a document
820 * into the parser incrementally, via multiple calls to this function.
821 * Typically, as you receive data from a network connection or file,
822 * you feed each received chunk of data into this function, aborting
823 * the process if an error occurs. Once an error is reported, no further
824 * data may be fed to the #GMarkupParseContext; all errors are fatal.
826 * Return value: %FALSE if an error occurred, %TRUE on success
829 g_markup_parse_context_parse (GMarkupParseContext *context,
834 const gchar *first_invalid;
836 g_return_val_if_fail (context != NULL, FALSE);
837 g_return_val_if_fail (text != NULL, FALSE);
838 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
839 g_return_val_if_fail (!context->parsing, FALSE);
842 text_len = strlen (text);
847 context->parsing = TRUE;
849 if (context->leftover_char_portion)
851 const gchar *first_char;
853 if ((*text & 0xc0) != 0x80)
856 first_char = g_utf8_find_next_char (text, text + text_len);
860 /* leftover_char_portion was completed. Parse it. */
861 GString *portion = context->leftover_char_portion;
863 g_string_append_len (context->leftover_char_portion,
864 text, first_char - text);
866 /* hacks to allow recursion */
867 context->parsing = FALSE;
868 context->leftover_char_portion = NULL;
870 if (!g_markup_parse_context_parse (context,
871 portion->str, portion->len,
874 g_assert (context->state == STATE_ERROR);
877 g_string_free (portion, TRUE);
878 context->parsing = TRUE;
880 /* Skip the fraction of char that was in this text */
881 text_len -= (first_char - text);
886 /* another little chunk of the leftover char; geez
887 * someone is inefficient.
889 g_string_append_len (context->leftover_char_portion,
896 context->current_text = text;
897 context->current_text_len = text_len;
898 context->iter = context->current_text;
899 context->start = context->iter;
901 /* Nothing left after finishing the leftover char, or nothing
902 * passed in to begin with.
904 if (context->current_text_len == 0)
907 /* find_current_text_end () assumes the string starts at
908 * a character start, so we need to validate at least
909 * that much. It doesn't assume any following bytes
912 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
916 G_MARKUP_ERROR_BAD_UTF8,
917 _("Invalid UTF-8 encoded text"));
921 /* Initialize context->current_text_end, possibly adjusting
922 * current_text_len, and add any leftover char portion
924 find_current_text_end (context);
926 /* Validate UTF8 (must be done after we find the end, since
927 * we could have a trailing incomplete char)
929 if (!g_utf8_validate (context->current_text,
930 context->current_text_len,
935 p = context->current_text;
936 while (p != context->current_text_end)
943 context->line_number += newlines;
947 G_MARKUP_ERROR_BAD_UTF8,
948 _("Invalid UTF-8 encoded text"));
952 while (context->iter != context->current_text_end)
954 switch (context->state)
957 /* Possible next state: AFTER_OPEN_ANGLE */
959 g_assert (context->tag_stack == NULL);
961 /* whitespace is ignored outside of any elements */
962 skip_spaces (context);
964 if (context->iter != context->current_text_end)
966 if (*context->iter == '<')
968 /* Move after the open angle */
969 advance_char (context);
971 context->state = STATE_AFTER_OPEN_ANGLE;
973 /* this could start a passthrough */
974 context->start = context->iter;
976 /* document is now non-empty */
977 context->document_empty = FALSE;
983 G_MARKUP_ERROR_PARSE,
984 _("Document must begin with an element (e.g. <book>)"));
989 case STATE_AFTER_OPEN_ANGLE:
990 /* Possible next states: INSIDE_OPEN_TAG_NAME,
991 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
993 if (*context->iter == '?' ||
994 *context->iter == '!')
996 /* include < in the passthrough */
997 const gchar *openangle = "<";
998 add_to_partial (context, openangle, openangle + 1);
999 context->start = context->iter;
1000 context->state = STATE_INSIDE_PASSTHROUGH;
1002 else if (*context->iter == '/')
1005 advance_char (context);
1007 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
1009 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1011 context->state = STATE_INSIDE_OPEN_TAG_NAME;
1013 /* start of tag name */
1014 context->start = context->iter;
1021 G_MARKUP_ERROR_PARSE,
1022 _("'%s' is not a valid character following "
1023 "a '<' character; it may not begin an "
1025 utf8_str (context->iter, buf));
1029 /* The AFTER_CLOSE_ANGLE state is actually sort of
1030 * broken, because it doesn't correspond to a range
1031 * of characters in the input stream as the others do,
1032 * and thus makes things harder to conceptualize
1034 case STATE_AFTER_CLOSE_ANGLE:
1035 /* Possible next states: INSIDE_TEXT, STATE_START */
1036 if (context->tag_stack == NULL)
1038 context->start = NULL;
1039 context->state = STATE_START;
1043 context->start = context->iter;
1044 context->state = STATE_INSIDE_TEXT;
1048 case STATE_AFTER_ELISION_SLASH:
1049 /* Possible next state: AFTER_CLOSE_ANGLE */
1052 /* We need to pop the tag stack and call the end_element
1053 * function, since this is the close tag
1055 GError *tmp_error = NULL;
1057 g_assert (context->tag_stack != NULL);
1060 if (context->parser->end_element)
1061 (* context->parser->end_element) (context,
1062 context->tag_stack->data,
1066 g_free (context->tag_stack->data);
1067 context->tag_stack = g_slist_delete_link (context->tag_stack,
1068 context->tag_stack);
1072 mark_error (context, tmp_error);
1073 g_propagate_error (error, tmp_error);
1077 if (*context->iter == '>')
1079 /* move after the close angle */
1080 advance_char (context);
1081 context->state = STATE_AFTER_CLOSE_ANGLE;
1088 G_MARKUP_ERROR_PARSE,
1089 _("Odd character '%s', expected a '>' character "
1090 "to end the start tag of element '%s'"),
1091 utf8_str (context->iter, buf),
1092 current_element (context));
1098 case STATE_INSIDE_OPEN_TAG_NAME:
1099 /* Possible next states: BETWEEN_ATTRIBUTES */
1101 /* if there's a partial chunk then it's the first part of the
1102 * tag name. If there's a context->start then it's the start
1103 * of the tag name in current_text, the partial chunk goes
1104 * before that start though.
1106 advance_to_name_end (context);
1108 if (context->iter == context->current_text_end)
1110 /* The name hasn't necessarily ended. Merge with
1111 * partial chunk, leave state unchanged.
1113 add_to_partial (context, context->start, context->iter);
1117 /* The name has ended. Combine it with the partial chunk
1118 * if any; push it on the stack; enter next state.
1120 add_to_partial (context, context->start, context->iter);
1121 context->tag_stack =
1122 g_slist_prepend (context->tag_stack,
1123 g_string_free (context->partial_chunk,
1126 context->partial_chunk = NULL;
1128 context->state = STATE_BETWEEN_ATTRIBUTES;
1129 context->start = NULL;
1133 case STATE_INSIDE_ATTRIBUTE_NAME:
1134 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1136 /* read the full name, if we enter the equals sign state
1137 * then add the attribute to the list (without the value),
1138 * otherwise store a partial chunk to be prepended later.
1140 advance_to_name_end (context);
1142 if (context->iter == context->current_text_end)
1144 /* The name hasn't necessarily ended. Merge with
1145 * partial chunk, leave state unchanged.
1147 add_to_partial (context, context->start, context->iter);
1151 /* The name has ended. Combine it with the partial chunk
1152 * if any; push it on the stack; enter next state.
1154 GMarkupAttribute *attr;
1155 add_to_partial (context, context->start, context->iter);
1157 attr = attribute_new (NULL, NULL);
1159 attr->name = g_string_free (context->partial_chunk,
1162 context->partial_chunk = NULL;
1163 context->start = NULL;
1165 context->attributes =
1166 g_slist_prepend (context->attributes, attr);
1168 if (*context->iter == '=')
1170 advance_char (context);
1171 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1178 G_MARKUP_ERROR_PARSE,
1179 _("Odd character '%s', expected a '=' after "
1180 "attribute name '%s' of element '%s'"),
1181 utf8_str (context->iter, buf),
1183 current_element (context));
1189 case STATE_BETWEEN_ATTRIBUTES:
1190 /* Possible next states: AFTER_CLOSE_ANGLE,
1191 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1193 skip_spaces (context);
1195 if (context->iter != context->current_text_end)
1197 if (*context->iter == '/')
1199 advance_char (context);
1200 context->state = STATE_AFTER_ELISION_SLASH;
1202 else if (*context->iter == '>')
1205 advance_char (context);
1206 context->state = STATE_AFTER_CLOSE_ANGLE;
1208 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1210 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1211 /* start of attribute name */
1212 context->start = context->iter;
1219 G_MARKUP_ERROR_PARSE,
1220 _("Odd character '%s', expected a '>' or '/' "
1221 "character to end the start tag of "
1222 "element '%s', or optionally an attribute; "
1223 "perhaps you used an invalid character in "
1224 "an attribute name"),
1225 utf8_str (context->iter, buf),
1226 current_element (context));
1229 /* If we're done with attributes, invoke
1230 * the start_element callback
1232 if (context->state == STATE_AFTER_ELISION_SLASH ||
1233 context->state == STATE_AFTER_CLOSE_ANGLE)
1235 const gchar *start_name;
1236 gchar **attr_names = NULL;
1237 gchar **attr_values = NULL;
1240 /* Call user callback for element start */
1241 start_name = current_element (context);
1243 /* this gratuituously copies the attr names/values
1246 attribute_list_to_arrays (context->attributes,
1252 if (context->parser->start_element)
1253 (* context->parser->start_element) (context,
1260 g_strfreev (attr_names);
1261 g_strfreev (attr_values);
1263 /* Go ahead and free this. */
1264 g_slist_foreach (context->attributes, (GFunc)attribute_free,
1266 g_slist_free (context->attributes);
1267 context->attributes = NULL;
1269 if (tmp_error != NULL)
1271 mark_error (context, tmp_error);
1272 g_propagate_error (error, tmp_error);
1278 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1279 /* Possible next state: INSIDE_ATTRIBUTE_VALUE */
1280 if (*context->iter == '"')
1282 advance_char (context);
1283 context->state = STATE_INSIDE_ATTRIBUTE_VALUE;
1284 context->start = context->iter;
1291 G_MARKUP_ERROR_PARSE,
1292 _("Odd character '%s', expected an open quote mark "
1293 "after the equals sign when giving value for "
1294 "attribute '%s' of element '%s'"),
1295 utf8_str (context->iter, buf),
1296 current_attribute (context),
1297 current_element (context));
1301 case STATE_INSIDE_ATTRIBUTE_VALUE:
1302 /* Possible next states: BETWEEN_ATTRIBUTES */
1305 if (*context->iter == '"')
1308 while (advance_char (context));
1310 if (context->iter == context->current_text_end)
1312 /* The value hasn't necessarily ended. Merge with
1313 * partial chunk, leave state unchanged.
1315 add_to_partial (context, context->start, context->iter);
1319 /* The value has ended at the quote mark. Combine it
1320 * with the partial chunk if any; set it for the current
1323 GMarkupAttribute *attr;
1325 add_to_partial (context, context->start, context->iter);
1327 attr = context->attributes->data;
1329 if (unescape_text (context,
1330 context->partial_chunk->str,
1331 context->partial_chunk->str +
1332 context->partial_chunk->len,
1336 /* success, advance past quote and set state. */
1337 advance_char (context);
1338 context->state = STATE_BETWEEN_ATTRIBUTES;
1339 context->start = NULL;
1342 free_partial (context);
1346 case STATE_INSIDE_TEXT:
1347 /* Possible next states: AFTER_OPEN_ANGLE */
1350 if (*context->iter == '<')
1353 while (advance_char (context));
1355 /* The text hasn't necessarily ended. Merge with
1356 * partial chunk, leave state unchanged.
1359 add_to_partial (context, context->start, context->iter);
1361 if (context->iter != context->current_text_end)
1363 gchar *unescaped = NULL;
1365 /* The text has ended at the open angle. Call the text
1369 if (unescape_text (context,
1370 context->partial_chunk->str,
1371 context->partial_chunk->str +
1372 context->partial_chunk->len,
1376 GError *tmp_error = NULL;
1378 if (context->parser->text)
1379 (*context->parser->text) (context,
1387 if (tmp_error == NULL)
1389 /* advance past open angle and set state. */
1390 advance_char (context);
1391 context->state = STATE_AFTER_OPEN_ANGLE;
1392 /* could begin a passthrough */
1393 context->start = context->iter;
1397 mark_error (context, tmp_error);
1398 g_propagate_error (error, tmp_error);
1402 free_partial (context);
1406 case STATE_AFTER_CLOSE_TAG_SLASH:
1407 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1408 if (is_name_start_char (g_utf8_get_char (context->iter)))
1410 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1412 /* start of tag name */
1413 context->start = context->iter;
1420 G_MARKUP_ERROR_PARSE,
1421 _("'%s' is not a valid character following "
1422 "the characters '</'; '%s' may not begin an "
1424 utf8_str (context->iter, buf),
1425 utf8_str (context->iter, buf));
1429 case STATE_INSIDE_CLOSE_TAG_NAME:
1430 /* Possible next state: AFTER_CLOSE_ANGLE */
1431 advance_to_name_end (context);
1433 if (context->iter == context->current_text_end)
1435 /* The name hasn't necessarily ended. Merge with
1436 * partial chunk, leave state unchanged.
1438 add_to_partial (context, context->start, context->iter);
1442 /* The name has ended. Combine it with the partial chunk
1443 * if any; check that it matches stack top and pop
1444 * stack; invoke proper callback; enter next state.
1448 add_to_partial (context, context->start, context->iter);
1450 close_name = g_string_free (context->partial_chunk, FALSE);
1451 context->partial_chunk = NULL;
1453 if (context->tag_stack == NULL)
1457 G_MARKUP_ERROR_PARSE,
1458 _("Element '%s' was closed, no element "
1459 "is currently open"),
1462 else if (strcmp (close_name, current_element (context)) != 0)
1466 G_MARKUP_ERROR_PARSE,
1467 _("Element '%s' was closed, but the currently "
1468 "open element is '%s'"),
1470 current_element (context));
1472 else if (*context->iter != '>')
1477 G_MARKUP_ERROR_PARSE,
1478 _("'%s' is not a valid character following "
1479 "the close element name '%s'; the allowed "
1480 "character is '>'"),
1481 utf8_str (context->iter, buf),
1487 advance_char (context);
1488 context->state = STATE_AFTER_CLOSE_ANGLE;
1489 context->start = NULL;
1491 /* call the end_element callback */
1493 if (context->parser->end_element)
1494 (* context->parser->end_element) (context,
1500 /* Pop the tag stack */
1501 g_free (context->tag_stack->data);
1502 context->tag_stack = g_slist_delete_link (context->tag_stack,
1503 context->tag_stack);
1507 mark_error (context, tmp_error);
1508 g_propagate_error (error, tmp_error);
1512 g_free (close_name);
1516 case STATE_INSIDE_PASSTHROUGH:
1517 /* Possible next state: AFTER_CLOSE_ANGLE */
1520 if (*context->iter == '>')
1523 while (advance_char (context));
1525 if (context->iter == context->current_text_end)
1527 /* The passthrough hasn't necessarily ended. Merge with
1528 * partial chunk, leave state unchanged.
1530 add_to_partial (context, context->start, context->iter);
1534 /* The passthrough has ended at the close angle. Combine
1535 * it with the partial chunk if any. Call the passthrough
1536 * callback. Note that the open/close angles are
1537 * included in the text of the passthrough.
1539 GError *tmp_error = NULL;
1541 advance_char (context); /* advance past close angle */
1542 add_to_partial (context, context->start, context->iter);
1544 if (context->parser->passthrough)
1545 (*context->parser->passthrough) (context,
1546 context->partial_chunk->str,
1547 context->partial_chunk->len,
1551 free_partial (context);
1553 if (tmp_error == NULL)
1555 context->state = STATE_AFTER_CLOSE_ANGLE;
1556 context->start = context->iter; /* could begin text */
1560 mark_error (context, tmp_error);
1561 g_propagate_error (error, tmp_error);
1571 g_assert_not_reached ();
1577 context->parsing = FALSE;
1579 return context->state != STATE_ERROR;
1583 * g_markup_parse_context_end_parse:
1584 * @context: a #GMarkupParseContext
1585 * @error: return location for a #GError
1587 * Signals to the #GMarkupParseContext that all data has been
1588 * fed into the parse context with g_markup_parse_context_parse().
1589 * This function reports an error if the document isn't complete,
1590 * for example if elements are still open.
1592 * Return value: %TRUE on success, %FALSE if an error was set
1595 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1598 g_return_val_if_fail (context != NULL, FALSE);
1599 g_return_val_if_fail (!context->parsing, FALSE);
1600 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1602 if (context->document_empty)
1604 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1605 _("Document was empty or contained only whitespace"));
1609 context->parsing = TRUE;
1611 switch (context->state)
1617 case STATE_AFTER_OPEN_ANGLE:
1618 set_error (context, error, G_MARKUP_ERROR_PARSE,
1619 _("Document ended unexpectedly just after an open angle bracket '<'"));
1622 case STATE_AFTER_CLOSE_ANGLE:
1623 if (context->tag_stack != NULL)
1625 /* Error message the same as for INSIDE_TEXT */
1626 set_error (context, error, G_MARKUP_ERROR_PARSE,
1627 _("Document ended unexpectedly with elements still open - "
1628 "'%s' was the last element opened"),
1629 current_element (context));
1633 case STATE_AFTER_ELISION_SLASH:
1634 set_error (context, error, G_MARKUP_ERROR_PARSE,
1635 _("Document ended unexpectedly, expected to see a close angle "
1636 "bracket ending the tag <%s/>"), current_element (context));
1639 case STATE_INSIDE_OPEN_TAG_NAME:
1640 set_error (context, error, G_MARKUP_ERROR_PARSE,
1641 _("Document ended unexpectedly inside an element name"));
1644 case STATE_INSIDE_ATTRIBUTE_NAME:
1645 set_error (context, error, G_MARKUP_ERROR_PARSE,
1646 _("Document ended unexpectedly inside an attribute name"));
1649 case STATE_BETWEEN_ATTRIBUTES:
1650 set_error (context, error, G_MARKUP_ERROR_PARSE,
1651 _("Document ended unexpectedly inside an element-opening "
1655 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1656 set_error (context, error, G_MARKUP_ERROR_PARSE,
1657 _("Document ended unexpectedly after the equals sign "
1658 "following an attribute name; no attribute value"));
1661 case STATE_INSIDE_ATTRIBUTE_VALUE:
1662 set_error (context, error, G_MARKUP_ERROR_PARSE,
1663 _("Document ended unexpectedly while inside an attribute "
1667 case STATE_INSIDE_TEXT:
1668 g_assert (context->tag_stack != NULL);
1669 set_error (context, error, G_MARKUP_ERROR_PARSE,
1670 _("Document ended unexpectedly with elements still open - "
1671 "'%s' was the last element opened"),
1672 current_element (context));
1675 case STATE_AFTER_CLOSE_TAG_SLASH:
1676 case STATE_INSIDE_CLOSE_TAG_NAME:
1677 set_error (context, error, G_MARKUP_ERROR_PARSE,
1678 _("Document ended unexpectedly inside the close tag for"
1679 "element '%s'"), current_element);
1682 case STATE_INSIDE_PASSTHROUGH:
1683 set_error (context, error, G_MARKUP_ERROR_PARSE,
1684 _("Document ended unexpectedly inside a comment or "
1685 "processing instruction"));
1690 g_assert_not_reached ();
1694 context->parsing = FALSE;
1696 return context->state != STATE_ERROR;
1700 * g_markup_parse_context_get_position:
1701 * @context: a #GMarkupParseContext
1702 * @line_number: return location for a line number, or %NULL
1703 * @char_number: return location for a char-on-line number, or %NULL
1705 * Returns the current line number and the number of the character on
1706 * that line. Intended for use in error messages; there are no strict
1707 * semantics for what constitutes the "current" line number other than
1708 * "the best number we could come up with for error messages."
1712 g_markup_parse_context_get_position (GMarkupParseContext *context,
1716 g_return_if_fail (context != NULL);
1719 *line_number = context->line_number;
1722 *char_number = context->char_number;
1726 append_escaped_text (GString *str,
1734 end = text + length;
1739 next = g_utf8_next_char (p);
1744 g_string_append (str, "&");
1748 g_string_append (str, "<");
1752 g_string_append (str, ">");
1756 g_string_append (str, "'");
1760 g_string_append (str, """);
1764 g_string_append_len (str, p, next - p);
1773 * g_markup_escape_text:
1774 * @text: some valid UTF-8 text
1775 * @length: length of @text in bytes
1777 * Escapes text so that the markup parser will parse it verbatim.
1778 * Less than, greater than, ampersand, etc. are replaced with the
1779 * corresponding entities. This function would typically be used
1780 * when writing out a file to be parsed with the markup parser.
1782 * Return value: escaped text
1785 g_markup_escape_text (const gchar *text,
1790 g_return_val_if_fail (text != NULL, NULL);
1793 length = strlen (text);
1795 str = g_string_new ("");
1796 append_escaped_text (str, text, length);
1798 return g_string_free (str, FALSE);