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-spacing
  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_spacing(css_language *c,
  31.                 const parserutils_vector *vector, int *ctx,
  32.                 css_style *result)
  33. {
  34.         int orig_ctx = *ctx;
  35.         css_error error;
  36.         const css_token *token;
  37.         css_fixed length[2] = { 0 };
  38.         uint32_t unit[2] = { 0 };
  39.         bool match;
  40.  
  41.         /* length length? | IDENT(inherit) */
  42.         token = parserutils_vector_peek(vector, *ctx);
  43.         if (token == NULL) {
  44.                 *ctx = orig_ctx;
  45.                 return CSS_INVALID;
  46.         }
  47.  
  48.         if (token->type == CSS_TOKEN_IDENT &&
  49.                         (lwc_string_caseless_isequal(
  50.                         token->idata, c->strings[INHERIT],
  51.                         &match) == lwc_error_ok && match)) {
  52.                 parserutils_vector_iterate(vector, ctx);
  53.                 /* inherit */
  54.                 error = css__stylesheet_style_appendOPV(result,
  55.                                                        CSS_PROP_BORDER_SPACING,
  56.                                                        FLAG_INHERIT,
  57.                                                        0);
  58.         } else {
  59.                 int num_lengths = 0;
  60.  
  61.                 error = css__parse_unit_specifier(c, vector, ctx, UNIT_PX,
  62.                                 &length[0], &unit[0]);
  63.                 if (error != CSS_OK) {
  64.                         *ctx = orig_ctx;
  65.                         return error;
  66.                 }
  67.  
  68.                 if (unit[0] & UNIT_ANGLE || unit[0] & UNIT_TIME ||
  69.                                 unit[0] & UNIT_FREQ || unit[0] & UNIT_PCT) {
  70.                         *ctx = orig_ctx;
  71.                         return CSS_INVALID;
  72.                 }
  73.  
  74.                 num_lengths = 1;
  75.  
  76.                 consumeWhitespace(vector, ctx);
  77.  
  78.                 token = parserutils_vector_peek(vector, *ctx);
  79.                 if (token != NULL) {
  80.                         /* Attempt second length, ignoring errors.
  81.                          * The core !important parser will ensure
  82.                          * any remaining junk is thrown out.
  83.                          * Ctx will be preserved on error, as usual
  84.                          */
  85.                         error = css__parse_unit_specifier(c, vector, ctx, UNIT_PX,
  86.                                         &length[1], &unit[1]);
  87.                         if (error == CSS_OK) {
  88.                                 if (unit[1] & UNIT_ANGLE ||
  89.                                                 unit[1] & UNIT_TIME ||
  90.                                                 unit[1] & UNIT_FREQ ||
  91.                                                 unit[1] & UNIT_PCT) {
  92.                                         *ctx = orig_ctx;
  93.                                         return CSS_INVALID;
  94.                                 }
  95.  
  96.                                 num_lengths = 2;
  97.                         }
  98.                 }
  99.  
  100.                 if (num_lengths == 1) {
  101.                         /* Only one length specified. Use for both axes. */
  102.                         length[1] = length[0];
  103.                         unit[1] = unit[0];
  104.                 }
  105.  
  106.                 /* Lengths must not be negative */
  107.                 if (length[0] < 0 || length[1] < 0) {
  108.                         *ctx = orig_ctx;
  109.                         return CSS_INVALID;
  110.                 }
  111.  
  112.                 error = css__stylesheet_style_appendOPV(result,
  113.                                                        CSS_PROP_BORDER_SPACING,
  114.                                                        0,
  115.                                                        BORDER_SPACING_SET);
  116.  
  117.                 if (error != CSS_OK) {
  118.                         *ctx = orig_ctx;
  119.                         return error;
  120.                 }
  121.  
  122.                 error = css__stylesheet_style_vappend(result,
  123.                                                      4,
  124.                                                      length[0],
  125.                                                      unit[0],
  126.                                                      length[1],
  127.                                                      unit[1]);
  128.  
  129.         }
  130.  
  131.         if (error != CSS_OK) {
  132.                 *ctx = orig_ctx;
  133.                 return error;
  134.         }
  135.  
  136.         return CSS_OK;
  137. }
  138.