Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
html_element.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_ELEMENT_H_
20 #define PAGESPEED_KERNEL_HTML_HTML_ELEMENT_H_
21 
29 
30 namespace net_instaweb {
31 
42 class HtmlElement : public HtmlNode {
43  public:
50  enum Style {
57  };
58 
60  enum QuoteStyle {
61  NO_QUOTE,
62  SINGLE_QUOTE,
63  DOUBLE_QUOTE
64  };
65 
66  class Attribute : public InlineSListElement<Attribute> {
67  public:
71 
75 
79  StringPiece name_str() const { return name_.value(); }
80 
84  HtmlName::Keyword keyword() const { return name_.keyword(); }
85 
86  HtmlName name() const { return name_; }
87  void set_name(const HtmlName& name) { name_ = name; }
88 
91  const char* escaped_value() const { return escaped_value_.get(); }
92 
118  const char* DecodedValueOrNull() const {
119  if (!decoded_value_computed_) {
120  ComputeDecodedValue();
121  }
122  return decoded_value_.get();
123  }
124 
125  void set_decoding_error(bool x) { decoding_error_ = x; }
126  bool decoding_error() const {
127  if (!decoded_value_computed_) {
128  ComputeDecodedValue();
129  }
130  return decoding_error_;
131  }
132 
135  QuoteStyle quote_style() const { return quote_style_; }
136 
138  const char* quote_str() const;
139 
153 
158  void SetValue(const StringPiece& value);
159 
163  void SetEscapedValue(const StringPiece& value);
164 
165  void set_quote_style(QuoteStyle new_quote_style) {
166  quote_style_ = new_quote_style;
167  }
168 
169  friend class HtmlElement;
170 
171  private:
172  void ComputeDecodedValue() const;
173 
175  Attribute(const HtmlName& name, const StringPiece& escaped_value,
177 
178  static inline void CopyValue(const StringPiece& src,
179  scoped_array<char>* dst);
180 
181  HtmlName name_;
182  QuoteStyle quote_style_ : 8;
183  mutable bool decoding_error_;
184  mutable bool decoded_value_computed_;
185 
194  scoped_array<char> escaped_value_;
195 
212  mutable scoped_array<char> decoded_value_;
213 
214 
215  };
216 
217  typedef InlineSList<Attribute> AttributeList;
218  typedef InlineSList<Attribute>::Iterator AttributeIterator;
219  typedef InlineSList<Attribute>::ConstIterator AttributeConstIterator;
220 
221  virtual ~HtmlElement();
222 
229  virtual bool live() const { return (data_.get() != NULL) && data_->live_; }
230 
231  virtual void MarkAsDead(const HtmlEventListIterator& end);
232 
235  void AddAttribute(const Attribute& attr);
236 
246  void AddAttribute(const HtmlName& name,
247  const StringPiece& decoded_value,
248  QuoteStyle quote_style);
250  void AddEscapedAttribute(const HtmlName& name,
251  const StringPiece& escaped_value,
252  QuoteStyle quote_style);
253 
257  bool DeleteAttribute(StringPiece name);
258 
262  const Attribute* FindAttribute(HtmlName::Keyword keyword) const;
264  const HtmlElement* const_this = this;
265  const Attribute* result = const_this->FindAttribute(keyword);
266  return const_cast<Attribute*>(result);
267  }
268 
269  const Attribute* FindAttribute(StringPiece name) const;
270  Attribute* FindAttribute(StringPiece name) {
271  const HtmlElement* const_this = this;
272  const Attribute* result = const_this->FindAttribute(name);
273  return const_cast<Attribute*>(result);
274  }
275 
276  bool HasAttribute(HtmlName::Keyword keyword) const {
277  const Attribute* attribute = FindAttribute(keyword);
278  return attribute != nullptr;
279  }
280 
290  const char* AttributeValue(HtmlName::Keyword name) const {
291  const Attribute* attribute = FindAttribute(name);
292  if (attribute != NULL) {
293  return attribute->DecodedValueOrNull();
294  }
295  return NULL;
296  }
297 
306  const char* EscapedAttributeValue(HtmlName::Keyword name) const {
307  const Attribute* attribute = FindAttribute(name);
308  if (attribute != NULL) {
309  return attribute->escaped_value();
310  }
311  return NULL;
312  }
313 
317  StringPiece name_str() const { return data_->name_.value(); }
318 
322  HtmlName::Keyword keyword() const { return data_->name_.keyword(); }
323 
324  const HtmlName& name() const { return data_->name_; }
325 
329  void set_name(const HtmlName& new_tag) { data_->name_ = new_tag; }
330 
331  const AttributeList& attributes() const { return data_->attributes_; }
332  AttributeList* mutable_attributes() { return &data_->attributes_; }
333 
334  friend class HtmlParse;
335  friend class HtmlLexer;
336 
337  Style style() const { return data_->style_; }
338  void set_style(Style style) { data_->style_ = style; }
339 
342  virtual GoogleString ToString() const;
343  void DebugPrint() const;
344 
345  int begin_line_number() const { return data_->begin_line_number_; }
346  int end_line_number() const { return data_->end_line_number_; }
347 
348  protected:
349  virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
350  HtmlEventList* queue);
351 
352  virtual HtmlEventListIterator begin() const { return data_->begin_; }
353  virtual HtmlEventListIterator end() const { return data_->end_; }
354 
355  private:
358  struct Data {
359  Data(const HtmlName& name,
360  const HtmlEventListIterator& begin,
361  const HtmlEventListIterator& end);
362  ~Data();
363 
366  static const unsigned kMaxLineNumber = 0x00ffffff;
367 
379  unsigned begin_line_number_ : 24;
380  unsigned live_ : 8;
381  unsigned end_line_number_ : 24;
382  Style style_ : 8;
383 
384  HtmlName name_;
385  AttributeList attributes_;
386  HtmlEventListIterator begin_;
387  HtmlEventListIterator end_;
388  };
389 
393  void set_begin(const HtmlEventListIterator& begin) { data_->begin_ = begin; }
394  void set_end(const HtmlEventListIterator& end) { data_->end_ = end; }
395 
396  void set_begin_line_number(int line) { data_->begin_line_number_ = line; }
397  void set_end_line_number(int line) { data_->end_line_number_ = line; }
398 
400  HtmlElement(HtmlElement* parent, const HtmlName& name,
401  const HtmlEventListIterator& begin,
402  const HtmlEventListIterator& end);
403 
409  void FreeData() { data_.reset(NULL); }
410 
411  scoped_ptr<Data> data_;
412 
413 
414 };
415 
416 }
417 
418 #endif
StringPiece name_str() const
Definition: html_element.h:317
Definition: html_element.h:66
void set_name(const HtmlName &new_tag)
Definition: html_element.h:329
E.g. anchor
Definition: html_element.h:53
virtual HtmlEventListIterator end() const
Return an iterator pointing to the last event associated with this node.
Definition: html_element.h:353
virtual GoogleString ToString() const
QuoteStyle
Various ways things can be quoted (or not)
Definition: html_element.h:60
const char * quote_str() const
Textual form of quote for printing.
synthesized tag, or not yet closed in source
Definition: html_element.h:51
const char * EscapedAttributeValue(HtmlName::Keyword name) const
Definition: html_element.h:306
virtual bool live() const
Definition: html_element.h:229
bool DeleteAttribute(HtmlName::Keyword keyword)
virtual void MarkAsDead(const HtmlEventListIterator &end)
Definition: html_element.h:42
const Attribute * FindAttribute(HtmlName::Keyword keyword) const
QuoteStyle quote_style() const
Definition: html_element.h:135
virtual void SynthesizeEvents(const HtmlEventListIterator &iter, HtmlEventList *queue)
void AddEscapedAttribute(const HtmlName &name, const StringPiece &escaped_value, QuoteStyle quote_style)
As AddAttribute, but assumes value has been escaped for html output.
E.g. <img...> <meta...> <link...> <br...> <input...>
Definition: html_element.h:52
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Style
Definition: html_element.h:50
HtmlName::Keyword keyword() const
Definition: html_element.h:322
E.g. <head>
Definition: html_element.h:54
Keyword keyword() const
HtmlName's should be normally constructed using HtmlParse::MakeName.
Definition: html_name.h:260
const char * DecodedValueOrNull() const
Definition: html_element.h:118
Definition: html_node.h:43
StringPiece value() const
Return the atom string, which may not be case folded.
Definition: html_name.h:263
HtmlName::Keyword keyword() const
Definition: html_element.h:84
const char * AttributeValue(HtmlName::Keyword name) const
Definition: html_element.h:290
Definition: html_name.h:31
const char * escaped_value() const
Definition: html_element.h:91
StringPiece name_str() const
Definition: html_element.h:79
void SetEscapedValue(const StringPiece &value)
Keyword
Definition: html_name.h:39
Definition: inline_slist.h:39
virtual HtmlEventListIterator begin() const
Return an iterator pointing to the first event associated with this node.
Definition: html_element.h:352
void AddAttribute(const Attribute &attr)
void SetValue(const StringPiece &value)
Programatically hidden element.
Definition: html_element.h:56
Was never closed in source, so don't serialize close-tag.
Definition: html_element.h:55