Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
javascript_filter.h
Go to the documentation of this file.
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 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_
21 
26 #include "pagespeed/kernel/base/basictypes.h"
27 #include "pagespeed/kernel/base/scoped_ptr.h"
28 #include "pagespeed/kernel/html/html_element.h"
29 
30 namespace net_instaweb {
31 
32 class HtmlCharactersNode;
33 class HtmlIEDirectiveNode;
34 class JavascriptRewriteConfig;
35 class RewriteContext;
36 class RewriteDriver;
37 class Statistics;
38 
39 /*
40  * Find Javascript elements (either inline scripts or imported js files) and
41  * rewrite them. This can involve any combination of minifaction,
42  * concatenation, renaming, reordering, and incrementalization that accomplishes
43  * our goals.
44  *
45  * For the moment we keep it simple and just minify any scripts that we find.
46  *
47  * Challenges:
48  * * Identifying everywhere js is invoked, in particular event handlers on
49  * elements that might be found in css or in variously-randomly-named
50  * html properties.
51  * * Analysis of eval() contexts. Actually less hard than the last, assuming
52  * constant strings. Otherwise hard.
53  * * Figuring out where to re-inject code after analysis.
54  *
55  * We will probably need to do an end run around the need for js analysis by
56  * instrumenting and incrementally loading code, then probably using dynamic
57  * feedback to change the runtime instrumentation in future pages as we serve
58  * them.
59  */
61  public:
62  explicit JavascriptFilter(RewriteDriver* rewrite_driver);
63  virtual ~JavascriptFilter();
64  static void InitStats(Statistics* statistics);
65 
66  virtual void StartDocumentImpl() { InitializeConfigIfNecessary(); }
67  virtual void StartElementImpl(HtmlElement* element);
68  virtual void Characters(HtmlCharactersNode* characters);
69  virtual void EndElementImpl(HtmlElement* element);
70  virtual void IEDirective(HtmlIEDirectiveNode* directive);
71 
72  virtual const char* Name() const { return "Javascript"; }
73  virtual const char* id() const { return RewriteOptions::kJavascriptMinId; }
74  virtual RewriteContext* MakeRewriteContext();
75 
76  static JavascriptRewriteConfig* InitializeConfig(RewriteDriver* driver);
77 
78  protected:
79  virtual RewriteContext* MakeNestedRewriteContext(
80  RewriteContext* parent, const ResourceSlotPtr& slot);
81 
82  private:
83  class Context;
84 
85  typedef enum {
86  kNoScript,
87  kExternalScript,
88  kInlineScript
89  } ScriptType;
90 
91  inline void RewriteInlineScript(HtmlCharactersNode* body_node);
92  inline void RewriteExternalScript(
93  HtmlElement* script_in_progress, HtmlElement::Attribute* script_src);
94 
98  void InitializeConfigIfNecessary();
99 
102  virtual bool output_source_map() const { return false; }
103 
104  ScriptType script_type_;
107  bool some_missing_scripts_;
108  scoped_ptr<JavascriptRewriteConfig> config_;
109  ScriptTagScanner script_tag_scanner_;
110 
111  DISALLOW_COPY_AND_ASSIGN(JavascriptFilter);
112 };
113 
115  public:
116  explicit JavascriptSourceMapFilter(RewriteDriver* rewrite_driver);
117  virtual ~JavascriptSourceMapFilter();
118 
119  virtual const char* Name() const { return "Javascript_Source_Map"; }
120  virtual const char* id() const {
121  return RewriteOptions::kJavascriptMinSourceMapId;
122  }
123 
124  private:
125  virtual bool output_source_map() const { return true; }
126 };
127 
128 }
129 
130 #endif
virtual void StartDocumentImpl()
Definition: javascript_filter.h:66
Definition: javascript_filter.h:60
virtual void Characters(HtmlCharactersNode *characters)
virtual RewriteContext * MakeNestedRewriteContext(RewriteContext *parent, const ResourceSlotPtr &slot)
virtual RewriteContext * MakeRewriteContext()
Definition: rewrite_filter.h:37
Definition: rewrite_driver.h:98
Definition: javascript_filter.h:114