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-width 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_width(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.         css_fixed side_length[4];
  39.         uint32_t side_unit[4];
  40.         uint32_t side_count = 0;
  41.         bool match;
  42.         css_error error;
  43.  
  44.         /* Firstly, handle inherit */
  45.         token = parserutils_vector_peek(vector, *ctx);
  46.         if (token == NULL)
  47.                 return CSS_INVALID;
  48.                
  49.         if (is_css_inherit(c, token)) {
  50.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_TOP_WIDTH);
  51.                 if (error != CSS_OK)
  52.                         return error;
  53.  
  54.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_RIGHT_WIDTH);
  55.                 if (error != CSS_OK)
  56.                         return error;          
  57.  
  58.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_BOTTOM_WIDTH);
  59.                 if (error != CSS_OK)
  60.                         return error;
  61.  
  62.                 error = css_stylesheet_style_inherit(result, CSS_PROP_BORDER_LEFT_WIDTH);
  63.                 if (error == CSS_OK)
  64.                         parserutils_vector_iterate(vector, ctx);
  65.  
  66.                 return error;
  67.         }
  68.  
  69.         /* Attempt to parse up to 4 widths */
  70.         do {
  71.                 prev_ctx = *ctx;
  72.  
  73.                 if ((token != NULL) && is_css_inherit(c, token)) {
  74.                         *ctx = orig_ctx;
  75.                         return CSS_INVALID;
  76.                 }
  77.  
  78.                 if ((token->type == CSS_TOKEN_IDENT) && (lwc_string_caseless_isequal(token->idata, c->strings[THIN], &match) == lwc_error_ok && match)) {
  79.                         side_val[side_count] =  BORDER_WIDTH_THIN;
  80.                         parserutils_vector_iterate(vector, ctx);
  81.                         error = CSS_OK;
  82.                 } else if ((token->type == CSS_TOKEN_IDENT) && (lwc_string_caseless_isequal(token->idata, c->strings[MEDIUM], &match) == lwc_error_ok && match)) {
  83.                         side_val[side_count] =  BORDER_WIDTH_MEDIUM;
  84.                         parserutils_vector_iterate(vector, ctx);
  85.                         error = CSS_OK;
  86.                 } else if ((token->type == CSS_TOKEN_IDENT) && (lwc_string_caseless_isequal(token->idata, c->strings[THICK], &match) == lwc_error_ok && match)) {
  87.                         parserutils_vector_iterate(vector, ctx);
  88.                         error = CSS_OK;
  89.                         side_val[side_count] =  BORDER_WIDTH_THICK;
  90.                 } else {
  91.                         side_val[side_count] = BORDER_WIDTH_SET;
  92.  
  93.                         error = css__parse_unit_specifier(c, vector, ctx, UNIT_PX, &side_length[side_count], &side_unit[side_count]);
  94.                         if (error == CSS_OK) {
  95.                                 if (side_unit[side_count] == UNIT_PCT ||
  96.                                     side_unit[side_count] & UNIT_ANGLE ||
  97.                                     side_unit[side_count] & UNIT_TIME ||
  98.                                     side_unit[side_count] & UNIT_FREQ) {
  99.                                         *ctx = orig_ctx;
  100.                                         return CSS_INVALID;
  101.                                 }
  102.  
  103.                                 if (side_length[side_count] < 0) {
  104.                                         *ctx = orig_ctx;
  105.                                         return CSS_INVALID;
  106.                                 }
  107.                         }
  108.                 }
  109.  
  110.                 if (error == CSS_OK) {
  111.                         side_count++;
  112.  
  113.                         consumeWhitespace(vector, ctx);
  114.  
  115.                         token = parserutils_vector_peek(vector, *ctx);
  116.                 } else {
  117.                         /* Forcibly cause loop to exit */
  118.                         token = NULL;
  119.                 }
  120.         } while ((*ctx != prev_ctx) && (token != NULL) && (side_count < 4));
  121.  
  122.  
  123. #define SIDE_APPEND(OP,NUM)                                                             \
  124.         error = css__stylesheet_style_appendOPV(result, (OP), 0, side_val[(NUM)]);      \
  125.         if (error != CSS_OK)                                                            \
  126.                 break;                                                                  \
  127.         if (side_val[(NUM)] == BORDER_WIDTH_SET) {                                      \
  128.                 error = css__stylesheet_style_append(result, side_length[(NUM)]);       \
  129.                 if (error != CSS_OK)                                                    \
  130.                         break;                                                          \
  131.                 error = css__stylesheet_style_append(result, side_unit[(NUM)]);         \
  132.                 if (error != CSS_OK)                                                    \
  133.                         break;                                                          \
  134.         }
  135.  
  136.         switch (side_count) {
  137.         case 1:
  138.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_WIDTH, 0);
  139.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_WIDTH, 0);
  140.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_WIDTH, 0);
  141.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_WIDTH, 0);
  142.                 break;
  143.         case 2:
  144.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_WIDTH, 0);
  145.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_WIDTH, 1);
  146.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_WIDTH, 0);
  147.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_WIDTH, 1);
  148.                 break;
  149.         case 3:
  150.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_WIDTH, 0);
  151.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_WIDTH, 1);
  152.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_WIDTH, 2);
  153.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_WIDTH, 1);
  154.                 break;
  155.         case 4:
  156.                 SIDE_APPEND(CSS_PROP_BORDER_TOP_WIDTH, 0);
  157.                 SIDE_APPEND(CSS_PROP_BORDER_RIGHT_WIDTH, 1);
  158.                 SIDE_APPEND(CSS_PROP_BORDER_BOTTOM_WIDTH, 2);
  159.                 SIDE_APPEND(CSS_PROP_BORDER_LEFT_WIDTH, 3);
  160.                 break;
  161.         default:
  162.                 error = CSS_INVALID;
  163.                 break;
  164.         }
  165.  
  166.         if (error != CSS_OK)
  167.                 *ctx = orig_ctx;
  168.  
  169.         return error;
  170. }
  171.