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 2012 Google Inc. All Rights Reserved.
18
// Author: sligocki@google.com (Shawn Ligocki)
20
// Classes for storing CSS3 @media queries.
21
// See: http://www.w3.org/TR/css3-mediaqueries/
23
#ifndef WEBUTIL_CSS_MEDIA_H_
24
#define WEBUTIL_CSS_MEDIA_H_
29
#include "base/css_macros.h"
30
#include "util/utf8/public/unicodetext.h"
36
// Classes named roughly after CSS3 @media query syntax names.
37
// From http://www.w3.org/TR/css3-mediaqueries/#syntax
40
// : S* [media_query [ ',' S* media_query ]* ]?
43
// : [ONLY | NOT]? S* media_type S* [ AND S* expression ]*
44
// | expression [ AND S* expression ]*
50
// : '(' S* media_feature S* [ ':' S* expr ]? ')' S*
56
// Ex: (max-width: 500px)
57
class MediaExpression {
59
// Media feature without a value. Ex: (color).
60
explicit MediaExpression(const UnicodeText& name)
61
: name_(name), has_value_(false) {}
62
// Media feature with value. Ex: (max-width: 500px).
63
MediaExpression(const UnicodeText& name, const UnicodeText& value)
64
: name_(name), has_value_(true), value_(value) {}
67
const UnicodeText& name() const { return name_; }
68
bool has_value() const { return has_value_; }
69
const UnicodeText& value() const { return value_; }
71
MediaExpression* DeepCopy() const;
72
string ToString() const;
77
// Unparsed value. TODO(sligocki): Actually parse it?
80
DISALLOW_COPY_AND_ASSIGN(MediaExpression);
83
// Ex: (max-width: 500px) and (color)
84
class MediaExpressions : public std::vector<MediaExpression*> {
86
MediaExpressions() : std::vector<MediaExpression*>() {}
89
string ToString() const;
92
DISALLOW_COPY_AND_ASSIGN(MediaExpressions);
95
// Ex: not screen and (max-width: 500px) and (color)
98
MediaQuery() : qualifier_(NO_QUALIFIER) {}
101
enum MediaQualifier { ONLY, NOT, NO_QUALIFIER };
102
MediaQualifier qualifier() const { return qualifier_; }
103
const UnicodeText& media_type() const { return media_type_; }
104
const MediaExpressions& expressions() const { return expressions_; }
105
const MediaExpression& expression(int i) const { return *expressions_[i]; }
107
void set_qualifier(MediaQualifier q) { qualifier_ = q; }
108
void set_media_type(const UnicodeText& m) { media_type_ = m; }
109
// Takes ownership of |expression|.
110
void add_expression(MediaExpression* expression) {
111
expressions_.push_back(expression);
114
MediaQuery* DeepCopy() const;
115
string ToString() const;
118
MediaQualifier qualifier_;
119
UnicodeText media_type_;
120
MediaExpressions expressions_;
122
DISALLOW_COPY_AND_ASSIGN(MediaQuery);
125
// Ex: not screen and (max-width: 500px), projection and (color)
126
class MediaQueries : public std::vector<MediaQuery*> {
128
MediaQueries() : std::vector<MediaQuery*>() {}
131
// Like clear(), but takes care of memory management as well.
134
MediaQueries* DeepCopy() const;
135
string ToString() const;
138
DISALLOW_COPY_AND_ASSIGN(MediaQueries);
143
#endif // WEBUTIL_CSS_MEDIA_H_