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 outline shorthand
  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_outline(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 color = true;
  39.         bool style = true;
  40.         bool width = true;
  41.         css_style *color_style;
  42.         css_style *style_style;
  43.         css_style *width_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_OUTLINE_COLOR);
  52.                 if (error != CSS_OK)
  53.                         return error;
  54.  
  55.                 error = css_stylesheet_style_inherit(result, CSS_PROP_OUTLINE_STYLE);
  56.                 if (error != CSS_OK)
  57.                         return error;          
  58.  
  59.                 error = css_stylesheet_style_inherit(result, CSS_PROP_OUTLINE_WIDTH);
  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, &color_style);
  69.         if (error != CSS_OK)
  70.                 return error;
  71.  
  72.         error = css__stylesheet_style_create(c->sheet, &style_style);
  73.         if (error != CSS_OK) {
  74.                 css__stylesheet_style_destroy(color_style);
  75.                 return error;
  76.         }
  77.  
  78.         error = css__stylesheet_style_create(c->sheet, &width_style);
  79.         if (error != CSS_OK) {
  80.                 css__stylesheet_style_destroy(color_style);
  81.                 css__stylesheet_style_destroy(style_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_outline_cleanup;
  95.                 }
  96.  
  97.                 if ((color) &&
  98.                            (error = css__parse_outline_color(c, vector, ctx,
  99.                                 color_style)) == CSS_OK) {
  100.                         color = false;
  101.                 } else if ((style) &&
  102.                            (error = css__parse_outline_style(c, vector,
  103.                                 ctx, style_style)) == CSS_OK) {
  104.                         style = false;
  105.                 } else if ((width) &&
  106.                     (error = css__parse_outline_width(c, vector,
  107.                                 ctx, width_style)) == CSS_OK) {
  108.                         width = 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 (color) {
  124.                 error = css__stylesheet_style_appendOPV(color_style,
  125.                                CSS_PROP_OUTLINE_COLOR,
  126.                                 0, OUTLINE_COLOR_INVERT);
  127.         }
  128.  
  129.         if (style) {
  130.                 error = css__stylesheet_style_appendOPV(style_style,
  131.                                CSS_PROP_OUTLINE_STYLE,
  132.                                 0, OUTLINE_STYLE_NONE);
  133.         }
  134.  
  135.         if (width) {
  136.                 error = css__stylesheet_style_appendOPV(width_style,
  137.                                CSS_PROP_OUTLINE_WIDTH,
  138.                                 0, OUTLINE_WIDTH_MEDIUM);
  139.         }
  140.  
  141.  
  142.         error = css__stylesheet_merge_style(result, color_style);
  143.         if (error != CSS_OK)
  144.                 goto css__parse_outline_cleanup;
  145.  
  146.         error = css__stylesheet_merge_style(result, style_style);
  147.         if (error != CSS_OK)
  148.                 goto css__parse_outline_cleanup;
  149.  
  150.         error = css__stylesheet_merge_style(result, width_style);
  151.  
  152.  
  153. css__parse_outline_cleanup:
  154.  
  155.         css__stylesheet_style_destroy(width_style);
  156.         css__stylesheet_style_destroy(style_style);
  157.         css__stylesheet_style_destroy(color_style);
  158.  
  159.         if (error != CSS_OK)
  160.                 *ctx = orig_ctx;
  161.  
  162.         return error;
  163. }
  164.