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: jmarantz@google.com (Joshua Marantz)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_
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"
27
namespace net_instaweb {
29
class DomainRewriteFilter;
34
class UrlLeftTrimFilter;
39
// Helper class for TransformUrls to allow any URL transformation to
40
// be applied to a CSS file.
43
virtual ~Transformer();
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;
55
static const char kStylesheet[];
56
static const char kAlternate[];
57
static const char kUriValue[];
59
explicit CssTagScanner(HtmlParse* html_parse);
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,
69
StringPieceVector* nonstandard_attributes);
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,
76
return ParseCssElement(element, href, media,
77
NULL /* nonstandard attributes */);
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);
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);
92
// Detemines whether this CSS contains a URI value (aka URL).
93
static bool HasUrl(const StringPiece& contents);
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);
99
// Does this rel attribute value represent an alternate stylesheet?
100
static bool IsAlternateStylesheet(const StringPiece& attribute_value);
103
DISALLOW_COPY_AND_ASSIGN(CssTagScanner);
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 {
112
RewriteDomainTransformer(const GoogleUrl* old_base_url,
113
const GoogleUrl* new_base_url,
114
RewriteDriver* driver);
115
virtual ~RewriteDomainTransformer();
117
virtual TransformStatus Transform(GoogleString* str);
119
void set_trim_urls(bool x) { trim_urls_ = x; }
122
const GoogleUrl* old_base_url_;
123
const GoogleUrl* new_base_url_;
125
DomainRewriteFilter* domain_rewriter_;
126
UrlLeftTrimFilter* url_trim_filter_;
127
MessageHandler* handler_;
129
RewriteDriver* driver_;
131
DISALLOW_COPY_AND_ASSIGN(RewriteDomainTransformer);
135
} // namespace net_instaweb
137
#endif // NET_INSTAWEB_REWRITER_PUBLIC_CSS_TAG_SCANNER_H_