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;
127 GMarkupParseContext
*
128 g_markup_parse_context_new (const GMarkupParser
*parser
,
129 GMarkupParseFlags flags
,
131 GDestroyNotify user_data_dnotify
)
133 GMarkupParseContext
*context
;
135 g_return_val_if_fail (parser
!= NULL
, NULL
);
137 context
= g_new (GMarkupParseContext
, 1);
139 context
->parser
= parser
;
140 context
->flags
= flags
;
141 context
->user_data
= user_data
;
142 context
->dnotify
= user_data_dnotify
;
144 context
->line_number
= 1;
145 context
->char_number
= 1;
147 context
->partial_chunk
= NULL
;
149 context
->state
= STATE_START
;
150 context
->tag_stack
= NULL
;
151 context
->attributes
= NULL
;
153 context
->current_text
= NULL
;
154 context
->current_text_len
= -1;
155 context
->current_text_end
= NULL
;
156 context
->leftover_char_portion
= NULL
;
158 context
->start
= NULL
;
159 context
->iter
= NULL
;
161 context
->document_empty
= TRUE
;
162 context
->parsing
= FALSE
;
168 g_markup_parse_context_free (GMarkupParseContext
*context
)
170 g_return_if_fail (context
!= NULL
);
171 g_return_if_fail (!context
->parsing
);
173 if (context
->dnotify
)
174 (* context
->dnotify
) (context
->user_data
);
176 g_slist_foreach (context
->attributes
, (GFunc
)attribute_free
, NULL
);
177 g_slist_free (context
->attributes
);
179 g_slist_foreach (context
->tag_stack
, (GFunc
)g_free
, NULL
);
180 g_slist_free (context
->tag_stack
);
182 if (context
->partial_chunk
)
183 g_string_free (context
->partial_chunk
, TRUE
);
185 if (context
->leftover_char_portion
)
186 g_string_free (context
->leftover_char_portion
, TRUE
);
192 attribute_list_to_arrays (GSList
*attributes
,
203 len
= g_slist_length (attributes
);
207 names
= g_new (gchar
*, len
+ 1);
215 values
= g_new (gchar
*, len
+ 1);
221 /* We want to reverse the list, since it's
222 * backward from the order the attributes appeared
226 tmp_list
= attributes
;
229 GMarkupAttribute
*attr
= tmp_list
->data
;
234 names
[i
] = g_strdup (attr
->name
);
237 values
[i
] = g_strdup (attr
->value
);
239 tmp_list
= g_slist_next (tmp_list
);
254 mark_error (GMarkupParseContext
*context
,
257 context
->state
= STATE_ERROR
;
259 if (context
->parser
->error
)
260 (*context
->parser
->error
) (context
, error
, context
->user_data
);
264 set_error (GMarkupParseContext
*context
,
266 GMarkupErrorType code
,
274 va_start (args
, format
);
275 s
= g_strdup_vprintf (format
, args
);
278 tmp_error
= g_error_new (G_MARKUP_ERROR
,
280 _("Error on line %d char %d: %s"),
281 context
->line_number
,
282 context
->char_number
,
287 mark_error (context
, tmp_error
);
289 g_propagate_error (error
, tmp_error
);
293 is_name_start_char (gunichar c
)
295 if (g_unichar_isalpha (c
) ||
304 is_name_char (gunichar c
)
306 if (g_unichar_isalnum (c
) ||
318 char_str (gunichar c
,
322 g_unichar_to_utf8 (c
, buf
);
327 utf8_str (const gchar
*utf8
,
330 char_str (g_utf8_get_char (utf8
), buf
);
335 set_unescape_error (GMarkupParseContext
*context
,
337 const gchar
*remaining_text
,
338 const gchar
*remaining_text_end
,
339 GMarkupErrorType code
,
346 gint remaining_newlines
;
349 remaining_newlines
= 0;
351 while (p
!= remaining_text_end
)
354 ++remaining_newlines
;
358 va_start (args
, format
);
359 s
= g_strdup_vprintf (format
, args
);
362 tmp_error
= g_error_new (G_MARKUP_ERROR
,
364 _("Error on line %d: %s"),
365 context
->line_number
- remaining_newlines
,
370 mark_error (context
, tmp_error
);
372 g_propagate_error (error
, tmp_error
);
378 USTATE_AFTER_AMPERSAND
,
379 USTATE_INSIDE_ENTITY_NAME
,
380 USTATE_AFTER_CHARREF_HASH
384 unescape_text (GMarkupParseContext
*context
,
386 const gchar
*text_end
,
390 #define MAX_ENT_LEN 5
396 str
= g_string_new ("");
398 state
= USTATE_INSIDE_TEXT
;
401 while (p
!= text_end
&& context
->state
!= STATE_ERROR
)
403 g_assert (p
< text_end
);
407 case USTATE_INSIDE_TEXT
:
409 while (p
!= text_end
&& *p
!= '&')
410 p
= g_utf8_next_char (p
);
414 g_string_append_len (str
, start
, p
- start
);
419 if (p
!= text_end
&& *p
== '&')
421 p
= g_utf8_next_char (p
);
422 state
= USTATE_AFTER_AMPERSAND
;
427 case USTATE_AFTER_AMPERSAND
:
431 p
= g_utf8_next_char (p
);
434 state
= USTATE_AFTER_CHARREF_HASH
;
436 else if (!is_name_start_char (g_utf8_get_char (p
)))
440 set_unescape_error (context
, error
,
442 G_MARKUP_ERROR_PARSE
,
443 _("Empty entity '&;' seen; valid "
444 "entities are: & " < > '"));
450 set_unescape_error (context
, error
,
452 G_MARKUP_ERROR_PARSE
,
453 _("Character '%s' is not valid at "
454 "the start of an entity name; "
455 "the & character begins an entity; "
456 "if this ampersand isn't supposed "
457 "to be an entity, escape it as "
465 state
= USTATE_INSIDE_ENTITY_NAME
;
471 case USTATE_INSIDE_ENTITY_NAME
:
473 gchar buf
[MAX_ENT_LEN
+1] = {
474 '\0', '\0', '\0', '\0', '\0', '\0'
478 while (p
!= text_end
)
482 else if (!is_name_char (*p
))
486 set_unescape_error (context
, error
,
488 G_MARKUP_ERROR_PARSE
,
489 _("Character '%s' is not valid "
490 "inside an entity name"),
495 p
= g_utf8_next_char (p
);
498 if (context
->state
!= STATE_ERROR
)
513 /* move to after semicolon */
514 p
= g_utf8_next_char (p
);
516 state
= USTATE_INSIDE_TEXT
;
518 if (strcmp (buf
, "lt") == 0)
519 g_string_append_c (str
, '<');
520 else if (strcmp (buf
, "gt") == 0)
521 g_string_append_c (str
, '>');
522 else if (strcmp (buf
, "amp") == 0)
523 g_string_append_c (str
, '&');
524 else if (strcmp (buf
, "quot") == 0)
525 g_string_append_c (str
, '"');
526 else if (strcmp (buf
, "apos") == 0)
527 g_string_append_c (str
, '\'');
530 set_unescape_error (context
, error
,
532 G_MARKUP_ERROR_PARSE
,
533 _("Entity name '%s' is not known"),
539 set_unescape_error (context
, error
,
540 /* give line number of the & */
542 G_MARKUP_ERROR_PARSE
,
543 _("Entity did not end with a semicolon; "
544 "most likely you used an ampersand "
545 "character without intending to start "
546 "an entity - escape ampersand as &"));
552 case USTATE_AFTER_CHARREF_HASH
:
554 gboolean is_hex
= FALSE
;
558 p
= g_utf8_next_char (p
);
562 while (p
!= text_end
&& *p
!= ';')
563 p
= g_utf8_next_char (p
);
567 g_assert (*p
== ';');
569 /* digit is between start and p */
573 gchar
*digit
= g_strndup (start
, p
- start
);
579 l
= strtoul (digit
, &end
, 16);
581 l
= strtoul (digit
, &end
, 10);
585 set_unescape_error (context
, error
,
587 G_MARKUP_ERROR_PARSE
,
588 _("Failed to parse '%s', which "
589 "should have been a digit "
590 "inside a character reference "
591 "(ê for example) - perhaps "
592 "the digit is too large"),
597 /* characters XML permits */
601 (l
>= 0x20 && l
<= 0xD7FF) ||
602 (l
>= 0xE000 && l
<= 0xFFFD) ||
603 (l
>= 0x10000 && l
<= 0x10FFFF))
606 g_string_append (str
,
611 set_unescape_error (context
, error
,
613 G_MARKUP_ERROR_PARSE
,
614 _("Character reference '%s' does not encode a permitted character"),
621 /* Move to next state */
622 p
= g_utf8_next_char (p
); /* past semicolon */
624 state
= USTATE_INSIDE_TEXT
;
628 set_unescape_error (context
, error
,
630 G_MARKUP_ERROR_PARSE
,
631 _("Empty character reference; "
632 "should include a digit such as "
638 set_unescape_error (context
, error
,
640 G_MARKUP_ERROR_PARSE
,
641 _("Character reference did not end with a "
643 "most likely you used an ampersand "
644 "character without intending to start "
645 "an entity - escape ampersand as &"));
651 g_assert_not_reached ();
656 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
657 g_assert (context
->state
== STATE_ERROR
||
658 state
== USTATE_INSIDE_TEXT
);
660 if (context
->state
== STATE_ERROR
)
662 g_string_free (str
, TRUE
);
668 *unescaped
= g_string_free (str
, FALSE
);
676 advance_char (GMarkupParseContext
*context
)
679 context
->iter
= g_utf8_next_char (context
->iter
);
680 context
->char_number
+= 1;
681 if (*context
->iter
== '\n')
683 context
->line_number
+= 1;
684 context
->char_number
= 1;
687 return context
->iter
!= context
->current_text_end
;
691 skip_spaces (GMarkupParseContext
*context
)
695 if (!g_unichar_isspace (g_utf8_get_char (context
->iter
)))
698 while (advance_char (context
));
702 advance_to_name_end (GMarkupParseContext
*context
)
706 if (!is_name_char (g_utf8_get_char (context
->iter
)))
709 while (advance_char (context
));
713 add_to_partial (GMarkupParseContext
*context
,
714 const gchar
*text_start
,
715 const gchar
*text_end
)
717 if (context
->partial_chunk
== NULL
)
718 context
->partial_chunk
= g_string_new ("");
720 if (text_start
!= text_end
)
721 g_string_append_len (context
->partial_chunk
, text_start
,
722 text_end
- text_start
);
724 /* Invariant here that partial_chunk exists */
728 free_partial (GMarkupParseContext
*context
)
730 if (context
->partial_chunk
!= NULL
)
732 g_string_free (context
->partial_chunk
, TRUE
);
733 context
->partial_chunk
= NULL
;
738 current_element (GMarkupParseContext
*context
)
740 return context
->tag_stack
->data
;
744 current_attribute (GMarkupParseContext
*context
)
746 return ((GMarkupAttribute
*)context
->attributes
->data
)->name
;
750 find_current_text_end (GMarkupParseContext
*context
)
752 /* This function must be safe (non-segfaulting) on invalid UTF8 */
753 const gchar
*end
= context
->current_text
+ context
->current_text_len
;
759 p
= context
->current_text
;
760 next
= g_utf8_find_next_char (p
, end
);
765 next
= g_utf8_find_next_char (p
, end
);
768 /* p is now the start of the last character or character portion. */
770 next
= g_utf8_next_char (p
); /* this only touches *p, nothing beyond */
774 /* whole character */
775 context
->current_text_end
= end
;
780 context
->leftover_char_portion
= g_string_new_len (p
, end
- p
);
781 context
->current_text_len
-= (end
- p
);
782 context
->current_text_end
= p
;
787 g_markup_parse_context_parse (GMarkupParseContext
*context
,
792 const gchar
*first_invalid
;
794 g_return_val_if_fail (context
!= NULL
, FALSE
);
795 g_return_val_if_fail (text
!= NULL
, FALSE
);
796 g_return_val_if_fail (context
->state
!= STATE_ERROR
, FALSE
);
797 g_return_val_if_fail (!context
->parsing
, FALSE
);
800 text_len
= strlen (text
);
805 context
->parsing
= TRUE
;
807 if (context
->leftover_char_portion
)
809 const gchar
*first_char
;
811 if ((*text
& 0xc0) != 0x80)
814 first_char
= g_utf8_find_next_char (text
, text
+ text_len
);
818 /* leftover_char_portion was completed. Parse it. */
819 GString
*portion
= context
->leftover_char_portion
;
821 g_string_append_len (context
->leftover_char_portion
,
822 text
, first_char
- text
);
824 /* hacks to allow recursion */
825 context
->parsing
= FALSE
;
826 context
->leftover_char_portion
= NULL
;
828 if (!g_markup_parse_context_parse (context
,
829 portion
->str
, portion
->len
,
832 g_assert (context
->state
== STATE_ERROR
);
835 g_string_free (portion
, TRUE
);
836 context
->parsing
= TRUE
;
838 /* Skip the fraction of char that was in this text */
839 text_len
-= (first_char
- text
);
844 /* another little chunk of the leftover char; geez
845 * someone is inefficient.
847 g_string_append_len (context
->leftover_char_portion
,
854 context
->current_text
= text
;
855 context
->current_text_len
= text_len
;
856 context
->iter
= context
->current_text
;
857 context
->start
= context
->iter
;
859 /* Nothing left after finishing the leftover char, or nothing
860 * passed in to begin with.
862 if (context
->current_text_len
== 0)
865 /* find_current_text_end () assumes the string starts at
866 * a character start, so we need to validate at least
867 * that much. It doesn't assume any following bytes
870 if ((*context
->current_text
& 0xc0) == 0x80) /* not a char start */
874 G_MARKUP_ERROR_BAD_UTF8
,
875 _("Invalid UTF-8 encoded text"));
879 /* Initialize context->current_text_end, possibly adjusting
880 * current_text_len, and add any leftover char portion
882 find_current_text_end (context
);
884 /* Validate UTF8 (must be done after we find the end, since
885 * we could have a trailing incomplete char)
887 if (!g_utf8_validate (context
->current_text
,
888 context
->current_text_len
,
893 p
= context
->current_text
;
894 while (p
!= context
->current_text_end
)
901 context
->line_number
+= newlines
;
905 G_MARKUP_ERROR_BAD_UTF8
,
906 _("Invalid UTF-8 encoded text"));
910 while (context
->iter
!= context
->current_text_end
)
912 switch (context
->state
)
915 /* Possible next state: AFTER_OPEN_ANGLE */
917 g_assert (context
->tag_stack
== NULL
);
919 /* whitespace is ignored outside of any elements */
920 skip_spaces (context
);
922 if (context
->iter
!= context
->current_text_end
)
924 if (*context
->iter
== '<')
926 /* Move after the open angle */
927 advance_char (context
);
929 context
->state
= STATE_AFTER_OPEN_ANGLE
;
931 /* this could start a passthrough */
932 context
->start
= context
->iter
;
934 /* document is now non-empty */
935 context
->document_empty
= FALSE
;
941 G_MARKUP_ERROR_PARSE
,
942 _("Document must begin with an element (e.g. <book>)"));
947 case STATE_AFTER_OPEN_ANGLE
:
948 /* Possible next states: INSIDE_OPEN_TAG_NAME,
949 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
951 if (*context
->iter
== '?' ||
952 *context
->iter
== '!')
954 /* include < in the passthrough */
955 const gchar
*openangle
= "<";
956 add_to_partial (context
, openangle
, openangle
+ 1);
957 context
->start
= context
->iter
;
958 context
->state
= STATE_INSIDE_PASSTHROUGH
;
960 else if (*context
->iter
== '/')
963 advance_char (context
);
965 context
->state
= STATE_AFTER_CLOSE_TAG_SLASH
;
967 else if (is_name_start_char (g_utf8_get_char (context
->iter
)))
969 context
->state
= STATE_INSIDE_OPEN_TAG_NAME
;
971 /* start of tag name */
972 context
->start
= context
->iter
;
979 G_MARKUP_ERROR_PARSE
,
980 _("'%s' is not a valid character following "
981 "a '<' character; it may not begin an "
983 utf8_str (context
->iter
, buf
));
987 /* The AFTER_CLOSE_ANGLE state is actually sort of
988 * broken, because it doesn't correspond to a range
989 * of characters in the input stream as the others do,
990 * and thus makes things harder to conceptualize
992 case STATE_AFTER_CLOSE_ANGLE
:
993 /* Possible next states: INSIDE_TEXT, STATE_START */
994 if (context
->tag_stack
== NULL
)
996 context
->start
= NULL
;
997 context
->state
= STATE_START
;
1001 context
->start
= context
->iter
;
1002 context
->state
= STATE_INSIDE_TEXT
;
1006 case STATE_AFTER_ELISION_SLASH
:
1007 /* Possible next state: AFTER_CLOSE_ANGLE */
1010 /* We need to pop the tag stack and call the end_element
1011 * function, since this is the close tag
1013 GError
*tmp_error
= NULL
;
1015 g_assert (context
->tag_stack
!= NULL
);
1018 if (context
->parser
->end_element
)
1019 (* context
->parser
->end_element
) (context
,
1020 context
->tag_stack
->data
,
1024 g_free (context
->tag_stack
->data
);
1025 context
->tag_stack
= g_slist_delete_link (context
->tag_stack
,
1026 context
->tag_stack
);
1030 mark_error (context
, tmp_error
);
1031 g_propagate_error (error
, tmp_error
);
1035 if (*context
->iter
== '>')
1037 /* move after the close angle */
1038 advance_char (context
);
1039 context
->state
= STATE_AFTER_CLOSE_ANGLE
;
1046 G_MARKUP_ERROR_PARSE
,
1047 _("Odd character '%s', expected a '>' character "
1048 "to end the start tag of element '%s'"),
1049 utf8_str (context
->iter
, buf
),
1050 current_element (context
));
1056 case STATE_INSIDE_OPEN_TAG_NAME
:
1057 /* Possible next states: BETWEEN_ATTRIBUTES */
1059 /* if there's a partial chunk then it's the first part of the
1060 * tag name. If there's a context->start then it's the start
1061 * of the tag name in current_text, the partial chunk goes
1062 * before that start though.
1064 advance_to_name_end (context
);
1066 if (context
->iter
== context
->current_text_end
)
1068 /* The name hasn't necessarily ended. Merge with
1069 * partial chunk, leave state unchanged.
1071 add_to_partial (context
, context
->start
, context
->iter
);
1075 /* The name has ended. Combine it with the partial chunk
1076 * if any; push it on the stack; enter next state.
1078 add_to_partial (context
, context
->start
, context
->iter
);
1079 context
->tag_stack
=
1080 g_slist_prepend (context
->tag_stack
,
1081 g_string_free (context
->partial_chunk
,
1084 context
->partial_chunk
= NULL
;
1086 context
->state
= STATE_BETWEEN_ATTRIBUTES
;
1087 context
->start
= NULL
;
1091 case STATE_INSIDE_ATTRIBUTE_NAME
:
1092 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1094 /* read the full name, if we enter the equals sign state
1095 * then add the attribute to the list (without the value),
1096 * otherwise store a partial chunk to be prepended later.
1098 advance_to_name_end (context
);
1100 if (context
->iter
== context
->current_text_end
)
1102 /* The name hasn't necessarily ended. Merge with
1103 * partial chunk, leave state unchanged.
1105 add_to_partial (context
, context
->start
, context
->iter
);
1109 /* The name has ended. Combine it with the partial chunk
1110 * if any; push it on the stack; enter next state.
1112 GMarkupAttribute
*attr
;
1113 add_to_partial (context
, context
->start
, context
->iter
);
1115 attr
= attribute_new (NULL
, NULL
);
1117 attr
->name
= g_string_free (context
->partial_chunk
,
1120 context
->partial_chunk
= NULL
;
1121 context
->start
= NULL
;
1123 context
->attributes
=
1124 g_slist_prepend (context
->attributes
, attr
);
1126 if (*context
->iter
== '=')
1128 advance_char (context
);
1129 context
->state
= STATE_AFTER_ATTRIBUTE_EQUALS_SIGN
;
1136 G_MARKUP_ERROR_PARSE
,
1137 _("Odd character '%s', expected a '=' after "
1138 "attribute name '%s' of element '%s'"),
1139 utf8_str (context
->iter
, buf
),
1141 current_element (context
));
1147 case STATE_BETWEEN_ATTRIBUTES
:
1148 /* Possible next states: AFTER_CLOSE_ANGLE,
1149 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1151 skip_spaces (context
);
1153 if (context
->iter
!= context
->current_text_end
)
1155 if (*context
->iter
== '/')
1157 advance_char (context
);
1158 context
->state
= STATE_AFTER_ELISION_SLASH
;
1160 else if (*context
->iter
== '>')
1163 advance_char (context
);
1164 context
->state
= STATE_AFTER_CLOSE_ANGLE
;
1166 else if (is_name_start_char (g_utf8_get_char (context
->iter
)))
1168 context
->state
= STATE_INSIDE_ATTRIBUTE_NAME
;
1169 /* start of attribute name */
1170 context
->start
= context
->iter
;
1177 G_MARKUP_ERROR_PARSE
,
1178 _("Odd character '%s', expected a '>' or '/' "
1179 "character to end the start tag of "
1180 "element '%s', or optionally an attribute; "
1181 "perhaps you used an invalid character in "
1182 "an attribute name"),
1183 utf8_str (context
->iter
, buf
),
1184 current_element (context
));
1187 /* If we're done with attributes, invoke
1188 * the start_element callback
1190 if (context
->state
== STATE_AFTER_ELISION_SLASH
||
1191 context
->state
== STATE_AFTER_CLOSE_ANGLE
)
1193 const gchar
*start_name
;
1194 gchar
**attr_names
= NULL
;
1195 gchar
**attr_values
= NULL
;
1198 /* Call user callback for element start */
1199 start_name
= current_element (context
);
1201 /* this gratuituously copies the attr names/values
1204 attribute_list_to_arrays (context
->attributes
,
1210 if (context
->parser
->start_element
)
1211 (* context
->parser
->start_element
) (context
,
1218 g_strfreev (attr_names
);
1219 g_strfreev (attr_values
);
1221 /* Go ahead and free this. */
1222 g_slist_foreach (context
->attributes
, (GFunc
)attribute_free
,
1224 g_slist_free (context
->attributes
);
1225 context
->attributes
= NULL
;
1227 if (tmp_error
!= NULL
)
1229 mark_error (context
, tmp_error
);
1230 g_propagate_error (error
, tmp_error
);
1236 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN
:
1237 /* Possible next state: INSIDE_ATTRIBUTE_VALUE */
1238 if (*context
->iter
== '"')
1240 advance_char (context
);
1241 context
->state
= STATE_INSIDE_ATTRIBUTE_VALUE
;
1242 context
->start
= context
->iter
;
1249 G_MARKUP_ERROR_PARSE
,
1250 _("Odd character '%s', expected an open quote mark "
1251 "after the equals sign when giving value for "
1252 "attribute '%s' of element '%s'"),
1253 utf8_str (context
->iter
, buf
),
1254 current_attribute (context
),
1255 current_element (context
));
1259 case STATE_INSIDE_ATTRIBUTE_VALUE
:
1260 /* Possible next states: BETWEEN_ATTRIBUTES */
1263 if (*context
->iter
== '"')
1266 while (advance_char (context
));
1268 if (context
->iter
== context
->current_text_end
)
1270 /* The value hasn't necessarily ended. Merge with
1271 * partial chunk, leave state unchanged.
1273 add_to_partial (context
, context
->start
, context
->iter
);
1277 /* The value has ended at the quote mark. Combine it
1278 * with the partial chunk if any; set it for the current
1281 GMarkupAttribute
*attr
;
1283 add_to_partial (context
, context
->start
, context
->iter
);
1285 attr
= context
->attributes
->data
;
1287 if (unescape_text (context
,
1288 context
->partial_chunk
->str
,
1289 context
->partial_chunk
->str
+
1290 context
->partial_chunk
->len
,
1294 /* success, advance past quote and set state. */
1295 advance_char (context
);
1296 context
->state
= STATE_BETWEEN_ATTRIBUTES
;
1297 context
->start
= NULL
;
1300 free_partial (context
);
1304 case STATE_INSIDE_TEXT
:
1305 /* Possible next states: AFTER_OPEN_ANGLE */
1308 if (*context
->iter
== '<')
1311 while (advance_char (context
));
1313 /* The text hasn't necessarily ended. Merge with
1314 * partial chunk, leave state unchanged.
1317 add_to_partial (context
, context
->start
, context
->iter
);
1319 if (context
->iter
!= context
->current_text_end
)
1321 gchar
*unescaped
= NULL
;
1323 /* The text has ended at the open angle. Call the text
1327 if (unescape_text (context
,
1328 context
->partial_chunk
->str
,
1329 context
->partial_chunk
->str
+
1330 context
->partial_chunk
->len
,
1334 GError
*tmp_error
= NULL
;
1336 if (context
->parser
->text
)
1337 (*context
->parser
->text
) (context
,
1345 if (tmp_error
== NULL
)
1347 /* advance past open angle and set state. */
1348 advance_char (context
);
1349 context
->state
= STATE_AFTER_OPEN_ANGLE
;
1350 /* could begin a passthrough */
1351 context
->start
= context
->iter
;
1355 mark_error (context
, tmp_error
);
1356 g_propagate_error (error
, tmp_error
);
1360 free_partial (context
);
1364 case STATE_AFTER_CLOSE_TAG_SLASH
:
1365 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1366 if (is_name_start_char (g_utf8_get_char (context
->iter
)))
1368 context
->state
= STATE_INSIDE_CLOSE_TAG_NAME
;
1370 /* start of tag name */
1371 context
->start
= context
->iter
;
1378 G_MARKUP_ERROR_PARSE
,
1379 _("'%s' is not a valid character following "
1380 "the characters '</'; '%s' may not begin an "
1382 utf8_str (context
->iter
, buf
),
1383 utf8_str (context
->iter
, buf
));
1387 case STATE_INSIDE_CLOSE_TAG_NAME
:
1388 /* Possible next state: AFTER_CLOSE_ANGLE */
1389 advance_to_name_end (context
);
1391 if (context
->iter
== context
->current_text_end
)
1393 /* The name hasn't necessarily ended. Merge with
1394 * partial chunk, leave state unchanged.
1396 add_to_partial (context
, context
->start
, context
->iter
);
1400 /* The name has ended. Combine it with the partial chunk
1401 * if any; check that it matches stack top and pop
1402 * stack; invoke proper callback; enter next state.
1406 add_to_partial (context
, context
->start
, context
->iter
);
1408 close_name
= g_string_free (context
->partial_chunk
, FALSE
);
1409 context
->partial_chunk
= NULL
;
1411 if (context
->tag_stack
== NULL
)
1415 G_MARKUP_ERROR_PARSE
,
1416 _("Element '%s' was closed, no element "
1417 "is currently open"),
1420 else if (strcmp (close_name
, current_element (context
)) != 0)
1424 G_MARKUP_ERROR_PARSE
,
1425 _("Element '%s' was closed, but the currently "
1426 "open element is '%s'"),
1428 current_element (context
));
1430 else if (*context
->iter
!= '>')
1435 G_MARKUP_ERROR_PARSE
,
1436 _("'%s' is not a valid character following "
1437 "the close element name '%s'; the allowed "
1438 "character is '>'"),
1439 utf8_str (context
->iter
, buf
),
1445 advance_char (context
);
1446 context
->state
= STATE_AFTER_CLOSE_ANGLE
;
1447 context
->start
= NULL
;
1449 /* call the end_element callback */
1451 if (context
->parser
->end_element
)
1452 (* context
->parser
->end_element
) (context
,
1458 /* Pop the tag stack */
1459 g_free (context
->tag_stack
->data
);
1460 context
->tag_stack
= g_slist_delete_link (context
->tag_stack
,
1461 context
->tag_stack
);
1465 mark_error (context
, tmp_error
);
1466 g_propagate_error (error
, tmp_error
);
1470 g_free (close_name
);
1474 case STATE_INSIDE_PASSTHROUGH
:
1475 /* Possible next state: AFTER_CLOSE_ANGLE */
1478 if (*context
->iter
== '>')
1481 while (advance_char (context
));
1483 if (context
->iter
== context
->current_text_end
)
1485 /* The passthrough hasn't necessarily ended. Merge with
1486 * partial chunk, leave state unchanged.
1488 add_to_partial (context
, context
->start
, context
->iter
);
1492 /* The passthrough has ended at the close angle. Combine
1493 * it with the partial chunk if any. Call the passthrough
1494 * callback. Note that the open/close angles are
1495 * included in the text of the passthrough.
1497 GError
*tmp_error
= NULL
;
1499 advance_char (context
); /* advance past close angle */
1500 add_to_partial (context
, context
->start
, context
->iter
);
1502 if (context
->parser
->passthrough
)
1503 (*context
->parser
->passthrough
) (context
,
1504 context
->partial_chunk
->str
,
1505 context
->partial_chunk
->len
,
1509 free_partial (context
);
1511 if (tmp_error
== NULL
)
1513 context
->state
= STATE_AFTER_CLOSE_ANGLE
;
1514 context
->start
= context
->iter
; /* could begin text */
1518 mark_error (context
, tmp_error
);
1519 g_propagate_error (error
, tmp_error
);
1529 g_assert_not_reached ();
1535 context
->parsing
= FALSE
;
1537 return context
->state
!= STATE_ERROR
;
1541 g_markup_parse_context_end_parse (GMarkupParseContext
*context
,
1544 g_return_val_if_fail (context
!= NULL
, FALSE
);
1545 g_return_val_if_fail (!context
->parsing
, FALSE
);
1546 g_return_val_if_fail (context
->state
!= STATE_ERROR
, FALSE
);
1548 if (context
->document_empty
)
1550 set_error (context
, error
, G_MARKUP_ERROR_EMPTY
,
1551 _("Document was empty or contained only whitespace"));
1555 context
->parsing
= TRUE
;
1557 switch (context
->state
)
1563 case STATE_AFTER_OPEN_ANGLE
:
1564 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1565 _("Document ended unexpectedly just after an open angle bracket '<'"));
1568 case STATE_AFTER_CLOSE_ANGLE
:
1569 if (context
->tag_stack
!= NULL
)
1571 /* Error message the same as for INSIDE_TEXT */
1572 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1573 _("Document ended unexpectedly with elements still open - "
1574 "'%s' was the last element opened"),
1575 current_element (context
));
1579 case STATE_AFTER_ELISION_SLASH
:
1580 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1581 _("Document ended unexpectedly, expected to see a close angle "
1582 "bracket ending the tag <%s/>"), current_element (context
));
1585 case STATE_INSIDE_OPEN_TAG_NAME
:
1586 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1587 _("Document ended unexpectedly inside an element name"));
1590 case STATE_INSIDE_ATTRIBUTE_NAME
:
1591 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1592 _("Document ended unexpectedly inside an attribute name"));
1595 case STATE_BETWEEN_ATTRIBUTES
:
1596 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1597 _("Document ended unexpectedly inside an element-opening "
1601 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN
:
1602 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1603 _("Document ended unexpectedly after the equals sign "
1604 "following an attribute name; no attribute value"));
1607 case STATE_INSIDE_ATTRIBUTE_VALUE
:
1608 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1609 _("Document ended unexpectedly while inside an attribute "
1613 case STATE_INSIDE_TEXT
:
1614 g_assert (context
->tag_stack
!= NULL
);
1615 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1616 _("Document ended unexpectedly with elements still open - "
1617 "'%s' was the last element opened"),
1618 current_element (context
));
1621 case STATE_AFTER_CLOSE_TAG_SLASH
:
1622 case STATE_INSIDE_CLOSE_TAG_NAME
:
1623 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1624 _("Document ended unexpectedly inside the close tag for"
1625 "element '%s'"), current_element
);
1628 case STATE_INSIDE_PASSTHROUGH
:
1629 set_error (context
, error
, G_MARKUP_ERROR_PARSE
,
1630 _("Document ended unexpectedly inside a comment or "
1631 "processing instruction"));
1636 g_assert_not_reached ();
1640 context
->parsing
= FALSE
;
1642 return context
->state
!= STATE_ERROR
;
1646 g_markup_parse_context_get_position (GMarkupParseContext
*context
,
1650 g_return_if_fail (context
!= NULL
);
1653 *line_number
= context
->line_number
;
1656 *char_number
= context
->char_number
;
1660 append_escaped_text (GString
*str
,
1668 end
= text
+ length
;
1673 next
= g_utf8_next_char (p
);
1678 g_string_append (str
, "&");
1682 g_string_append (str
, "<");
1686 g_string_append (str
, ">");
1690 g_string_append (str
, "'");
1694 g_string_append (str
, """);
1698 g_string_append_len (str
, p
, next
- p
);
1707 g_markup_escape_text (const gchar
*text
,
1712 g_return_val_if_fail (text
!= NULL
, NULL
);
1715 length
= strlen (text
);
1717 str
= g_string_new ("");
1718 append_escaped_text (str
, text
, length
);
1720 return g_string_free (str
, FALSE
);