Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mobilize_rewrite_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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_MOBILIZE_REWRITE_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_MOBILIZE_REWRITE_FILTER_H_
21 
22 #include <vector>
23 
24 #include "net/instaweb/htmlparse/public/empty_html_filter.h"
25 #include "net/instaweb/htmlparse/public/html_name.h"
27 #include "pagespeed/kernel/base/string_util.h"
28 
29 namespace net_instaweb {
30 
31 class HtmlCharactersNode;
32 class HtmlElement;
33 class RewriteDriver;
34 class Statistics;
35 class Variable;
36 
38 struct MobileRole {
39  enum Level {
42  kKeeper = 0,
58  };
59 
60  static const MobileRole kMobileRoles[kInvalid];
61 
62  MobileRole(Level level, const char* value)
63  : level(level),
64  value(value) { }
65 
66  static const MobileRole* FromString(const StringPiece& mobile_role);
67  static Level LevelFromString(const StringPiece& mobile_role);
68  static const char* StringFromLevel(Level level) {
69  return (level < kInvalid) ? kMobileRoles[level].value : NULL;
70  }
71 
72  const Level level;
73  const char* const value;
74 };
75 
113 class MobilizeRewriteFilter : public EmptyHtmlFilter {
114  public:
115  static const char kPagesMobilized[];
116  static const char kKeeperBlocks[];
117  static const char kHeaderBlocks[];
118  static const char kNavigationalBlocks[];
119  static const char kContentBlocks[];
120  static const char kMarginalBlocks[];
121  static const char kDeletedElements[];
122 
123  explicit MobilizeRewriteFilter(RewriteDriver* rewrite_driver);
124  virtual ~MobilizeRewriteFilter();
125 
126  static void InitStats(Statistics* statistics);
127 
128  virtual void StartDocument();
129  virtual void EndDocument();
130  virtual void StartElement(HtmlElement* element);
131  virtual void EndElement(HtmlElement* element);
132  virtual void Characters(HtmlCharactersNode* characters);
133  virtual const char* Name() const { return "MobilizeRewrite"; }
134 
135  private:
136  void HandleStartTagInBody(HtmlElement* element);
137  void HandleEndTagInBody(HtmlElement* element);
138  void AddStyleAndViewport(HtmlElement* element);
139  void AddReorderContainers(HtmlElement* element);
140  void RemoveReorderContainers();
141  bool IsReorderContainer(HtmlElement* element);
142  HtmlElement* MobileRoleToContainer(MobileRole::Level level);
143  MobileRole::Level GetMobileRole(HtmlElement* element);
144 
145  bool InImportantElement() {
146  return (important_element_depth_ > 0);
147  }
148 
149  bool CheckForKeyword(
150  const HtmlName::Keyword* sorted_list, int len, HtmlName::Keyword keyword);
151  void LogMovedBlock(MobileRole::Level level);
152 
153  RewriteDriver* driver_;
154  std::vector<HtmlName::Keyword> nav_keyword_stack_;
155  std::vector<HtmlElement*> mobile_role_containers_;
156  int important_element_depth_;
157  int body_element_depth_;
158  int nav_element_depth_;
159  bool reached_reorder_containers_;
160  bool added_style_;
161  bool added_containers_;
162 
165  Variable* num_pages_mobilized_;
167  Variable* num_keeper_blocks_;
168  Variable* num_header_blocks_;
169  Variable* num_navigational_blocks_;
170  Variable* num_content_blocks_;
171  Variable* num_marginal_blocks_;
173  Variable* num_elements_deleted_;
174 
180  const char* style_css_;
181 
182  DISALLOW_COPY_AND_ASSIGN(MobilizeRewriteFilter);
183 };
184 
185 }
186 
187 #endif
A mobile role and its associated HTML attribute value.
Definition: mobilize_rewrite_filter.h:38
Definition: mobilize_rewrite_filter.h:42
const char *const value
Set to a static string in cc.
Definition: mobilize_rewrite_filter.h:73
Definition: mobilize_rewrite_filter.h:57
The page header, such as.
Definition: mobilize_rewrite_filter.h:44
Level
Definition: mobilize_rewrite_filter.h:39
Definition: mobilize_rewrite_filter.h:54
Definition: rewrite_driver.h:98
Main content of the page.
Definition: mobilize_rewrite_filter.h:50
Definition: mobilize_rewrite_filter.h:113
friend class MobilizeRewriteFilterTest
Used for overriding default behavior in testing.
Definition: mobilize_rewrite_filter.h:176
Definition: mobilize_rewrite_filter.h:48