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/javascript_code_block.h" 00024 #include "net/instaweb/rewriter/public/resource_slot.h" 00025 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00026 #include "net/instaweb/rewriter/public/rewrite_options.h" 00027 #include "net/instaweb/rewriter/public/script_tag_scanner.h" 00028 #include "net/instaweb/util/public/basictypes.h" 00029 00030 namespace net_instaweb { 00031 00032 class HtmlCharactersNode; 00033 class HtmlIEDirectiveNode; 00034 class RewriteContext; 00035 class RewriteDriver; 00036 class Statistics; 00037 00038 /* 00039 * Find Javascript elements (either inline scripts or imported js files) and 00040 * rewrite them. This can involve any combination of minifaction, 00041 * concatenation, renaming, reordering, and incrementalization that accomplishes 00042 * our goals. 00043 * 00044 * For the moment we keep it simple and just minify any scripts that we find. 00045 * 00046 * Challenges: 00047 * * Identifying everywhere js is invoked, in particular event handlers on 00048 * elements that might be found in css or in variously-randomly-named 00049 * html properties. 00050 * * Analysis of eval() contexts. Actually less hard than the last, assuming 00051 * constant strings. Otherwise hard. 00052 * * Figuring out where to re-inject code after analysis. 00053 * 00054 * We will probably need to do an end run around the need for js analysis by 00055 * instrumenting and incrementally loading code, then probably using dynamic 00056 * feedback to change the runtime instrumentation in future pages as we serve 00057 * them. 00058 */ 00059 class JavascriptFilter : public RewriteFilter { 00060 public: 00061 explicit JavascriptFilter(RewriteDriver* rewrite_driver); 00062 virtual ~JavascriptFilter(); 00063 static void Initialize(Statistics* statistics); 00064 00065 virtual void StartDocumentImpl() {} 00066 virtual void StartElementImpl(HtmlElement* element); 00067 virtual void Characters(HtmlCharactersNode* characters); 00068 virtual void EndElementImpl(HtmlElement* element); 00069 virtual void Flush(); 00070 virtual void IEDirective(HtmlIEDirectiveNode* directive); 00071 00074 void set_minify(bool minify) { 00075 config_.set_minify(minify); 00076 } 00077 00078 virtual const char* Name() const { return "Javascript"; } 00079 virtual const char* id() const { return RewriteOptions::kJavascriptMinId; } 00080 virtual RewriteContext* MakeRewriteContext(); 00081 00082 protected: 00083 virtual RewriteContext* MakeNestedRewriteContext( 00084 RewriteContext* parent, const ResourceSlotPtr& slot); 00085 00086 private: 00087 class Context; 00088 00089 inline void CompleteScriptInProgress(); 00090 inline void RewriteInlineScript(); 00091 inline void RewriteExternalScript(); 00092 00093 HtmlCharactersNode* body_node_; 00094 HtmlElement* script_in_progress_; 00095 HtmlElement::Attribute* script_src_; 00098 bool some_missing_scripts_; 00099 JavascriptRewriteConfig config_; 00100 ScriptTagScanner script_tag_scanner_; 00101 00102 DISALLOW_COPY_AND_ASSIGN(JavascriptFilter); 00103 }; 00104 00105 } 00106 00107 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_