GMarkup: share some common code for closing tags

The code for dealing with </foo> and the second half of <foo/> was
largely duplicated.  We can share a lot of it by using a common
function.

This slightly changes the behaviour of the parser under error
circumstances: previously the parser would deal with '<foo/}' by first
issuing the end_element callback and then flagging the error due to the
unexpected character.  Now we will flag the unexpected character error
first, skipping the callback.

This behaviour change required modifying the testsuite.

https://bugzilla.gnome.org/show_bug.cgi?id=665634
This commit is contained in:
Ryan Lortie 2013-10-28 13:56:26 -07:00
parent 44f13124c9
commit cbccbaeacf
3 changed files with 50 additions and 65 deletions

View File

@ -1038,6 +1038,37 @@ emit_start_element (GMarkupParseContext *context,
propagate_error (context, error, tmp_error); propagate_error (context, error, tmp_error);
} }
static void
emit_end_element (GMarkupParseContext *context,
GError **error)
{
/* We need to pop the tag stack and call the end_element
* function, since this is the close tag
*/
GError *tmp_error = NULL;
g_assert (context->tag_stack != NULL);
possibly_finish_subparser (context);
tmp_error = NULL;
if (context->parser->end_element)
(* context->parser->end_element) (context,
current_element (context),
context->user_data,
&tmp_error);
ensure_no_outstanding_subparser (context);
if (tmp_error)
{
mark_error (context, tmp_error);
g_propagate_error (error, tmp_error);
}
pop_tag (context);
}
/** /**
* g_markup_parse_context_parse: * g_markup_parse_context_parse:
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
@ -1184,38 +1215,12 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
case STATE_AFTER_ELISION_SLASH: case STATE_AFTER_ELISION_SLASH:
/* Possible next state: AFTER_CLOSE_ANGLE */ /* Possible next state: AFTER_CLOSE_ANGLE */
{
/* We need to pop the tag stack and call the end_element
* function, since this is the close tag
*/
GError *tmp_error = NULL;
g_assert (context->tag_stack != NULL);
possibly_finish_subparser (context);
tmp_error = NULL;
if (context->parser->end_element)
(* context->parser->end_element) (context,
current_element (context),
context->user_data,
&tmp_error);
ensure_no_outstanding_subparser (context);
if (tmp_error)
{
mark_error (context, tmp_error);
g_propagate_error (error, tmp_error);
}
else
{
if (*context->iter == '>') if (*context->iter == '>')
{ {
/* move after the close angle */ /* move after the close angle */
advance_char (context); advance_char (context);
context->state = STATE_AFTER_CLOSE_ANGLE; context->state = STATE_AFTER_CLOSE_ANGLE;
emit_end_element (context, error);
} }
else else
{ {
@ -1229,9 +1234,6 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
utf8_str (context->iter, buf), utf8_str (context->iter, buf),
current_element (context)); current_element (context));
} }
}
pop_tag (context);
}
break; break;
case STATE_INSIDE_OPEN_TAG_NAME: case STATE_INSIDE_OPEN_TAG_NAME:
@ -1589,26 +1591,11 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
} }
else else
{ {
GError *tmp_error;
advance_char (context); advance_char (context);
context->state = STATE_AFTER_CLOSE_ANGLE; context->state = STATE_AFTER_CLOSE_ANGLE;
context->start = NULL; context->start = NULL;
possibly_finish_subparser (context); emit_end_element (context, error);
/* call the end_element callback */
tmp_error = NULL;
if (context->parser->end_element)
(* context->parser->end_element) (context,
close_name->str,
context->user_data,
&tmp_error);
ensure_no_outstanding_subparser (context);
pop_tag (context);
if (tmp_error)
propagate_error (context, error, tmp_error);
} }
context->partial_chunk = close_name; context->partial_chunk = close_name;
truncate_partial (context); truncate_partial (context);

View File

@ -1,3 +1,2 @@
ELEMENT 'foo' ELEMENT 'foo'
END 'foo'
ERROR Error on line 1 char 6: Odd character '≻', expected a '>' character to end the empty-element tag 'foo' ERROR Error on line 1 char 6: Odd character '≻', expected a '>' character to end the empty-element tag 'foo'

View File

@ -1,3 +1,2 @@
ELEMENT 'foo' ELEMENT 'foo'
END 'foo'
ERROR Error on line 1 char 6: Odd character '}', expected a '>' character to end the empty-element tag 'foo' ERROR Error on line 1 char 6: Odd character '}', expected a '>' character to end the empty-element tag 'foo'