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 column-rule 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_column_rule(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;
  38.         bool color = true;
  39.         bool style = true;
  40.         bool width = true;
  41.         css_style *color_style;
  42.         css_style *style_style;
  43.         css_style *width_style;
  44.  
  45.         /* Firstly, handle inherit */
  46.         token = parserutils_vector_peek(vector, *ctx);
  47.         if (token == NULL)
  48.                 return CSS_INVALID;
  49.  
  50.         if (is_css_inherit(c, token)) {
  51.                 error = css_stylesheet_style_inherit(result,
  52.                                 CSS_PROP_COLUMN_RULE_COLOR);
  53.                 if (error != CSS_OK)
  54.                         return error;
  55.  
  56.                 error = css_stylesheet_style_inherit(result,
  57.                                 CSS_PROP_COLUMN_RULE_STYLE);
  58.                 if (error != CSS_OK)
  59.                         return error;
  60.  
  61.                 error = css_stylesheet_style_inherit(result,
  62.                                 CSS_PROP_COLUMN_RULE_WIDTH);
  63.  
  64.                 if (error == CSS_OK)
  65.                         parserutils_vector_iterate(vector, ctx);
  66.  
  67.                 return error;
  68.         }
  69.  
  70.         /* Allocate for styles */
  71.         error = css__stylesheet_style_create(c->sheet, &color_style);
  72.         if (error != CSS_OK)
  73.                 return error;
  74.  
  75.         error = css__stylesheet_style_create(c->sheet, &style_style);
  76.         if (error != CSS_OK) {
  77.                 css__stylesheet_style_destroy(color_style);
  78.                 return error;
  79.         }
  80.  
  81.         error = css__stylesheet_style_create(c->sheet, &width_style);
  82.         if (error != CSS_OK) {
  83.                 css__stylesheet_style_destroy(color_style);
  84.                 css__stylesheet_style_destroy(style_style);
  85.                 return error;
  86.         }
  87.  
  88.         /* Attempt to parse the various longhand properties */
  89.         do {
  90.                 prev_ctx = *ctx;
  91.                 error = CSS_OK;
  92.  
  93.                 /* Ensure that we're not about to parse another inherit */
  94.                 token = parserutils_vector_peek(vector, *ctx);
  95.                 if (token != NULL && is_css_inherit(c, token)) {
  96.                         error = CSS_INVALID;
  97.                         goto css__parse_column_rule_cleanup;
  98.                 }
  99.  
  100.                 if ((color) &&
  101.                                 (error = css__parse_column_rule_color(c, vector,
  102.                                                 ctx, color_style)) == CSS_OK) {
  103.                         color = false;
  104.                 } else if ((style) &&
  105.                                 (error = css__parse_column_rule_style(c, vector,
  106.                                                 ctx, style_style)) == CSS_OK) {
  107.                         style = false;
  108.                 } else if ((width) &&
  109.                                 (error = css__parse_column_rule_width(c, vector,
  110.                                                 ctx, width_style)) == CSS_OK) {
  111.                         width = false;
  112.                 }
  113.  
  114.                 if (error == CSS_OK) {
  115.                         consumeWhitespace(vector, ctx);
  116.  
  117.                         token = parserutils_vector_peek(vector, *ctx);
  118.                 } else {
  119.                         /* Forcibly cause loop to exit */
  120.                         token = NULL;
  121.                 }
  122.         } while (*ctx != prev_ctx && token != NULL);
  123.  
  124.         /* Set unset properties to initial values */
  125.         if (color) {
  126.                 error = css__stylesheet_style_appendOPV(color_style,
  127.                                 CSS_PROP_COLUMN_RULE_COLOR, 0,
  128.                                 COLUMN_RULE_COLOR_SET);
  129.                 if (error != CSS_OK)
  130.                         goto css__parse_column_rule_cleanup;
  131.  
  132.                 /** TODO: initial colour should be the UA-defined initial
  133.                  *        value of the "color" property, not "0" */
  134.                 error = css__stylesheet_style_append(color_style, 0x00000000);
  135.                 if (error != CSS_OK)
  136.                         goto css__parse_column_rule_cleanup;
  137.         }
  138.  
  139.         if (style) {
  140.                 error = css__stylesheet_style_appendOPV(style_style,
  141.                                 CSS_PROP_COLUMN_RULE_STYLE, 0,
  142.                                 COLUMN_RULE_STYLE_NONE);
  143.                 if (error != CSS_OK)
  144.                         goto css__parse_column_rule_cleanup;
  145.         }
  146.  
  147.         if (width) {
  148.                 error = css__stylesheet_style_appendOPV(width_style,
  149.                                 CSS_PROP_COLUMN_RULE_WIDTH, 0,
  150.                                 COLUMN_RULE_WIDTH_MEDIUM);
  151.                 if (error != CSS_OK)
  152.                         goto css__parse_column_rule_cleanup;
  153.         }
  154.  
  155.         /* Merge styles into the result */
  156.         error = css__stylesheet_merge_style(result, color_style);
  157.         if (error != CSS_OK)
  158.                 goto css__parse_column_rule_cleanup;
  159.  
  160.         error = css__stylesheet_merge_style(result, style_style);
  161.         if (error != CSS_OK)
  162.                 goto css__parse_column_rule_cleanup;
  163.  
  164.         error = css__stylesheet_merge_style(result, width_style);
  165.  
  166.  
  167. css__parse_column_rule_cleanup:
  168.  
  169.         css__stylesheet_style_destroy(width_style);
  170.         css__stylesheet_style_destroy(style_style);
  171.         css__stylesheet_style_destroy(color_style);
  172.  
  173.         if (error != CSS_OK)
  174.                 *ctx = orig_ctx;
  175.  
  176.         return error;
  177. }
  178.