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 2012 Michael Drake <tlsa@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 columns 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_columns(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 = CSS_OK;
  38.         bool width = true;
  39.         bool count = true;
  40.         css_style *width_style;
  41.         css_style *count_style;
  42.  
  43.         /* Firstly, handle inherit */
  44.         token = parserutils_vector_peek(vector, *ctx);
  45.         if (token == NULL)
  46.                 return CSS_INVALID;
  47.  
  48.         if (is_css_inherit(c, token)) {
  49.                 error = css_stylesheet_style_inherit(result,
  50.                                 CSS_PROP_COLUMN_WIDTH);
  51.                 if (error != CSS_OK)
  52.                         return error;
  53.  
  54.                 error = css_stylesheet_style_inherit(result,
  55.                                 CSS_PROP_COLUMN_COUNT);
  56.                 if (error == CSS_OK)
  57.                         parserutils_vector_iterate(vector, ctx);
  58.  
  59.                 return error;
  60.         }
  61.  
  62.         /* Allocate for styles */
  63.         error = css__stylesheet_style_create(c->sheet, &width_style);
  64.         if (error != CSS_OK)
  65.                 return error;
  66.  
  67.         error = css__stylesheet_style_create(c->sheet, &count_style);
  68.         if (error != CSS_OK) {
  69.                 css__stylesheet_style_destroy(width_style);
  70.                 return error;
  71.         }
  72.  
  73.         /* Attempt to parse the various longhand properties */
  74.         do {
  75.                 prev_ctx = *ctx;
  76.                 error = CSS_OK;
  77.  
  78.                 if (is_css_inherit(c, token)) {
  79.                         /* Can't have another inherit */
  80.                         error = CSS_INVALID;
  81.                         goto css__parse_columns_cleanup;
  82.                 }
  83.  
  84.                 /* Try each property parser in turn, but only if we
  85.                  * haven't already got a value for this property.
  86.                  */
  87.                 if ((width) &&
  88.                                 (error = css__parse_column_width(c, vector, ctx,
  89.                                                 width_style)) == CSS_OK) {
  90.                         width = false;
  91.                 } else if ((count) &&
  92.                                 (error = css__parse_column_count(c, vector, ctx,
  93.                                                 count_style)) == CSS_OK) {
  94.                         count = false;
  95.                 }
  96.  
  97.                 if (error == CSS_OK) {
  98.                         consumeWhitespace(vector, ctx);
  99.  
  100.                         token = parserutils_vector_peek(vector, *ctx);
  101.                 } else {
  102.                         /* Forcibly cause loop to exit */
  103.                         token = NULL;
  104.                 }
  105.         } while (*ctx != prev_ctx && token != NULL);
  106.  
  107.         /* Set unset properties to initial values */
  108.         if (width) {
  109.                 error = css__stylesheet_style_appendOPV(width_style,
  110.                                 CSS_PROP_COLUMN_WIDTH, 0,
  111.                                 COLUMN_WIDTH_AUTO);
  112.                 if (error != CSS_OK)
  113.                         goto css__parse_columns_cleanup;
  114.         }
  115.  
  116.         if (count) {
  117.                 error = css__stylesheet_style_appendOPV(count_style,
  118.                                 CSS_PROP_COLUMN_COUNT, 0,
  119.                                 COLUMN_COUNT_AUTO);
  120.                 if (error != CSS_OK)
  121.                         goto css__parse_columns_cleanup;
  122.         }
  123.  
  124.         /* Merge styles into the result */
  125.         error = css__stylesheet_merge_style(result, width_style);
  126.         if (error != CSS_OK)
  127.                 goto css__parse_columns_cleanup;
  128.  
  129.         error = css__stylesheet_merge_style(result, count_style);
  130.  
  131.  
  132. css__parse_columns_cleanup:
  133.         css__stylesheet_style_destroy(width_style);
  134.         css__stylesheet_style_destroy(count_style);
  135.  
  136.         if (error != CSS_OK)
  137.                 *ctx = orig_ctx;
  138.  
  139.         return error;
  140. }
  141.  
  142.