2
* Copyright 2010 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Copyright 2006 Google Inc. All Rights Reserved.
18
// Author: dpeng@google.com (Daniel Peng)
20
#ifndef WEBUTIL_CSS_VALUE_H__
21
#define WEBUTIL_CSS_VALUE_H__
26
#include "base/logging.h"
27
#include "base/css_macros.h"
28
#include "base/scoped_ptr.h"
29
#include "strings/stringpiece.h"
30
#include "util/utf8/public/unicodetext.h"
31
#include "webutil/css/identifier.h"
32
#include "webutil/css/string.h"
33
#include "webutil/html/htmlcolor.h"
35
// resolving conflict on macro HZ defined elsewhere.
37
const int HZ_temporary = HZ;
39
const int HZ = HZ_temporary;
44
class FunctionParameters;
47
// A Value represents a value in CSS (maybe more generally, a
48
// lexical unit). There are many different types of these, so you can
49
// think of a Value as a tagged union of various values. The tag
50
// is set by the constructor and accessed with GetLexicalUnitType().
51
// The values are also set by the constructor and accessed with the
55
enum ValueType { NUMBER, URI, FUNCTION, RECT,
56
COLOR, STRING, IDENT, UNKNOWN, DEFAULT };
57
enum Unit { EM, EX, PX, CM, MM, IN, PT, PC,
58
DEG, RAD, GRAD, MS, S, HZ, KHZ, PERCENT, OTHER, NO_UNIT,
61
// These constructors generate Values of various types.
63
Value() : type_(DEFAULT), color_(0, 0, 0) { }
66
Value(ValueType ty); // NOLINT
68
// NUMBER with unit. OTHER is not a valid unit here. Use the next form:
69
Value(double num, Unit unit);
71
// NUMBER with unit; we convert unit to an enum for you. If it's
72
// not a known unit, we use the OTHER enum and save the text.
73
Value(double num, const UnicodeText& unit);
75
// Any of the string types (URI, STRING). For IDENT, use the next
76
// constructor instead.
77
Value(ValueType ty, const UnicodeText& str);
79
// IDENT from an identifier.
80
explicit Value(const Identifier& identifier);
81
explicit Value(const Identifier::Ident ident);
83
// Any of the special function types (RECT)
84
// NOTE: The ownership of params will be taken.
85
// params cannot be NULL, if no parameters are needed, pass an empty Values.
86
explicit Value(ValueType ty, FunctionParameters* params);
88
// FUNCTION with name func
89
// NOTE: The ownership of params will be taken.
90
// params cannot be NULL, if no parameters are needed, pass an empty Values.
91
explicit Value(const UnicodeText& func, FunctionParameters* params);
94
explicit Value(HtmlColor color);
96
// copy constructor and assignment operator
97
Value(const Value& other);
98
Value& operator=(const Value& other);
101
bool Equals(const Value& other) const;
103
// Given the text of a CSS unit, UnitFromText returns the
104
// corresponding enum. If no such unit is found, UnitFromText
105
// returns OTHER. Since all CSS units are ASCII, we are happy with
106
// ASCII, UTF8, Latin-1, etc.
107
static Unit UnitFromText(const char* s, int len);
108
// Given a unit, returns its string representation. If u is
109
// NO_UNIT, returns "". If u is OTHER, we return "OTHER", but this
110
// may not be what you want.
111
static const char* TextFromUnit(Unit u);
113
// Returns a string representation of the value.
114
string ToString() const;
116
// Accessors. Modeled after
117
// http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/LexicalUnit.html
119
ValueType GetLexicalUnitType() const; // The type of value
121
// Each of these accessors is only valid for certain types. The
122
// comment indicates for which types they are valid; we DCHECK this
124
string GetDimensionUnitText() const; // NUMBER: the unit as a string.
125
Unit GetDimension() const; // NUMBER: the unit.
126
int GetIntegerValue() const; // NUMBER: the integer value.
127
double GetFloatValue() const; // NUMBER: the float value.
128
// FUNCTION: the function parameter values (ignoring separators).
129
const Values* GetParameters() const;
130
// FUNCITON: the function parameters with separator information.
131
const FunctionParameters* GetParametersWithSeparators() const;
132
const UnicodeText& GetFunctionName() const; // FUNCTION: the function name.
133
const UnicodeText& GetStringValue() const; // URI, STRING: the string value
134
UnicodeText GetIdentifierText() const; // IDENT: the ident as a string.
135
const Identifier& GetIdentifier() const; // IDENT: identifier.
136
const HtmlColor& GetColorValue() const; // COLOR: the color value
138
// Verbatim bytes parsed for the declaration. Only stored for some Values.
139
// Only available using preservation-mode parsing and only stored for things
140
// like strings and numbers where the original contents may not be fully
141
// recoverable after value parsing.
142
// Note: May be invalid UTF8.
143
StringPiece bytes_in_original_buffer() const {
144
return bytes_in_original_buffer_;
146
void set_bytes_in_original_buffer(const StringPiece& bytes) {
147
bytes.CopyToString(&bytes_in_original_buffer_);
151
ValueType type_; // indicates the type of value. Always valid.
152
double num_; // for NUMBER (integer values are stored as doubles)
153
Unit unit_; // for NUMBER
154
Identifier identifier_; // for IDENT
155
UnicodeText str_; // for NUMBER (OTHER unit_), URI, STRING, FUNCTION
156
scoped_ptr<FunctionParameters> params_; // FUNCTION and RECT params
157
HtmlColor color_; // COLOR
159
string bytes_in_original_buffer_;
161
// kDimensionUnitText stores the name of each unit (see TextFromUnit)
162
static const char* const kDimensionUnitText[];
165
// Values is a vector of Value*, which we own and will delete
166
// upon destruction. If you remove elements from Values, you are
167
// responsible for deleting them.
168
// Also, be careful --- there's no virtual destructor, so this must be
169
// deleted as a Values.
170
class Values : public std::vector<Value*> {
172
Values() : std::vector<Value*>() { }
175
// We provide syntactic sugar for accessing elements.
176
// values->get(i) looks better than (*values)[i])
177
const Value* get(int i) const { return (*this)[i]; }
179
string ToString() const;
181
DISALLOW_COPY_AND_ASSIGN(Values);
184
// FunctionParameters stores all values and separators between them from
185
// a parsed function. Functions may have comma and space separation
186
// interspersed throughout and it matters which was used.
188
// Ex: -webkit-gradient(radial, 430 50, 0, 430 50, 252, from(red), to(#000))
190
// -webkit-gradient(radial, 430, 50, 0, 430, 50, 252, from(red), to(#000))
192
// -webkit-gradient(radial 430 50 0 430 50 252 from(red) to(#000))
193
// are interpretted correctly. Only the original mix of spaces and commas.
195
// FunctionParameters will delete all of its stored Value*'s on destruction.
196
class FunctionParameters {
203
FunctionParameters() : values_(new Values) {}
204
~FunctionParameters();
206
// Add a value and the separator that preceeded it.
207
// If this is the first value, separator is ignored.
208
void AddSepValue(Separator separator, Value* value);
210
Separator separator(int i) const { return separators_[i]; }
211
const Values* values() const { return values_.get(); }
212
const Value* value(int i) const { return values_->at(i); }
214
DCHECK_EQ(separators_.size(), values_->size());
215
return values_->size();
218
bool Equals(const FunctionParameters& other) const;
219
void Copy(const FunctionParameters& other);
221
string ToString() const;
224
std::vector<Separator> separators_;
225
scoped_ptr<Values> values_;
227
DISALLOW_COPY_AND_ASSIGN(FunctionParameters);
232
#endif // WEBUTIL_CSS_VALUE_H__