1 /*
2  * Licensed under the MIT License,
3  *		  http://www.opensource.org/licenses/mit-license.php
4  * Copyright 2010 The NetSurf Browser Project.
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include "bytecode/bytecode.h"
11 #include "bytecode/opcodes.h"
12 #include "parse/properties/properties.h"
13 #include "parse/properties/utils.h"
14 
15 /**
16  * Parse opacity
17  *
18  * \param c	  Parsing context
19  * \param vector  Vector of tokens to process
20  * \param ctx	  Pointer to vector iteration context
21  * \param result  resulting style
22  * \return CSS_OK on success,
23  *	   CSS_NOMEM on memory exhaustion,
24  *	   CSS_INVALID if the input is not valid
25  *
26  * Post condition: \a *ctx is updated with the next token to process
27  *		   If the input is invalid, then \a *ctx remains unchanged.
28  */
css__parse_opacity(css_language * c,const parserutils_vector * vector,int * ctx,css_style * result)29 css_error css__parse_opacity(css_language *c,
30 		const parserutils_vector *vector, int *ctx,
31 		css_style *result)
32 {
33 	int orig_ctx = *ctx;
34 	css_error error;
35 	const css_token *token;
36 	bool match;
37 
38 	token = parserutils_vector_iterate(vector, ctx);
39 	if ((token == NULL) || ((token->type != CSS_TOKEN_IDENT) && (token->type != CSS_TOKEN_NUMBER))) {
40 		*ctx = orig_ctx;
41 		return CSS_INVALID;
42 	}
43 
44 	if ((token->type == CSS_TOKEN_IDENT) && (lwc_string_caseless_isequal(token->idata, c->strings[INHERIT], &match) == lwc_error_ok && match)) {
45 			error = css_stylesheet_style_inherit(result, CSS_PROP_OPACITY);
46 	} else if (token->type == CSS_TOKEN_NUMBER) {
47 		css_fixed num = 0;
48 		size_t consumed = 0;
49 
50 		num = css__number_from_lwc_string(token->idata, false, &consumed);
51 		/* Invalid if there are trailing characters */
52 		if (consumed != lwc_string_length(token->idata)) {
53 			*ctx = orig_ctx;
54 			return CSS_INVALID;
55 		}
56 
57 		/* Clamp to range [0,1] */
58 		if (num < 0)
59 			num = 0;
60 		if (num > INTTOFIX(1))
61 			num = INTTOFIX(1);
62 
63 		error = css__stylesheet_style_appendOPV(result, CSS_PROP_OPACITY, 0, OPACITY_SET);
64 		if (error != CSS_OK) {
65 			*ctx = orig_ctx;
66 			return error;
67 		}
68 
69 		error = css__stylesheet_style_append(result, num);
70 	} else {
71 		error = CSS_INVALID;
72 	}
73 
74 	if (error != CSS_OK)
75 		*ctx = orig_ctx;
76 
77 	return error;
78 }
79 
80