Page Speed Optimization Libraries  1.5.27.2
net/instaweb/rewriter/public/javascript_filter.h
Go to the documentation of this file.
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  protected:
00077   virtual RewriteContext* MakeNestedRewriteContext(
00078       RewriteContext* parent, const ResourceSlotPtr& slot);
00079 
00080  private:
00081   class Context;
00082 
00083   typedef enum {
00084     kNoScript,
00085     kExternalScript,
00086     kInlineScript
00087   } ScriptType;
00088 
00089   inline void RewriteInlineScript(HtmlCharactersNode* body_node);
00090   inline void RewriteExternalScript(
00091       HtmlElement* script_in_progress, HtmlElement::Attribute* script_src);
00093   void InitializeConfigIfNecessary() {
00094     if (config_.get() != NULL) {
00095       return;
00096     }
00097     InitializeConfig();
00098   }
00099   void InitializeConfig();
00100 
00101   ScriptType script_type_;
00104   bool some_missing_scripts_;
00105   scoped_ptr<JavascriptRewriteConfig> config_;
00106   ScriptTagScanner script_tag_scanner_;
00107 
00108   DISALLOW_COPY_AND_ASSIGN(JavascriptFilter);
00109 };
00110 
00111 }  
00112 
00113 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines