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: jmaessen@google.com (Jan Maessen)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_URL_LEFT_TRIM_FILTER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_URL_LEFT_TRIM_FILTER_H_
22
#include "net/instaweb/htmlparse/public/html_element.h"
23
#include "net/instaweb/rewriter/public/common_filter.h"
24
#include "net/instaweb/util/public/basictypes.h"
25
#include "net/instaweb/util/public/string.h"
26
#include "net/instaweb/util/public/string_util.h"
28
namespace net_instaweb {
35
// Filter that trims redundant information from the left end of each url.
37
// For example: if the page's base URL is http://www.example.com/foo/bar.html
38
// then the following URLs can be trimmed:
39
// http://www.example.com/foo/bar/other.html -> bar/other.html
40
// http://www.example.com/another.html -> /another.html
41
// http://www.example.org/index.html -> //www.example.org/index.html
43
// TODO(jmaessen): Do we care to introduce ../ in order to relativize more urls?
44
// For example, if base URL is http://www.example.com/foo/bar/index.html
45
// we could convert: http://www.example.com/foo/other.html -> ../other.html
46
// rather than -> /foo/other.html.
47
class UrlLeftTrimFilter : public CommonFilter {
49
UrlLeftTrimFilter(RewriteDriver* rewrite_driver, Statistics* stats);
50
virtual ~UrlLeftTrimFilter();
52
static void InitStats(Statistics* statistics);
53
virtual void StartDocumentImpl() {}
54
virtual void StartElementImpl(HtmlElement* element);
55
virtual void EndElementImpl(HtmlElement* element) {}
57
virtual const char* Name() const { return "UrlLeftTrim"; }
59
// Trim 'url_to_trim' relative to 'base_url' returning the result in
60
// 'trimmed_url'. Returns true if we succeeded at trimming the URL.
62
// This is static and requires the base_url explicitly, so that it can be
63
// called from other places (like the CSS filter).
64
static bool Trim(const GoogleUrl& base_url, const StringPiece& url_to_trim,
65
GoogleString* trimmed_url, MessageHandler* handler);
68
void TrimAttribute(HtmlElement::Attribute* attr);
71
friend class UrlLeftTrimFilterTest;
73
// Stats on how much trimming we've done.
74
Variable* trim_count_;
75
Variable* trim_saved_bytes_;
77
DISALLOW_COPY_AND_ASSIGN(UrlLeftTrimFilter);
80
} // namespace net_instaweb
82
#endif // NET_INSTAWEB_REWRITER_PUBLIC_URL_LEFT_TRIM_FILTER_H_