Page Speed Optimization Libraries
1.7.30.4
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_ 00021 00022 #include "net/instaweb/htmlparse/public/html_element.h" 00023 #include "net/instaweb/rewriter/public/resource_slot.h" 00024 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00025 #include "net/instaweb/rewriter/public/rewrite_options.h" 00026 #include "net/instaweb/rewriter/public/script_tag_scanner.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/scoped_ptr.h" 00029 00030 namespace net_instaweb { 00031 00032 class HtmlCharactersNode; 00033 class HtmlIEDirectiveNode; 00034 class JavascriptRewriteConfig; 00035 class RewriteContext; 00036 class RewriteDriver; 00037 class Statistics; 00038 00039 /* 00040 * Find Javascript elements (either inline scripts or imported js files) and 00041 * rewrite them. This can involve any combination of minifaction, 00042 * concatenation, renaming, reordering, and incrementalization that accomplishes 00043 * our goals. 00044 * 00045 * For the moment we keep it simple and just minify any scripts that we find. 00046 * 00047 * Challenges: 00048 * * Identifying everywhere js is invoked, in particular event handlers on 00049 * elements that might be found in css or in variously-randomly-named 00050 * html properties. 00051 * * Analysis of eval() contexts. Actually less hard than the last, assuming 00052 * constant strings. Otherwise hard. 00053 * * Figuring out where to re-inject code after analysis. 00054 * 00055 * We will probably need to do an end run around the need for js analysis by 00056 * instrumenting and incrementally loading code, then probably using dynamic 00057 * feedback to change the runtime instrumentation in future pages as we serve 00058 * them. 00059 */ 00060 class JavascriptFilter : public RewriteFilter { 00061 public: 00062 explicit JavascriptFilter(RewriteDriver* rewrite_driver); 00063 virtual ~JavascriptFilter(); 00064 static void InitStats(Statistics* statistics); 00065 00066 virtual void StartDocumentImpl() { InitializeConfigIfNecessary(); } 00067 virtual void StartElementImpl(HtmlElement* element); 00068 virtual void Characters(HtmlCharactersNode* characters); 00069 virtual void EndElementImpl(HtmlElement* element); 00070 virtual void IEDirective(HtmlIEDirectiveNode* directive); 00071 00072 virtual const char* Name() const { return "Javascript"; } 00073 virtual const char* id() const { return RewriteOptions::kJavascriptMinId; } 00074 virtual RewriteContext* MakeRewriteContext(); 00075 00076 static JavascriptRewriteConfig* InitializeConfig(RewriteDriver* driver); 00077 00078 protected: 00079 virtual RewriteContext* MakeNestedRewriteContext( 00080 RewriteContext* parent, const ResourceSlotPtr& slot); 00081 00082 private: 00083 class Context; 00084 00085 typedef enum { 00086 kNoScript, 00087 kExternalScript, 00088 kInlineScript 00089 } ScriptType; 00090 00091 inline void RewriteInlineScript(HtmlCharactersNode* body_node); 00092 inline void RewriteExternalScript( 00093 HtmlElement* script_in_progress, HtmlElement::Attribute* script_src); 00094 00098 void InitializeConfigIfNecessary(); 00099 00102 virtual bool output_source_map() const { return false; } 00103 00104 ScriptType script_type_; 00107 bool some_missing_scripts_; 00108 scoped_ptr<JavascriptRewriteConfig> config_; 00109 ScriptTagScanner script_tag_scanner_; 00110 00111 DISALLOW_COPY_AND_ASSIGN(JavascriptFilter); 00112 }; 00113 00114 class JavascriptSourceMapFilter : public JavascriptFilter { 00115 public: 00116 explicit JavascriptSourceMapFilter(RewriteDriver* rewrite_driver); 00117 virtual ~JavascriptSourceMapFilter(); 00118 00119 virtual const char* Name() const { return "Javascript_Source_Map"; } 00120 virtual const char* id() const { 00121 return RewriteOptions::kJavascriptMinSourceMapId; 00122 } 00123 00124 private: 00125 virtual bool output_source_map() const { return true; } 00126 }; 00127 00128 } 00129 00130 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_