2
* Copyright 2010 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: mdsteele@google.com (Matthew D. Steele)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_JS_INLINE_FILTER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_JS_INLINE_FILTER_H_
24
#include "net/instaweb/http/public/semantic_type.h"
25
#include "net/instaweb/rewriter/public/common_filter.h"
26
#include "net/instaweb/rewriter/public/resource.h"
27
#include "net/instaweb/rewriter/public/rewrite_driver.h"
28
#include "net/instaweb/rewriter/public/rewrite_options.h"
29
#include "net/instaweb/rewriter/public/script_tag_scanner.h"
30
#include "net/instaweb/util/public/basictypes.h"
31
#include "net/instaweb/util/public/string_util.h"
32
#include "pagespeed/kernel/base/string.h"
34
namespace net_instaweb {
36
class HtmlCharactersNode;
40
// Inline small Javascript files.
41
class JsInlineFilter : public CommonFilter {
43
static const char kNumJsInlined[];
44
explicit JsInlineFilter(RewriteDriver* driver);
45
virtual ~JsInlineFilter();
47
virtual void StartDocumentImpl();
48
virtual void EndDocument();
49
virtual void StartElementImpl(HtmlElement* element);
50
virtual void EndElementImpl(HtmlElement* element);
51
virtual void Characters(HtmlCharactersNode* characters);
52
virtual const char* Name() const { return "InlineJs"; }
53
// Inlining javascript from unauthorized domains into HTML is considered
54
// safe because it does not cause any new content to be executed compared
55
// to the unoptimized page.
56
virtual RewriteDriver::InlineAuthorizationPolicy AllowUnauthorizedDomain()
58
return driver()->options()->HasInlineUnauthorizedResourceType(
59
semantic_type::kScript) ?
60
RewriteDriver::kInlineUnauthorizedResources :
61
RewriteDriver::kInlineOnlyAuthorizedResources;
63
virtual bool IntendedForInlining() const { return true; }
65
static void InitStats(Statistics* statistics);
71
bool ShouldInline(const ResourcePtr& resource, GoogleString* reason) const;
72
void RenderInline(const ResourcePtr& resource, const StringPiece& text,
73
HtmlElement* element);
75
const size_t size_threshold_bytes_;
76
ScriptTagScanner script_tag_scanner_;
78
// This is set to true during StartElement() for a <script> tag that we
79
// should maybe inline, but may be set back to false by Characters(). If it
80
// is still true when we hit the corresponding EndElement(), then we'll
81
// inline the script (and set it back to false). It should never be true
82
// outside of <script> and </script>.
85
Variable* num_js_inlined_;
87
DISALLOW_COPY_AND_ASSIGN(JsInlineFilter);
90
} // namespace net_instaweb
92
#endif // NET_INSTAWEB_REWRITER_PUBLIC_JS_INLINE_FILTER_H_