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 margin 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_margin(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_MARGIN_TOP);
  51.                 if (error != CSS_OK)
  52.                         return error;
  53.  
  54.                 error = css_stylesheet_style_inherit(result, CSS_PROP_MARGIN_RIGHT);
  55.                 if (error != CSS_OK)
  56.                         return error;          
  57.  
  58.                 error = css_stylesheet_style_inherit(result, CSS_PROP_MARGIN_BOTTOM);
  59.                 if (error != CSS_OK)
  60.                         return error;
  61.  
  62.                 error = css_stylesheet_style_inherit(result, CSS_PROP_MARGIN_LEFT);
  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[AUTO], &match) == lwc_error_ok && match)) {
  79.                         side_val[side_count] =  MARGIN_AUTO;
  80.                         parserutils_vector_iterate(vector, ctx);
  81.                         error = CSS_OK;
  82.                 } else {
  83.                         side_val[side_count] = MARGIN_SET;
  84.  
  85.                         error = css__parse_unit_specifier(c, vector, ctx, UNIT_PX, &side_length[side_count], &side_unit[side_count]);
  86.                         if (error == CSS_OK) {
  87.                                 if (side_unit[side_count] & UNIT_ANGLE||
  88.                                     side_unit[side_count] & UNIT_TIME||
  89.                                     side_unit[side_count] & UNIT_FREQ) {
  90.                                         *ctx = orig_ctx;
  91.                                         return CSS_INVALID;
  92.                                 }
  93.                         }
  94.                 }
  95.  
  96.                 if (error == CSS_OK) {
  97.                         side_count++;
  98.  
  99.                         consumeWhitespace(vector, ctx);
  100.  
  101.                         token = parserutils_vector_peek(vector, *ctx);
  102.                 } else {
  103.                         /* Forcibly cause loop to exit */
  104.                         token = NULL;
  105.                 }
  106.         } while ((*ctx != prev_ctx) && (token != NULL) && (side_count < 4));
  107.  
  108.  
  109. #define SIDE_APPEND(OP,NUM)                                                             \
  110.         error = css__stylesheet_style_appendOPV(result, (OP), 0, side_val[(NUM)]);      \
  111.         if (error != CSS_OK)                                                            \
  112.                 break;                                                                  \
  113.         if (side_val[(NUM)] == MARGIN_SET) {                                            \
  114.                 error = css__stylesheet_style_append(result, side_length[(NUM)]);       \
  115.                 if (error != CSS_OK)                                                    \
  116.                         break;                                                          \
  117.                 error = css__stylesheet_style_append(result, side_unit[(NUM)]);         \
  118.                 if (error != CSS_OK)                                                    \
  119.                         break;                                                          \
  120.         }
  121.  
  122.         switch (side_count) {
  123.         case 1:
  124.                 SIDE_APPEND(CSS_PROP_MARGIN_TOP, 0);
  125.                 SIDE_APPEND(CSS_PROP_MARGIN_RIGHT, 0);
  126.                 SIDE_APPEND(CSS_PROP_MARGIN_BOTTOM, 0);
  127.                 SIDE_APPEND(CSS_PROP_MARGIN_LEFT, 0);
  128.                 break;
  129.         case 2:
  130.                 SIDE_APPEND(CSS_PROP_MARGIN_TOP, 0);
  131.                 SIDE_APPEND(CSS_PROP_MARGIN_RIGHT, 1);
  132.                 SIDE_APPEND(CSS_PROP_MARGIN_BOTTOM, 0);
  133.                 SIDE_APPEND(CSS_PROP_MARGIN_LEFT, 1);
  134.                 break;
  135.         case 3:
  136.                 SIDE_APPEND(CSS_PROP_MARGIN_TOP, 0);
  137.                 SIDE_APPEND(CSS_PROP_MARGIN_RIGHT, 1);
  138.                 SIDE_APPEND(CSS_PROP_MARGIN_BOTTOM, 2);
  139.                 SIDE_APPEND(CSS_PROP_MARGIN_LEFT, 1);
  140.                 break;
  141.         case 4:
  142.                 SIDE_APPEND(CSS_PROP_MARGIN_TOP, 0);
  143.                 SIDE_APPEND(CSS_PROP_MARGIN_RIGHT, 1);
  144.                 SIDE_APPEND(CSS_PROP_MARGIN_BOTTOM, 2);
  145.                 SIDE_APPEND(CSS_PROP_MARGIN_LEFT, 3);
  146.                 break;
  147.         default:
  148.                 error = CSS_INVALID;
  149.         }
  150.  
  151.         if (error != CSS_OK)
  152.                 *ctx = orig_ctx;
  153.  
  154.         return error;
  155. }
  156.  
  157.