Page Speed Optimization Libraries  1.9.32.2
 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 "pagespeed/kernel/base/basictypes.h"
25 #include "pagespeed/kernel/base/string.h"
26 #include "pagespeed/kernel/base/string_util.h"
27 #include "pagespeed/kernel/html/empty_html_filter.h"
28 #include "pagespeed/kernel/html/html_name.h"
29 
30 namespace net_instaweb {
31 
32 class HtmlCharactersNode;
33 class HtmlElement;
34 class RewriteDriver;
35 class Statistics;
36 class Variable;
37 
39 struct MobileRole {
40  enum Level {
43  kKeeper = 0,
59  };
60 
61  static const MobileRole kMobileRoles[kInvalid];
62 
63  MobileRole(Level level, const char* value)
64  : level(level),
65  value(value) { }
66 
67  static const MobileRole* FromString(const StringPiece& mobile_role);
68  static Level LevelFromString(const StringPiece& mobile_role);
69  static const char* StringFromLevel(Level level) {
70  return (level < kInvalid) ? kMobileRoles[level].value : NULL;
71  }
72 
73  const Level level;
74  const char* const value;
75 };
76 
114 class MobilizeRewriteFilter : public EmptyHtmlFilter {
115  public:
116  static const char kPagesMobilized[];
117  static const char kKeeperBlocks[];
118  static const char kHeaderBlocks[];
119  static const char kNavigationalBlocks[];
120  static const char kContentBlocks[];
121  static const char kMarginalBlocks[];
122  static const char kDeletedElements[];
123 
126  static const HtmlName::Keyword kKeeperTags[];
127  static const int kNumKeeperTags;
128 
129  explicit MobilizeRewriteFilter(RewriteDriver* rewrite_driver);
130  virtual ~MobilizeRewriteFilter();
131 
132  static void InitStats(Statistics* statistics);
133 
134  virtual void StartDocument();
135  virtual void EndDocument();
136  virtual void StartElement(HtmlElement* element);
137  virtual void EndElement(HtmlElement* element);
138  virtual void Characters(HtmlCharactersNode* characters);
139  virtual const char* Name() const { return "MobilizeRewrite"; }
140 
141  private:
142  void HandleStartTagInBody(HtmlElement* element);
143  void HandleEndTagInBody(HtmlElement* element);
144  void AddStyleAndViewport(HtmlElement* element);
145  void AddReorderContainers(HtmlElement* element);
146  void RemoveReorderContainers();
147  bool IsReorderContainer(HtmlElement* element);
148  HtmlElement* MobileRoleToContainer(MobileRole::Level level);
149  MobileRole::Level GetMobileRole(HtmlElement* element);
150 
151  bool InImportantElement() {
152  return (!element_roles_stack_.empty());
153  }
154 
155  bool CheckForKeyword(
156  const HtmlName::Keyword* sorted_list, int len, HtmlName::Keyword keyword);
157  void LogMovedBlock(MobileRole::Level level);
158 
159  RewriteDriver* driver_;
160  std::vector<MobileRole::Level> element_roles_stack_;
161  std::vector<HtmlName::Keyword> nav_keyword_stack_;
162  std::vector<HtmlElement*> mobile_role_containers_;
163  int body_element_depth_;
164  int nav_element_depth_;
165  bool reached_reorder_containers_;
166  bool found_viewport_;
167  bool added_style_;
168  bool added_containers_;
169  bool added_mob_js_;
170  bool in_script_;
171  bool use_cxx_layout_;
172  bool use_js_layout_;
173  bool use_js_logo_;
174  bool use_js_nav_;
175  GoogleString static_file_prefix_;
176 
179  Variable* num_pages_mobilized_;
181  Variable* num_keeper_blocks_;
182  Variable* num_header_blocks_;
183  Variable* num_navigational_blocks_;
184  Variable* num_content_blocks_;
185  Variable* num_marginal_blocks_;
187  Variable* num_elements_deleted_;
188 
194  const char* style_css_;
195 
196  DISALLOW_COPY_AND_ASSIGN(MobilizeRewriteFilter);
197 };
198 
199 }
200 
201 #endif
static const HtmlName::Keyword kKeeperTags[]
Definition: mobilize_rewrite_filter.h:126
A mobile role and its associated HTML attribute value.
Definition: mobilize_rewrite_filter.h:39
Definition: mobilize_rewrite_filter.h:43
const char *const value
Set to a static string in cc.
Definition: mobilize_rewrite_filter.h:74
Definition: mobilize_rewrite_filter.h:58
The page header, such as.
Definition: mobilize_rewrite_filter.h:45
Level
Definition: mobilize_rewrite_filter.h:40
Definition: mobilize_rewrite_filter.h:55
Definition: rewrite_driver.h:98
Main content of the page.
Definition: mobilize_rewrite_filter.h:51
Definition: mobilize_rewrite_filter.h:114
friend class MobilizeRewriteFilterTest
Used for overriding default behavior in testing.
Definition: mobilize_rewrite_filter.h:190
Definition: mobilize_rewrite_filter.h:49