~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/rewriter/public/css_tag_scanner.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2010 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
 
19
 
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_
20
 
#define NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_
21
 
 
22
 
#include "net/instaweb/htmlparse/public/html_element.h"
23
 
#include "net/instaweb/util/public/basictypes.h"
24
 
#include "net/instaweb/util/public/string.h"
25
 
#include "net/instaweb/util/public/string_util.h"
26
 
 
27
 
namespace net_instaweb {
28
 
 
29
 
class DomainRewriteFilter;
30
 
class GoogleUrl;
31
 
class HtmlParse;
32
 
class MessageHandler;
33
 
class RewriteDriver;
34
 
class UrlLeftTrimFilter;
35
 
class Writer;
36
 
 
37
 
class CssTagScanner {
38
 
 public:
39
 
  // Helper class for TransformUrls to allow any URL transformation to
40
 
  // be applied to a CSS file.
41
 
  class Transformer {
42
 
   public:
43
 
    virtual ~Transformer();
44
 
 
45
 
    enum TransformStatus { kSuccess, kNoChange, kFailure };
46
 
    // Transforms str in-place.
47
 
    // If kSuccess -> transformation succeeded and str may have changed
48
 
    // (Generally implementers should only return kSuccess if str changed, but
49
 
    // this is merely an optimization. Functionally it doesn't matter).
50
 
    // If kNoChange -> transformation succeeded and str was unchanged.
51
 
    // If kFailure -> transformation failed. str is undefined, do not use.
52
 
    virtual TransformStatus Transform(GoogleString* str) = 0;
53
 
  };
54
 
 
55
 
  static const char kStylesheet[];
56
 
  static const char kAlternate[];
57
 
  static const char kUriValue[];
58
 
 
59
 
  explicit CssTagScanner(HtmlParse* html_parse);
60
 
 
61
 
  // Examines an HTML element to determine if it's a CSS link, extracting out
62
 
  // the href, the media type (if any) and any nonstandard attributes.  If it's
63
 
  // not CSS, href is set to NULL, media is set to "", and no nonstandard
64
 
  // attributes are identified.  NULL may be passed for nonstandard_attributes
65
 
  // to indicate the caller doesn't need them collected.
66
 
  bool ParseCssElement(HtmlElement* element,
67
 
                       HtmlElement::Attribute** href,
68
 
                       const char** media,
69
 
                       StringPieceVector* nonstandard_attributes);
70
 
 
71
 
  // Many callers don't care about nonstandard attributes, so we provide a
72
 
  // version that discards that information.
73
 
  bool ParseCssElement(HtmlElement* element,
74
 
                       HtmlElement::Attribute** href,
75
 
                       const char** media) {
76
 
    return ParseCssElement(element, href, media,
77
 
                           NULL /* nonstandard attributes */);
78
 
  }
79
 
 
80
 
  // Scans the contents of a CSS file, looking for the pattern url(xxx).
81
 
  // Performs an arbitrary mutation on all such URLs.
82
 
  // If xxx is quoted with single-quotes or double-quotes, those are
83
 
  // retained and the URL inside is transformed.
84
 
  static bool TransformUrls(
85
 
      const StringPiece& contents, Writer* writer, Transformer* transformer,
86
 
      MessageHandler* handler);
87
 
 
88
 
  // Does this CSS file contain @import? If so, it cannot be combined with
89
 
  // previous CSS files. This may give false-positives, but no false-negatives.
90
 
  static bool HasImport(const StringPiece& contents, MessageHandler* handler);
91
 
 
92
 
  // Detemines whether this CSS contains a URI value (aka URL).
93
 
  static bool HasUrl(const StringPiece& contents);
94
 
 
95
 
  // Does this attribute value represent a stylesheet or alternate stylesheet?
96
 
  // Should be called with element->AttributeValue(HtmlName::kRel) as the arg.
97
 
  static bool IsStylesheetOrAlternate(const StringPiece& attribute_value);
98
 
 
99
 
  // Does this rel attribute value represent an alternate stylesheet?
100
 
  static bool IsAlternateStylesheet(const StringPiece& attribute_value);
101
 
 
102
 
 private:
103
 
  DISALLOW_COPY_AND_ASSIGN(CssTagScanner);
104
 
};
105
 
 
106
 
// Transform URLs by:
107
 
//   1 Resolving them against old_base_url,
108
 
//   2 Mapping them appropriately with domain_rewrite_filter and then
109
 
//   3 Trimming them against new_base_url.
110
 
class RewriteDomainTransformer : public CssTagScanner::Transformer {
111
 
 public:
112
 
  RewriteDomainTransformer(const GoogleUrl* old_base_url,
113
 
                           const GoogleUrl* new_base_url,
114
 
                           RewriteDriver* driver);
115
 
  virtual ~RewriteDomainTransformer();
116
 
 
117
 
  virtual TransformStatus Transform(GoogleString* str);
118
 
 
119
 
  void set_trim_urls(bool x) { trim_urls_ = x; }
120
 
 
121
 
 private:
122
 
  const GoogleUrl* old_base_url_;
123
 
  const GoogleUrl* new_base_url_;
124
 
 
125
 
  DomainRewriteFilter* domain_rewriter_;
126
 
  UrlLeftTrimFilter* url_trim_filter_;
127
 
  MessageHandler* handler_;
128
 
  bool trim_urls_;
129
 
  RewriteDriver* driver_;
130
 
 
131
 
  DISALLOW_COPY_AND_ASSIGN(RewriteDomainTransformer);
132
 
};
133
 
 
134
 
 
135
 
}  // namespace net_instaweb
136
 
 
137
 
#endif  // NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_