00001 /* 00002 * Copyright 2011 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 00022 00023 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_JS_COMBINE_FILTER_H_ 00024 #define NET_INSTAWEB_REWRITER_PUBLIC_JS_COMBINE_FILTER_H_ 00025 00026 #include "base/scoped_ptr.h" 00027 #include "net/instaweb/htmlparse/public/html_element.h" 00028 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00029 #include "net/instaweb/rewriter/public/rewrite_options.h" 00030 #include "net/instaweb/rewriter/public/script_tag_scanner.h" 00031 #include "net/instaweb/util/public/basictypes.h" 00032 #include "net/instaweb/util/public/string.h" 00033 #include "net/instaweb/util/public/url_multipart_encoder.h" 00034 00035 namespace net_instaweb { 00036 00037 class HtmlCharactersNode; 00038 class HtmlIEDirectiveNode; 00039 class RewriteDriver; 00040 class RewriteContext; 00041 class Statistics; 00042 class UrlSegmentEncoder; 00043 00063 class JsCombineFilter : public RewriteFilter { 00064 public: 00065 static const char kJsFileCountReduction[]; 00066 00069 explicit JsCombineFilter(RewriteDriver* rewrite_driver); 00070 virtual ~JsCombineFilter(); 00071 00073 static void Initialize(Statistics* statistics); 00074 virtual const char* id() const { 00075 return RewriteOptions::kJavascriptCombinerId; 00076 } 00077 00078 protected: 00080 virtual void StartDocumentImpl(); 00081 virtual void StartElementImpl(HtmlElement* element); 00082 virtual void EndElementImpl(HtmlElement* element); 00083 virtual void Characters(HtmlCharactersNode* characters); 00084 virtual void Flush(); 00085 virtual void IEDirective(HtmlIEDirectiveNode* directive); 00086 virtual const char* Name() const { return "JsCombine"; } 00087 virtual RewriteContext* MakeRewriteContext(); 00088 virtual const UrlSegmentEncoder* encoder() const { return &encoder_; } 00089 00090 private: 00091 class JsCombiner; 00092 class Context; 00093 00094 friend class JsCombineFilterTest; 00095 00096 void ConsiderJsForCombination(HtmlElement* element, 00097 HtmlElement::Attribute* src); 00098 00100 GoogleString VarName(const GoogleString& url) const; 00101 00102 void NextCombination(); 00103 00104 Context* MakeContext(); 00105 00106 JsCombiner* combiner() const; 00107 00108 ScriptTagScanner script_scanner_; 00109 int script_depth_; 00110 00111 HtmlElement* current_js_script_; 00112 scoped_ptr<Context> context_; 00113 UrlMultipartEncoder encoder_; 00114 00115 DISALLOW_COPY_AND_ASSIGN(JsCombineFilter); 00116 }; 00117 00118 } 00119 00120 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_JS_COMBINE_FILTER_H_