~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/css_parser/src/webutil/css/value.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Copyright 2010 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Copyright 2006 Google Inc. All Rights Reserved.
18
 
// Author: dpeng@google.com (Daniel Peng)
19
 
 
20
 
#ifndef WEBUTIL_CSS_VALUE_H__
21
 
#define WEBUTIL_CSS_VALUE_H__
22
 
 
23
 
#include <string>
24
 
#include <vector>
25
 
 
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"
34
 
 
35
 
// resolving conflict on macro HZ defined elsewhere.
36
 
#ifdef HZ
37
 
const int HZ_temporary = HZ;
38
 
#undef HZ
39
 
const int HZ = HZ_temporary;
40
 
#endif
41
 
 
42
 
namespace Css {
43
 
 
44
 
class FunctionParameters;
45
 
class Values;
46
 
 
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
52
 
// various accessors.
53
 
class Value {
54
 
 public:
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,
59
 
              NUM_UNITS };
60
 
 
61
 
  // These constructors generate Values of various types.
62
 
 
63
 
  Value() : type_(DEFAULT), color_(0, 0, 0) { }
64
 
 
65
 
  // UNKNOWN or DEFAULT
66
 
  Value(ValueType ty);  // NOLINT
67
 
 
68
 
  // NUMBER with unit.  OTHER is not a valid unit here.  Use the next form:
69
 
  Value(double num, Unit unit);
70
 
 
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);
74
 
 
75
 
  // Any of the string types (URI, STRING). For IDENT, use the next
76
 
  // constructor instead.
77
 
  Value(ValueType ty, const UnicodeText& str);
78
 
 
79
 
  // IDENT from an identifier.
80
 
  explicit Value(const Identifier& identifier);
81
 
  explicit Value(const Identifier::Ident ident);
82
 
 
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);
87
 
 
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);
92
 
 
93
 
  // COLOR.
94
 
  explicit Value(HtmlColor color);
95
 
 
96
 
  // copy constructor and assignment operator
97
 
  Value(const Value& other);
98
 
  Value& operator=(const Value& other);
99
 
 
100
 
  // equality.
101
 
  bool Equals(const Value& other) const;
102
 
 
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);
112
 
 
113
 
  // Returns a string representation of the value.
114
 
  string ToString() const;
115
 
 
116
 
  // Accessors.  Modeled after
117
 
  // http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/LexicalUnit.html
118
 
 
119
 
  ValueType GetLexicalUnitType() const;  // The type of value
120
 
 
121
 
  // Each of these accessors is only valid for certain types.  The
122
 
  // comment indicates for which types they are valid; we DCHECK this
123
 
  // precondition.
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
137
 
 
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_;
145
 
  }
146
 
  void set_bytes_in_original_buffer(const StringPiece& bytes) {
147
 
    bytes.CopyToString(&bytes_in_original_buffer_);
148
 
  }
149
 
 
150
 
 private:
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
158
 
 
159
 
  string bytes_in_original_buffer_;
160
 
 
161
 
  // kDimensionUnitText stores the name of each unit (see TextFromUnit)
162
 
  static const char* const kDimensionUnitText[];
163
 
};
164
 
 
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*> {
171
 
 public:
172
 
  Values() : std::vector<Value*>() { }
173
 
  ~Values();
174
 
 
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]; }
178
 
 
179
 
  string ToString() const;
180
 
 private:
181
 
  DISALLOW_COPY_AND_ASSIGN(Values);
182
 
};
183
 
 
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.
187
 
//
188
 
// Ex: -webkit-gradient(radial, 430 50, 0, 430 50, 252, from(red), to(#000))
189
 
// Neither
190
 
//   -webkit-gradient(radial, 430, 50, 0, 430, 50, 252, from(red), to(#000))
191
 
// nor
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.
194
 
//
195
 
// FunctionParameters will delete all of its stored Value*'s on destruction.
196
 
class FunctionParameters {
197
 
 public:
198
 
  enum Separator {
199
 
    COMMA_SEPARATED,
200
 
    SPACE_SEPARATED,
201
 
  };
202
 
 
203
 
  FunctionParameters() : values_(new Values) {}
204
 
  ~FunctionParameters();
205
 
 
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);
209
 
 
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); }
213
 
  int size() const {
214
 
    DCHECK_EQ(separators_.size(), values_->size());
215
 
    return values_->size();
216
 
  }
217
 
 
218
 
  bool Equals(const FunctionParameters& other) const;
219
 
  void Copy(const FunctionParameters& other);
220
 
 
221
 
  string ToString() const;
222
 
 
223
 
 private:
224
 
  std::vector<Separator> separators_;
225
 
  scoped_ptr<Values> values_;
226
 
 
227
 
  DISALLOW_COPY_AND_ASSIGN(FunctionParameters);
228
 
};
229
 
 
230
 
}  // namespace
231
 
 
232
 
#endif  // WEBUTIL_CSS_VALUE_H__