Page Speed Optimization Libraries  1.13.35.1
 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 
35 
36 namespace net_instaweb {
37 
38 /*
39  * Find Javascript elements (either inline scripts or imported js files) and
40  * rewrite them. This can involve any combination of minifaction,
41  * concatenation, renaming, reordering, and incrementalization that accomplishes
42  * our goals.
43  *
44  * For the moment we keep it simple and just minify any scripts that we find.
45  *
46  * Challenges:
47  * * Identifying everywhere js is invoked, in particular event handlers on
48  * elements that might be found in css or in variously-randomly-named
49  * html properties.
50  * * Analysis of eval() contexts. Actually less hard than the last, assuming
51  * constant strings. Otherwise hard.
52  * * Figuring out where to re-inject code after analysis.
53  *
54  * We will probably need to do an end run around the need for js analysis by
55  * instrumenting and incrementally loading code, then probably using dynamic
56  * feedback to change the runtime instrumentation in future pages as we serve
57  * them.
58  */
60  public:
61  explicit JavascriptFilter(RewriteDriver* rewrite_driver);
62  ~JavascriptFilter() override;
63  static void InitStats(Statistics* statistics);
64 
65  void StartDocumentImpl() override { InitializeConfigIfNecessary(); }
66  void StartElementImpl(HtmlElement* element) override;
67  void Characters(HtmlCharactersNode* characters) override;
68  void EndElementImpl(HtmlElement* element) override;
69  void IEDirective(HtmlIEDirectiveNode* directive) override;
70 
71  const char* Name() const override { return "JavascriptFilter"; }
72  const char* id() const override { return RewriteOptions::kJavascriptMinId; }
73  RewriteContext* MakeRewriteContext() override;
74  ScriptUsage GetScriptUsage() const override { return kWillInjectScripts; }
75 
76  static JavascriptRewriteConfig* InitializeConfig(RewriteDriver* driver);
77 
78  protected:
80  RewriteContext* parent, const ResourceSlotPtr& slot) override;
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 
112 };
113 
115  public:
116  explicit JavascriptSourceMapFilter(RewriteDriver* rewrite_driver);
117  ~JavascriptSourceMapFilter() override;
118 
119  const char* Name() const override { return "Javascript_Source_Map"; }
120  const char* id() const override {
121  return RewriteOptions::kJavascriptMinSourceMapId;
122  }
123 
124  private:
125  bool output_source_map() const override { return true; }
126 };
127 
128 }
129 
130 #endif
ScriptUsage GetScriptUsage() const override
Definition: javascript_filter.h:74
RewriteContext * MakeRewriteContext() override
const char * Name() const override
The name of this filter – used for logging and debugging.
Definition: javascript_filter.h:119
Definition: javascript_filter.h:59
Leaf node representing raw characters in HTML.
Definition: html_node.h:167
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Definition: html_element.h:42
Definition: ref_counted_ptr.h:69
void IEDirective(HtmlIEDirectiveNode *directive) override
void Characters(HtmlCharactersNode *characters) override
RewriteContext * MakeNestedRewriteContext(RewriteContext *parent, const ResourceSlotPtr &slot) override
const char * Name() const override
The name of this filter – used for logging and debugging.
Definition: javascript_filter.h:71
ScriptUsage
Describes a filter's relationship with scripts.
Definition: html_filter.h:38
Definition: rewrite_filter.h:35
Definition: rewrite_driver.h:100
void StartDocumentImpl() override
Definition: javascript_filter.h:65
Definition: rewrite_context.h:155
Definition: javascript_code_block.h:45
Definition: javascript_filter.h:114
Leaf node representing an HTML IE directive.
Definition: html_node.h:213