Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of LibCSS.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #include <assert.h>
  9. #include <string.h>
  10.  
  11. #include "bytecode/bytecode.h"
  12. #include "bytecode/opcodes.h"
  13. #include "parse/properties/properties.h"
  14. #include "parse/properties/utils.h"
  15.  
  16. /**
  17.  * Parse list-style
  18.  *
  19.  * \param c       Parsing context
  20.  * \param vector  Vector of tokens to process
  21.  * \param ctx     Pointer to vector iteration context
  22.  * \param result  Pointer to location to receive resulting style
  23.  * \return CSS_OK on success,
  24.  *         CSS_NOMEM on memory exhaustion,
  25.  *         CSS_INVALID if the input is not valid
  26.  *
  27.  * Post condition: \a *ctx is updated with the next token to process
  28.  *                 If the input is invalid, then \a *ctx remains unchanged.
  29.  */
  30. css_error css__parse_list_style(css_language *c,
  31.                 const parserutils_vector *vector, int *ctx,
  32.                 css_style *result)
  33. {
  34.         int orig_ctx = *ctx;
  35.         int prev_ctx;
  36.         const css_token *token;
  37.         css_error error;
  38.         bool image = true;
  39.         bool position = true;
  40.         bool type = true;
  41.         css_style *image_style;
  42.         css_style *position_style;
  43.         css_style *type_style;
  44.  
  45.         /* Firstly, handle inherit */
  46.         token = parserutils_vector_peek(vector, *ctx);
  47.         if (token == NULL)
  48.                 return CSS_INVALID;
  49.                
  50.         if (is_css_inherit(c, token)) {
  51.                 error = css_stylesheet_style_inherit(result, CSS_PROP_LIST_STYLE_IMAGE);
  52.                 if (error != CSS_OK)
  53.                         return error;
  54.  
  55.                 error = css_stylesheet_style_inherit(result, CSS_PROP_LIST_STYLE_POSITION);
  56.                 if (error != CSS_OK)
  57.                         return error;          
  58.  
  59.                 error = css_stylesheet_style_inherit(result, CSS_PROP_LIST_STYLE_TYPE);
  60.  
  61.                 if (error == CSS_OK)
  62.                         parserutils_vector_iterate(vector, ctx);
  63.  
  64.                 return error;
  65.         }
  66.  
  67.         /* allocate styles */
  68.         error = css__stylesheet_style_create(c->sheet, &image_style);
  69.         if (error != CSS_OK)
  70.                 return error;
  71.  
  72.         error = css__stylesheet_style_create(c->sheet, &position_style);
  73.         if (error != CSS_OK) {
  74.                 css__stylesheet_style_destroy(image_style);
  75.                 return error;
  76.         }
  77.  
  78.         error = css__stylesheet_style_create(c->sheet, &type_style);
  79.         if (error != CSS_OK) {
  80.                 css__stylesheet_style_destroy(image_style);
  81.                 css__stylesheet_style_destroy(position_style);
  82.                 return error;
  83.         }
  84.  
  85.         /* Attempt to parse the various longhand properties */
  86.         do {
  87.                 prev_ctx = *ctx;
  88.                 error = CSS_OK;
  89.  
  90.                 /* Ensure that we're not about to parse another inherit */
  91.                 token = parserutils_vector_peek(vector, *ctx);
  92.                 if (token != NULL && is_css_inherit(c, token)) {
  93.                         error = CSS_INVALID;
  94.                         goto css__parse_list_style_cleanup;
  95.                 }
  96.  
  97.                 if ((type) &&
  98.                     (error = css__parse_list_style_type(c, vector,
  99.                                 ctx, type_style)) == CSS_OK) {
  100.                         type = false;
  101.                 } else if ((position) &&
  102.                            (error = css__parse_list_style_position(c, vector,
  103.                                 ctx, position_style)) == CSS_OK) {
  104.                         position = false;
  105.                 } else if ((image) &&
  106.                            (error = css__parse_list_style_image(c, vector, ctx,
  107.                                 image_style)) == CSS_OK) {
  108.                         image = false;
  109.                 }
  110.  
  111.                 if (error == CSS_OK) {
  112.                         consumeWhitespace(vector, ctx);
  113.  
  114.                         token = parserutils_vector_peek(vector, *ctx);
  115.                 } else {
  116.                         /* Forcibly cause loop to exit */
  117.                         token = NULL;
  118.                 }
  119.         } while (*ctx != prev_ctx && token != NULL);
  120.  
  121.  
  122.         /* defaults */
  123.         if (image) {
  124.                 error = css__stylesheet_style_appendOPV(image_style,
  125.                                CSS_PROP_LIST_STYLE_IMAGE,
  126.                                 0, LIST_STYLE_IMAGE_NONE);
  127.         }
  128.  
  129.         if (position) {
  130.                 error = css__stylesheet_style_appendOPV(position_style,
  131.                                CSS_PROP_LIST_STYLE_POSITION,
  132.                                 0, LIST_STYLE_POSITION_OUTSIDE);
  133.         }
  134.  
  135.         if (type) {
  136.                 error = css__stylesheet_style_appendOPV(type_style,
  137.                                CSS_PROP_LIST_STYLE_TYPE,
  138.                                 0, LIST_STYLE_TYPE_DISC);
  139.         }
  140.  
  141.  
  142.         error = css__stylesheet_merge_style(result, image_style);
  143.         if (error != CSS_OK)
  144.                 goto css__parse_list_style_cleanup;
  145.  
  146.         error = css__stylesheet_merge_style(result, position_style);
  147.         if (error != CSS_OK)
  148.                 goto css__parse_list_style_cleanup;
  149.  
  150.         error = css__stylesheet_merge_style(result, type_style);
  151.  
  152.  
  153. css__parse_list_style_cleanup:
  154.  
  155.         css__stylesheet_style_destroy(type_style);
  156.         css__stylesheet_style_destroy(position_style);
  157.         css__stylesheet_style_destroy(image_style);
  158.  
  159.         if (error != CSS_OK)
  160.                 *ctx = orig_ctx;
  161.  
  162.         return error;
  163.  
  164. }
  165.