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 border-color 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_border_color(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.         uint16_t side_val[4];
  38.         uint32_t side_color[4];
  39.         uint32_t side_count = 0;
  40.         css_error error;
  41.  
  42.         /* Firstly, handle inherit */
  43.         token = parserutils_vector_peek(vector, *ctx);
  44.         if (token == NULL)
  45.                 return CSS_INVALID;
  46.                
  47.         if (is_css_inherit(c, token)) {
  48.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_TOP_COLOR);
  49.                 if (error != CSS_OK)
  50.                         return error;
  51.  
  52.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_RIGHT_COLOR);
  53.                 if (error != CSS_OK)
  54.                         return error;          
  55.  
  56.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_BOTTOM_COLOR);
  57.                 if (error != CSS_OK)
  58.                         return error;
  59.  
  60.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_LEFT_COLOR);
  61.                 if (error == CSS_OK)
  62.                         parserutils_vector_iterate(vector, ctx);
  63.  
  64.                 return error;
  65.         }
  66.  
  67.         /* Attempt to parse up to 4 colours */
  68.         do {
  69.                 prev_ctx = *ctx;
  70.  
  71.                 if ((token != NULL) && is_css_inherit(c, token)) {
  72.                         *ctx = orig_ctx;
  73.                         return CSS_INVALID;
  74.                 }
  75.  
  76.                 error = css__parse_colour_specifier(c, vector, ctx, &side_val[side_count], &side_color[side_count]);
  77.                 if (error == CSS_OK) {
  78.                         side_count++;
  79.  
  80.                         consumeWhitespace(vector, ctx);
  81.  
  82.                         token = parserutils_vector_peek(vector, *ctx);
  83.                 } else {
  84.                         /* Forcibly cause loop to exit */
  85.                         token = NULL;
  86.                 }
  87.         } while ((*ctx != prev_ctx) && (token != NULL) && (side_count < 4));
  88.  
  89.  
  90. #define SIDE_APPEND(OP,NUM)                                                             \
  91.         error = css__stylesheet_style_appendOPV(result, (OP), 0, side_val[(NUM)]);      \
  92.         if (error != CSS_OK)                                                            \
  93.                 break;                                                                  \
  94.         if (side_val[(NUM)] == BORDER_COLOR_SET)                                        \
  95.                 error = css__stylesheet_style_append(result, side_color[(NUM)]);                \
  96.         if (error != CSS_OK)                                                            \
  97.                 break;
  98.  
  99.         switch (side_count) {
  100.         case 1:
  101.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_COLOR, 0);
  102.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_COLOR, 0);
  103.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_COLOR, 0);
  104.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_COLOR, 0);
  105.                 break;
  106.         case 2:
  107.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_COLOR, 0);
  108.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_COLOR, 1);
  109.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_COLOR, 0);
  110.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_COLOR, 1);
  111.                 break;
  112.         case 3:
  113.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_COLOR, 0);
  114.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_COLOR, 1);
  115.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_COLOR, 2);
  116.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_COLOR, 1);
  117.                 break;
  118.         case 4:
  119.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_COLOR, 0);
  120.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_COLOR, 1);
  121.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_COLOR, 2);
  122.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_COLOR, 3);
  123.                 break;
  124.         default:
  125.                 error = CSS_INVALID;
  126.                 break;
  127.         }
  128.  
  129.         if (error != CSS_OK)
  130.                 *ctx = orig_ctx;
  131.  
  132.         return error;
  133. }
  134.