summaryrefslogtreecommitdiff
path: root/src/parse
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/properties/margin.c236
-rw-r--r--src/parse/properties/properties.c1
-rw-r--r--src/parse/properties/properties.h3
-rw-r--r--src/parse/propstrings.c1
-rw-r--r--src/parse/propstrings.h13
5 files changed, 248 insertions, 6 deletions
diff --git a/src/parse/properties/margin.c b/src/parse/properties/margin.c
index 7ab185c..76147f1 100644
--- a/src/parse/properties/margin.c
+++ b/src/parse/properties/margin.c
@@ -5,6 +5,7 @@
* Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
*/
+#include <assert.h>
#include <string.h>
#include "bytecode/bytecode.h"
@@ -17,6 +18,241 @@ static css_error parse_margin_side(css_language *c,
uint16_t op, css_style **result);
/**
+ * Parse margin shorthand
+ *
+ * \param c Parsing context
+ * \param vector Vector of tokens to process
+ * \param ctx Pointer to vector iteration context
+ * \param result Pointer to location to receive resulting style
+ * \return CSS_OK on success,
+ * CSS_NOMEM on memory exhaustion,
+ * CSS_INVALID if the input is not valid
+ *
+ * Post condition: \a *ctx is updated with the next token to process
+ * If the input is invalid, then \a *ctx remains unchanged.
+ */
+css_error parse_margin(css_language *c,
+ const parserutils_vector *vector, int *ctx,
+ css_style **result)
+{
+ int orig_ctx = *ctx;
+ int prev_ctx;
+ const css_token *token;
+ css_style *top = NULL;
+ css_style *right = NULL;
+ css_style *bottom = NULL;
+ css_style *left = NULL;
+ css_style *ret = NULL;
+ uint32_t num_sides = 0;
+ uint32_t required_size;
+ css_error error;
+
+ /* Firstly, handle inherit */
+ token = parserutils_vector_peek(vector, *ctx);
+ if (token != NULL && token->type == CSS_TOKEN_IDENT &&
+ token->ilower == c->strings[INHERIT]) {
+ uint32_t *bytecode;
+
+ error = css_stylesheet_style_create(c->sheet,
+ 4 * sizeof(uint32_t), &ret);
+ if (error != CSS_OK) {
+ *ctx = orig_ctx;
+ return error;
+ }
+
+ bytecode = (uint32_t *) ret->bytecode;
+
+ *(bytecode++) = buildOPV(CSS_PROP_MARGIN_TOP,
+ FLAG_INHERIT, 0);
+ *(bytecode++) = buildOPV(CSS_PROP_MARGIN_RIGHT,
+ FLAG_INHERIT, 0);
+ *(bytecode++) = buildOPV(CSS_PROP_MARGIN_BOTTOM,
+ FLAG_INHERIT, 0);
+ *(bytecode++) = buildOPV(CSS_PROP_MARGIN_LEFT,
+ FLAG_INHERIT, 0);
+
+ parserutils_vector_iterate(vector, ctx);
+
+ *result = ret;
+
+ return CSS_OK;
+ } else if (token == NULL) {
+ /* No tokens -- clearly garbage */
+ *ctx = orig_ctx;
+ return CSS_INVALID;
+ }
+
+ /* Attempt to parse up to 4 widths */
+ do {
+ prev_ctx = *ctx;
+ error = CSS_OK;
+
+ if (top == NULL &&
+ (error = parse_margin_side(c, vector, ctx,
+ CSS_PROP_MARGIN_TOP, &top)) == CSS_OK) {
+ num_sides = 1;
+ } else if (right == NULL &&
+ (error = parse_margin_side(c, vector, ctx,
+ CSS_PROP_MARGIN_RIGHT, &right)) == CSS_OK) {
+ num_sides = 2;
+ } else if (bottom == NULL &&
+ (error = parse_margin_side(c, vector, ctx,
+ CSS_PROP_MARGIN_BOTTOM, &bottom)) == CSS_OK) {
+ num_sides = 3;
+ } else if (left == NULL &&
+ (error = parse_margin_side(c, vector, ctx,
+ CSS_PROP_MARGIN_LEFT, &left)) == CSS_OK) {
+ num_sides = 4;
+ }
+
+ if (error == CSS_OK) {
+ consumeWhitespace(vector, ctx);
+
+ token = parserutils_vector_peek(vector, *ctx);
+ } else {
+ /* Forcibly cause loop to exit */
+ token = NULL;
+ }
+ } while (*ctx != prev_ctx && token != NULL);
+
+ if (num_sides == 0) {
+ error = CSS_INVALID;
+ goto cleanup;
+ }
+
+ /* Calculate size of resultant style */
+ if (num_sides == 1) {
+ required_size = 4 * top->length;
+ } else if (num_sides == 2) {
+ required_size = 2 * top->length + 2 * right->length;
+ } else if (num_sides == 3) {
+ required_size = top->length + 2 * right->length +
+ bottom->length;
+ } else {
+ required_size = top->length + right->length +
+ bottom->length + left->length;
+ }
+
+ error = css_stylesheet_style_create(c->sheet, required_size, &ret);
+ if (error != CSS_OK)
+ goto cleanup;
+
+ required_size = 0;
+
+ if (num_sides == 1) {
+ uint32_t *opv = ((uint32_t *) top->bytecode);
+ uint8_t flags = getFlags(*opv);
+ uint16_t value = getValue(*opv);
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ *opv = buildOPV(CSS_PROP_MARGIN_RIGHT, flags, value);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ *opv = buildOPV(CSS_PROP_MARGIN_BOTTOM, flags, value);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ *opv = buildOPV(CSS_PROP_MARGIN_LEFT, flags, value);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+ } else if (num_sides == 2) {
+ uint32_t *vopv = ((uint32_t *) top->bytecode);
+ uint32_t *hopv = ((uint32_t *) right->bytecode);
+ uint8_t vflags = getFlags(*vopv);
+ uint8_t hflags = getFlags(*hopv);
+ uint16_t vvalue = getValue(*vopv);
+ uint16_t hvalue = getValue(*hopv);
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ right->bytecode, right->length);
+ required_size += right->length;
+
+ *vopv = buildOPV(CSS_PROP_MARGIN_BOTTOM, vflags, vvalue);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ *hopv = buildOPV(CSS_PROP_MARGIN_LEFT, hflags, hvalue);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ right->bytecode, right->length);
+ required_size += right->length;
+ } else if (num_sides == 3) {
+ uint32_t *opv = ((uint32_t *) right->bytecode);
+ uint8_t flags = getFlags(*opv);
+ uint16_t value = getValue(*opv);
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ right->bytecode, right->length);
+ required_size += right->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ bottom->bytecode, bottom->length);
+ required_size += bottom->length;
+
+ *opv = buildOPV(CSS_PROP_MARGIN_LEFT, flags, value);
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ right->bytecode, right->length);
+ required_size += right->length;
+ } else {
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ top->bytecode, top->length);
+ required_size += top->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ right->bytecode, right->length);
+ required_size += right->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ bottom->bytecode, bottom->length);
+ required_size += bottom->length;
+
+ memcpy(((uint8_t *) ret->bytecode) + required_size,
+ left->bytecode, left->length);
+ required_size += left->length;
+ }
+
+ assert(required_size == ret->length);
+
+ /* Write the result */
+ *result = ret;
+ /* Invalidate ret, so that cleanup doesn't destroy it */
+ ret = NULL;
+
+ /* Clean up after ourselves */
+cleanup:
+ if (top)
+ css_stylesheet_style_destroy(c->sheet, top);
+ if (right)
+ css_stylesheet_style_destroy(c->sheet, right);
+ if (bottom)
+ css_stylesheet_style_destroy(c->sheet, bottom);
+ if (left)
+ css_stylesheet_style_destroy(c->sheet, left);
+ if (ret)
+ css_stylesheet_style_destroy(c->sheet, ret);
+
+ if (error != CSS_OK)
+ *ctx = orig_ctx;
+
+ return error;
+}
+
+/**
* Parse margin-bottom
*
* \param c Parsing context
diff --git a/src/parse/properties/properties.c b/src/parse/properties/properties.c
index 0fd5747..28b92b7 100644
--- a/src/parse/properties/properties.c
+++ b/src/parse/properties/properties.c
@@ -72,6 +72,7 @@ const css_prop_handler property_handlers[LAST_PROP + 1 - FIRST_PROP] =
parse_list_style_image,
parse_list_style_position,
parse_list_style_type,
+ parse_margin,
parse_margin_bottom,
parse_margin_left,
parse_margin_right,
diff --git a/src/parse/properties/properties.h b/src/parse/properties/properties.h
index 976766d..7c7268a 100644
--- a/src/parse/properties/properties.h
+++ b/src/parse/properties/properties.h
@@ -202,6 +202,9 @@ css_error parse_list_style_position(css_language *c,
css_error parse_list_style_type(css_language *c,
const parserutils_vector *vector, int *ctx,
css_style **result);
+css_error parse_margin(css_language *c,
+ const parserutils_vector *vector, int *ctx,
+ css_style **result);
css_error parse_margin_bottom(css_language *c,
const parserutils_vector *vector, int *ctx,
css_style **result);
diff --git a/src/parse/propstrings.c b/src/parse/propstrings.c
index 9e26271..213595d 100644
--- a/src/parse/propstrings.c
+++ b/src/parse/propstrings.c
@@ -102,6 +102,7 @@ const stringmap_entry stringmap[LAST_KNOWN] = {
{ "list-style-image", SLEN("list-style-image") },
{ "list-style-position", SLEN("list-style-position") },
{ "list-style-type", SLEN("list-style-type") },
+ { "margin", SLEN("margin") },
{ "margin-bottom", SLEN("margin-bottom") },
{ "margin-left", SLEN("margin-left") },
{ "margin-right", SLEN("margin-right") },
diff --git a/src/parse/propstrings.h b/src/parse/propstrings.h
index 68b4b53..bff7c4f 100644
--- a/src/parse/propstrings.h
+++ b/src/parse/propstrings.h
@@ -43,12 +43,13 @@ enum {
CLIP, COLOR, CONTENT, COUNTER_INCREMENT, COUNTER_RESET, CUE, CUE_AFTER,
CUE_BEFORE, CURSOR, DIRECTION, DISPLAY, ELEVATION, EMPTY_CELLS, FLOAT,
FONT, FONT_FAMILY, FONT_SIZE, FONT_STYLE, FONT_VARIANT, FONT_WEIGHT,
- HEIGHT, LEFT, LETTER_SPACING, LINE_HEIGHT, LIST_STYLE,
- LIST_STYLE_IMAGE, LIST_STYLE_POSITION, LIST_STYLE_TYPE, MARGIN_BOTTOM,
- MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MAX_HEIGHT, MAX_WIDTH,
- MIN_HEIGHT, MIN_WIDTH, ORPHANS, OUTLINE, OUTLINE_COLOR, OUTLINE_STYLE,
- OUTLINE_WIDTH, OVERFLOW, PADDING_BOTTOM, PADDING_LEFT, PADDING_RIGHT,
- PADDING_TOP, PAGE_BREAK_AFTER, PAGE_BREAK_BEFORE, PAGE_BREAK_INSIDE,
+ HEIGHT, LEFT, LETTER_SPACING, LINE_HEIGHT,
+ LIST_STYLE, LIST_STYLE_IMAGE, LIST_STYLE_POSITION, LIST_STYLE_TYPE,
+ MARGIN, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP,
+ MAX_HEIGHT, MAX_WIDTH, MIN_HEIGHT, MIN_WIDTH, ORPHANS,
+ OUTLINE, OUTLINE_COLOR, OUTLINE_STYLE, OUTLINE_WIDTH, OVERFLOW,
+ PADDING_BOTTOM, PADDING_LEFT, PADDING_RIGHT, PADDING_TOP,
+ PAGE_BREAK_AFTER, PAGE_BREAK_BEFORE, PAGE_BREAK_INSIDE,
PAUSE, PAUSE_AFTER, PAUSE_BEFORE, PITCH_RANGE, PITCH, PLAY_DURING,
POSITION, QUOTES, RICHNESS, RIGHT, SPEAK_HEADER, SPEAK_NUMERAL,
SPEAK_PUNCTUATION, SPEAK, SPEECH_RATE, STRESS, TABLE_LAYOUT,