Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
css_hierarchy.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 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 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_HIERARCHY_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_HIERARCHY_H_
21 
22 #include <vector>
23 
31 
32 namespace Css {
33 class Stylesheet;
34 }
35 
36 namespace net_instaweb {
37 
38 class CssFilter;
39 class MessageHandler;
40 
64 class CssHierarchy {
65  public:
66  static const char kFailureReasonPrefix[];
67 
70  explicit CssHierarchy(CssFilter* filter);
71  ~CssHierarchy();
72 
76  void InitializeRoot(const GoogleUrl& css_base_url,
77  const GoogleUrl& css_trim_url,
78  const StringPiece input_contents,
79  bool has_unparseables,
80  int64 flattened_result_limit,
81  Css::Stylesheet* stylesheet,
82  MessageHandler* message_handler);
83 
85  bool NeedsRewriting() const {
86  return flattening_succeeded_ && !url_.empty();
87  }
88 
89  const StringPiece url() const { return url_; }
90  const StringPiece url_for_humans() const {
91  return (url_.empty() ? "inline"
92  : IsDataUrl(url_) ? "data URL"
93  : url_);
94  }
95 
96  const GoogleUrl& css_base_url() const { return css_base_url_; }
97  const GoogleUrl& css_trim_url() const { return css_trim_url_; }
98 
103  return (input_contents_resolved_ ? css_trim_url_ : css_base_url_);
104  }
105 
106  const Css::Stylesheet* stylesheet() const { return stylesheet_.get(); }
107  Css::Stylesheet* mutable_stylesheet() { return stylesheet_.get(); }
108  void set_stylesheet(Css::Stylesheet* stylesheet);
109 
110  const StringPiece input_contents() const { return input_contents_; }
113  void set_input_contents(const StringPiece input_contents) {
114  input_contents_ = input_contents;
115  }
116 
117  GoogleString* input_contents_backing_store() {
118  return &input_contents_backing_store_;
119  }
120  void set_input_contents_to_backing_store() {
121  input_contents_ = input_contents_backing_store_;
122  }
123 
124  const GoogleString& minified_contents() const { return minified_contents_; }
125  void set_minified_contents(const StringPiece minified_contents);
126 
127  const GoogleString& charset() const { return charset_; }
128  GoogleString* mutable_charset() { return &charset_; }
129 
130  const GoogleString& charset_source() const { return charset_source_; }
131 
132  const StringVector& media() const { return media_; }
133  StringVector* mutable_media() { return &media_; }
134 
136  const std::vector<CssHierarchy*>& children() const { return children_; }
137  std::vector<CssHierarchy*>& children() { return children_; }
138 
139  bool input_contents_resolved() const { return input_contents_resolved_; }
140  void set_input_contents_resolved(bool x) { input_contents_resolved_ = x; }
141 
142  bool flattening_succeeded() const { return flattening_succeeded_; }
143  void set_flattening_succeeded(bool ok) { flattening_succeeded_ = ok; }
144 
145  const GoogleString& flattening_failure_reason() const {
146  return flattening_failure_reason_;
147  }
153  void AddFlatteningFailureReason(const GoogleString& reason);
154 
155  bool unparseable_detected() const { return unparseable_detected_; }
156  void set_unparseable_detected(bool ok) { unparseable_detected_ = ok; }
157 
158  int64 flattened_result_limit() const { return flattened_result_limit_; }
159  void set_flattened_result_limit(int64 x) { flattened_result_limit_ = x; }
160 
173  bool CheckCharsetOk(const ResourcePtr& resource,
174  GoogleString* failure_reason);
175 
181  bool Parse();
182 
195  bool ExpandChildren();
196 
210  void RollUpContents();
211 
226  bool RollUpStylesheets();
227 
228  private:
229  friend class CssHierarchyTest;
230 
234  void InitializeNested(const CssHierarchy& parent,
235  const GoogleUrl& import_url);
236 
238  void ResizeChildren(int n);
239 
243  bool IsRecursive() const;
244 
250  bool DetermineImportMedia(const StringVector& containing_media,
251  const StringVector& import_media);
252 
259  bool DetermineRulesetMedia(StringVector* ruleset_media);
260 
262  CssFilter* filter_;
263 
266  StringPiece url_;
267 
269  GoogleUrl css_base_url_;
270 
273  GoogleUrl css_trim_url_;
274 
277  const CssHierarchy* parent_;
278 
282  std::vector<CssHierarchy*> children_;
283 
285  StringPiece input_contents_;
286 
292  GoogleString input_contents_backing_store_;
293 
295  GoogleString minified_contents_;
296 
300  scoped_ptr<Css::Stylesheet> stylesheet_;
301 
304  GoogleString charset_;
305 
307  GoogleString charset_source_;
308 
314  StringVector media_;
315 
320  bool input_contents_resolved_;
321 
325  bool flattening_succeeded_;
326 
329  GoogleString flattening_failure_reason_;
330 
332  bool unparseable_detected_;
333 
342  int64 flattened_result_limit_;
343 
345  MessageHandler* message_handler_;
346 
347 
348 };
349 
350 }
351 
352 #endif
class GoogleUrl
Definition: google_url.h:58
bool NeedsRewriting() const
A hierarchy needs rewriting only if it has an import to read and expand.
Definition: css_hierarchy.h:85
Definition: css_hierarchy.h:64
bool CheckCharsetOk(const ResourcePtr &resource, GoogleString *failure_reason)
const std::vector< CssHierarchy * > & children() const
Intended for access to children; add new children using ExpandChildren.
Definition: css_hierarchy.h:136
void InitializeRoot(const GoogleUrl &css_base_url, const GoogleUrl &css_trim_url, const StringPiece input_contents, bool has_unparseables, int64 flattened_result_limit, Css::Stylesheet *stylesheet, MessageHandler *message_handler)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void set_input_contents(const StringPiece input_contents)
Definition: css_hierarchy.h:113
const GoogleUrl & css_resolution_base()
Definition: css_hierarchy.h:102
CssHierarchy(CssFilter *filter)
Definition: message_handler.h:39
Definition: css_filter.h:74
bool IsDataUrl(const StringPiece url)
Determine if the given URL is a data: URL. Cheaper than ParseDataUrl.
void AddFlatteningFailureReason(const GoogleString &reason)