Ejemplo n.º 1
0
/**
 * cr_declaration_parse_list_from_buf:
 *@a_str: the input buffer that contains the list of declaration to
 *parse.
 *@a_enc: the encoding of a_str
 *
 *Parses a ';' separated list of properties declaration.
 *Returns the parsed list of declaration, NULL if parsing failed.
 */
CRDeclaration *
cr_declaration_parse_list_from_buf (const guchar * a_str,
                                    enum CREncoding a_enc)
{

        enum CRStatus status = CR_OK;
        CRTerm *value = NULL;
        CRString *property = NULL;
        CRDeclaration *result = NULL,
                *cur_decl = NULL;
        CRParser *parser = NULL;
        CRTknzr *tokenizer = NULL;
        gboolean important = FALSE;

        g_return_val_if_fail (a_str, NULL);

        parser = cr_parser_new_from_buf ((guchar*)a_str, strlen (a_str), a_enc, FALSE);
        g_return_val_if_fail (parser, NULL);
        status = cr_parser_get_tknzr (parser, &tokenizer);
        if (status != CR_OK || !tokenizer) {
                if (status == CR_OK)
                        status = CR_ERROR;
                goto cleanup;
        }
        status = cr_parser_try_to_skip_spaces_and_comments (parser);
        if (status != CR_OK)
                goto cleanup;

        status = cr_parser_parse_declaration (parser, &property,
                                              &value, &important);
        if (status != CR_OK || !property) {
                if (status != CR_OK)
                        status = CR_ERROR;
                goto cleanup;
        }
        result = cr_declaration_new (NULL, property, value);
        if (result) {
                property = NULL;
                value = NULL;
                result->important = important;
        }
        /*now, go parse the other declarations */
        for (;;) {
                guint32 c = 0;

                cr_parser_try_to_skip_spaces_and_comments (parser);
                status = cr_tknzr_peek_char (tokenizer, &c);
                if (status != CR_OK) {
                        if (status == CR_END_OF_INPUT_ERROR)
                                status = CR_OK;
                        goto cleanup;
                }
                if (c == ';') {
                        status = cr_tknzr_read_char (tokenizer, &c);
                } else {
                        break;
                }
                important = FALSE;
                cr_parser_try_to_skip_spaces_and_comments (parser);
                status = cr_parser_parse_declaration (parser, &property,
                                                      &value, &important);
                if (status != CR_OK || !property) {
                        if (status == CR_END_OF_INPUT_ERROR) {
                                status = CR_OK;
                        }
                        break;
                }
                cur_decl = cr_declaration_new (NULL, property, value);
                if (cur_decl) {
                        cur_decl->important = important;
                        result = cr_declaration_append (result, cur_decl);
                        property = NULL;
                        value = NULL;
                        cur_decl = NULL;
                } else {
                        break;
                }
        }

      cleanup:

        if (parser) {
                cr_parser_destroy (parser);
                parser = NULL;
        }

        if (property) {
                cr_string_destroy (property);
                property = NULL;
        }

        if (value) {
                cr_term_destroy (value);
                value = NULL;
        }

        if (status != CR_OK && result) {
                cr_declaration_destroy (result);
                result = NULL;
        }
        return result;
}
Ejemplo n.º 2
0
/**
 *Parses a ';' separated list of properties declaration.
 *@param a_str the input buffer that contains the list of declaration to
 *parse.
 *@param a_enc the encoding of a_str
 *@return the parsed list of declaration, NULL if parsing failed.
 */
CRDeclaration *
cr_declaration_parse_list_from_buf (const guchar * a_str,
                                    enum CREncoding a_enc)
{

        enum CRStatus status = CR_OK;
        CRTerm *value = NULL;
        CRString *property = NULL;
        CRDeclaration *result = NULL,
                *cur_decl = NULL;
        CRTknzr *tokenizer = NULL;
        gboolean important = FALSE;

        g_return_val_if_fail (a_str, NULL);

        CRParser *parser = (CRParser *)cr_parser_new_from_buf
		      ((guchar*)a_str, strlen ((char *)a_str), a_enc, FALSE);
        g_return_val_if_fail (parser, NULL);
        status = cr_parser_get_tknzr (parser, &tokenizer);
        if (status != CR_OK || !tokenizer) {
                if (status == CR_OK)
                        status = CR_ERROR;
                goto cleanup;
        }
        status = cr_parser_try_to_skip_spaces_and_comments (parser);
        if (status != CR_OK)
                goto cleanup;

        status = cr_parser_parse_declaration (parser, &property,
                                              &value, &important);
        if (status != CR_OK || !property) {
                if (status != CR_OK)
                        status = CR_ERROR;
                goto cleanup;
        }
        result = cr_declaration_new (NULL, property, value);
        if (result) {
                property = NULL;
                value = NULL;
                result->important = important;
        }
        /*now, go parse the other declarations */
        for (;;) {
                guint32 c = 0;

                cr_parser_try_to_skip_spaces_and_comments (parser);
                status = cr_tknzr_peek_char (tokenizer, &c);
                if (status != CR_OK) {
                        if (status == CR_END_OF_INPUT_ERROR) 
                                status = CR_OK;
                        goto cleanup;
                }
                if (c == ';') {
                        status = cr_tknzr_read_char (tokenizer, &c);
                } else {
                        cr_tknzr_read_char (tokenizer, &c);
                        continue; // try to keep reading until we reach the end or a ;
                }
                important = FALSE;
                cr_parser_try_to_skip_spaces_and_comments (parser);
                status = cr_parser_parse_declaration (parser, &property,
                                                      &value, &important);
                if (status != CR_OK || !property) {
                        if (status == CR_END_OF_INPUT_ERROR) {
                                status = CR_OK; // simply the end of input, do not delete what we got so far, just finish
                                break; 
                        } else {
                                continue; // even if one declaration is broken, it's no reason to discard others (see http://www.w3.org/TR/CSS21/syndata.html#declaration)
								}
                }
                cur_decl = cr_declaration_new (NULL, property, value);
                if (cur_decl) {
                        cur_decl->important = important;
                        result = cr_declaration_append (result, cur_decl);
                        property = NULL;
                        value = NULL;
                        cur_decl = NULL;
                } else {
                        break;
                }
        }

      cleanup:

        if (parser) {
                cr_parser_destroy (parser);
                parser = NULL;
        }

        if (property) {
                cr_string_destroy (property);
                property = NULL;
        }

        if (value) {
                cr_term_destroy (value);
                value = NULL;
        }

        if (status != CR_OK && result) {
                cr_declaration_destroy (result);
                result = NULL;
        }
        return result;
}