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 PAGESPEED_KERNEL_HTML_HTML_FILTER_H_
20
#define PAGESPEED_KERNEL_HTML_HTML_FILTER_H_
22
#include "pagespeed/kernel/base/string.h"
24
namespace net_instaweb {
27
class HtmlCharactersNode;
28
class HtmlCommentNode;
29
class HtmlDirectiveNode;
31
class HtmlIEDirectiveNode;
33
// Base-class used to register for HTML Parser Callbacks. Derive from this
34
// class and register with HtmlParse::AddFilter to use the HTML Parser.
38
virtual ~HtmlFilter();
40
// Starts a new document. Filters should clear their state in this function,
41
// as the same Filter instance may be used for multiple HTML documents.
42
virtual void StartDocument() = 0;
43
// Note: EndDocument will be called imediately before the last Flush call.
44
virtual void EndDocument() = 0;
46
// When an HTML element is encountered during parsing, each filter's
47
// StartElement method is called. The HtmlElement lives for the entire
48
// duration of the document.
50
// TODO(jmarantz): consider passing handles rather than pointers and
51
// reference-counting them instead to save memory on long documents.
52
virtual void StartElement(HtmlElement* element) = 0;
53
virtual void EndElement(HtmlElement* element) = 0;
55
// Called for CDATA blocks (e.g. <![CDATA[foobar]]>)
56
virtual void Cdata(HtmlCdataNode* cdata) = 0;
58
// Called for HTML comments that aren't IE directives (e.g. <!--foobar-->).
59
virtual void Comment(HtmlCommentNode* comment) = 0;
61
// Called for an IE directive; typically used for CSS styling.
62
// See http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
64
// TODO(mdsteele): Should we try to maintain the nested structure of
65
// the conditionals, in the same way that we maintain nesting of elements?
66
virtual void IEDirective(HtmlIEDirectiveNode* directive) = 0;
68
// Called for raw characters between tags.
69
virtual void Characters(HtmlCharactersNode* characters) = 0;
71
// Called for HTML directives (e.g. <!doctype foobar>).
72
virtual void Directive(HtmlDirectiveNode* directive) = 0;
74
// Notifies the Filter that a flush is occurring. A filter that's
75
// generating streamed output should flush at this time. A filter
76
// that's mutating elements can mutate any element seen since the
77
// most recent flush; once an element is flushed it is already on
78
// the wire to its destination and it's too late to mutate. Flush
79
// is initiated by an application calling HttpParse::Flush().
81
// Flush() is called after all other handlers during a HttpParse::Flush(),
82
// except RenderDone(), which (if in use) happens after Flush().
83
virtual void Flush() = 0;
85
// Notifies a filter that an asynchronous rewrite & render computation
86
// phase has finished. This is not used by HtmlParse itself, but only by
87
// RewriteDriver for pre-render filters. Happens after the corresponding
88
// flush, for every flush window. Default implementation does nothing.
89
// TODO(morlovich): Push this down into CommonFilter and convert all the
90
// pre-render filters to inherit off it.
91
virtual void RenderDone();
93
// Invoked by rewrite driver where all filters should determine whether
94
// they are enabled for this request. The re-writer my optionally set
95
// disabled_reason to explain why it disabled itself, which will appear
96
// in the debug output.
97
virtual void DetermineEnabled(GoogleString* disabled_reason) = 0;
99
// Intended to be called from DetermineEnabled implementations in filters.
100
// Returns whether a filter is enabled.
101
bool is_enabled() const { return is_enabled_; }
103
// The name of this filter -- used for logging and debugging.
104
virtual const char* Name() const = 0;
107
void set_is_enabled(bool is_enabled) { is_enabled_ = is_enabled; }
113
} // namespace net_instaweb
115
#endif // PAGESPEED_KERNEL_HTML_HTML_FILTER_H_