Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
html_node.h
Go to the documentation of this file.
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 
18 
19 #ifndef PAGESPEED_KERNEL_HTML_HTML_NODE_H_
20 #define PAGESPEED_KERNEL_HTML_HTML_NODE_H_
21 
22 #include <cstddef>
23 #include <list>
24 
25 #include "base/logging.h"
31 
32 namespace net_instaweb {
33 
34 class HtmlElement;
35 class HtmlEvent;
36 
37 typedef std::list<HtmlEvent*> HtmlEventList;
38 typedef HtmlEventList::iterator HtmlEventListIterator;
39 
43 class HtmlNode {
44  public:
45  virtual ~HtmlNode();
46  friend class HtmlParse;
47 
48  HtmlElement* parent() const { return parent_; }
49  virtual bool live() const = 0;
50  virtual GoogleString ToString() const = 0;
51 
55  virtual void MarkAsDead(const HtmlEventListIterator& end) = 0;
56 
57  void* operator new(size_t size, Arena<HtmlNode>* arena) {
58  return arena->Allocate(size);
59  }
60 
61  void operator delete(void* ptr, Arena<HtmlNode>* arena) {
62  LOG(FATAL) << "HtmlNode must not be deleted directly.";
63  }
64 
65  protected:
71  explicit HtmlNode(HtmlElement* parent) : parent_(parent) {}
72 
77  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
78  HtmlEventList* queue) = 0;
79 
81  virtual HtmlEventListIterator begin() const = 0;
83  virtual HtmlEventListIterator end() const = 0;
84 
86  void operator delete(void* ptr) {
87  LOG(FATAL) << "HtmlNode must not be deleted directly.";
88  }
89 
90  private:
91  friend class HtmlLexer;
92  friend class HtmlTestingPeer;
93 
97  void set_parent(HtmlElement* parent) { parent_ = parent; }
98 
99  HtmlElement* parent_;
100 
101 };
102 
103 class HtmlLeafNode : public HtmlNode {
104  public:
105  virtual ~HtmlLeafNode();
106  virtual bool live() const { return (data_.get() != NULL) && data_->is_live_; }
107  virtual void MarkAsDead(const HtmlEventListIterator& end);
108  virtual GoogleString ToString() const;
109 
110  const GoogleString& contents() const { return data_->contents_; }
111  virtual HtmlEventListIterator begin() const {
112  return data_->iter_;
113  }
114  virtual HtmlEventListIterator end() const {
115  return data_->iter_;
116  }
117  void set_iter(const HtmlEventListIterator& iter) {
118  data_->iter_ = iter;
119  }
120 
121  void FreeData() { data_.reset(NULL); }
122 
123  protected:
124  HtmlLeafNode(HtmlElement* parent, const HtmlEventListIterator& iter,
125  const StringPiece& contents);
126 
129  GoogleString* mutable_contents() { return &data_->contents_; }
130 
131  private:
132  struct Data {
133  Data(const HtmlEventListIterator& iter, const StringPiece& contents)
134  : contents_(contents.data(), contents.size()),
135  is_live_(true),
136  iter_(iter) {
137  }
138  GoogleString contents_;
139  bool is_live_;
140  HtmlEventListIterator iter_;
141  };
142 
143  scoped_ptr<Data> data_;
144 };
145 
147 class HtmlCdataNode : public HtmlLeafNode {
148  public:
149  virtual ~HtmlCdataNode();
150  friend class HtmlParse;
151 
152  protected:
153  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
154  HtmlEventList* queue);
155 
156  private:
157  HtmlCdataNode(HtmlElement* parent,
158  const StringPiece& contents,
159  const HtmlEventListIterator& iter)
160  : HtmlLeafNode(parent, iter, contents) {
161  }
162 
163 
164 };
165 
168  public:
169  virtual ~HtmlCharactersNode();
170  void Append(const StringPiece& str) {
171  mutable_contents()->append(str.data(), str.size());
172  }
173  friend class HtmlParse;
174 
177 
178  protected:
179  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
180  HtmlEventList* queue);
181 
182  private:
184  const StringPiece& contents,
185  const HtmlEventListIterator& iter)
186  : HtmlLeafNode(parent, iter, contents) {
187  }
188 
189 
190 };
191 
194  public:
195  virtual ~HtmlCommentNode();
196  friend class HtmlParse;
197 
198  protected:
199  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
200  HtmlEventList* queue);
201 
202  private:
204  const StringPiece& contents,
205  const HtmlEventListIterator& iter)
206  : HtmlLeafNode(parent, iter, contents) {
207  }
208 
209 
210 };
211 
214  public:
215  virtual ~HtmlIEDirectiveNode();
216  friend class HtmlParse;
217 
218  protected:
219  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
220  HtmlEventList* queue);
221 
222  private:
224  const StringPiece& contents,
225  const HtmlEventListIterator& iter)
226  : HtmlLeafNode(parent, iter, contents) {
227  }
228 
229 
230 };
231 
234  public:
235  virtual ~HtmlDirectiveNode();
236  friend class HtmlParse;
237 
238  protected:
239  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
240  HtmlEventList* queue);
241 
242  private:
244  const StringPiece& contents,
245  const HtmlEventListIterator& iter)
246  : HtmlLeafNode(parent, iter, contents) {
247  }
248 
249 
250 };
251 
252 }
253 
254 #endif
Definition: html_node.h:103
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
GoogleString * mutable_contents()
Definition: html_node.h:129
Leaf node representing raw characters in HTML.
Definition: html_node.h:167
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
Definition: html_parse.h:88
Definition: html_element.h:42
virtual void MarkAsDead(const HtmlEventListIterator &end)
virtual HtmlEventListIterator end() const =0
Return an iterator pointing to the last event associated with this node.
Leaf node representing an HTML comment.
Definition: html_node.h:193
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual HtmlEventListIterator begin() const
Return an iterator pointing to the first event associated with this node.
Definition: html_node.h:111
Definition: arena.h:34
Definition: html_node.h:43
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)=0
Definition: html_testing_peer.h:33
Leaf node representing an HTML directive.
Definition: html_node.h:233
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
Leaf node representing a CDATA section.
Definition: html_node.h:147
Definition: html_lexer.h:45
Leaf node representing an HTML IE directive.
Definition: html_node.h:213
virtual void MarkAsDead(const HtmlEventListIterator &end)=0
virtual HtmlEventListIterator begin() const =0
Return an iterator pointing to the first event associated with this node.
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
HtmlNode(HtmlElement *parent)
Definition: html_node.h:71
virtual HtmlEventListIterator end() const
Return an iterator pointing to the last event associated with this node.
Definition: html_node.h:114