Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
html_keywords.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_KEYWORDS_H_
20 #define PAGESPEED_KERNEL_HTML_HTML_KEYWORDS_H_
21 
22 #include <algorithm>
23 #include <vector>
24 
31 
32 namespace net_instaweb {
33 
34 class MessageHandler;
35 class Writer;
36 
40 class HtmlKeywords {
41  public:
46  static void Init();
47 
50  static void ShutDown();
51 
53  static const StringPiece* KeywordToString(HtmlName::Keyword keyword) {
54  if (keyword < HtmlName::kNotAKeyword) {
55  return &singleton_->keyword_vector_[keyword];
56  } else {
57  return NULL;
58  }
59  }
60 
63  static StringPiece Escape(const StringPiece& unescaped, GoogleString* buf) {
64  return singleton_->EscapeHelper(unescaped, buf);
65  }
66 
74  static StringPiece Unescape(const StringPiece& escaped, GoogleString* buf,
75  bool* decoding_error) {
76  return singleton_->UnescapeHelper(escaped, buf, decoding_error);
77  }
78 
89 
94  return std::binary_search(singleton_->auto_close_.begin(),
95  singleton_->auto_close_.end(),
96  MakeKeywordPair(k1, k2));
97  }
98 
103  return std::binary_search(singleton_->contained_.begin(),
104  singleton_->contained_.end(),
105  MakeKeywordPair(k1, k2));
106  }
107 
114  return std::binary_search(singleton_->optionally_closed_.begin(),
115  singleton_->optionally_closed_.end(),
116  keyword);
117  }
118 
123  static bool WritePre(StringPiece text, StringPiece style,
124  Writer* writer, MessageHandler* handler);
125 
126  private:
127  typedef int32 KeywordPair;
128  typedef std::vector<KeywordPair> KeywordPairVec;
129  typedef std::vector<HtmlName::Keyword> KeywordVec;
130 
131  HtmlKeywords();
132  const char* UnescapeAttributeValue();
133  void InitEscapeSequences();
134  void InitAutoClose();
135  void InitContains();
136  void InitOptionallyClosedKeywords();
137 
155  bool TryUnescape(bool accumulate_numeric_code,
156  uint32 numeric_value,
157  const GoogleString& escape,
158  bool was_terminated,
159  GoogleString* buf) const;
160 
162  static KeywordPair MakeKeywordPair(HtmlName::Keyword k1,
163  HtmlName::Keyword k2) {
164  return (static_cast<KeywordPair>(k1) << 16) | static_cast<KeywordPair>(k2);
165  }
166 
171  void AddCrossProduct(const StringPiece& k1_list, const StringPiece& k2_list,
172  KeywordPairVec* kmap);
173  void AddAutoClose(const StringPiece& k1_list, const StringPiece& k2_list) {
174  AddCrossProduct(k1_list, k2_list, &auto_close_);
175  }
176  void AddContained(const StringPiece& k1_list, const StringPiece& k2_list) {
177  AddCrossProduct(k1_list, k2_list, &contained_);
178  }
179 
181  void AddToSet(const StringPiece& klist, KeywordVec* kset);
182 
183  static HtmlKeywords* singleton_;
184 
185  StringPiece EscapeHelper(const StringPiece& unescaped,
186  GoogleString* buf) const;
187  StringPiece UnescapeHelper(const StringPiece& escaped,
188  GoogleString* buf,
189  bool* decoding_error) const;
190 
196  typedef sparse_hash_map<
197  GoogleString, const char*,
198  CaseFoldStringHash,
199  CaseFoldStringEqual> StringStringSparseHashMapInsensitive;
200  typedef sparse_hash_map<
201  GoogleString, const char*,
202  CasePreserveStringHash> StringStringSparseHashMapSensitive;
203 
204  StringStringSparseHashMapInsensitive unescape_insensitive_map_;
205  StringStringSparseHashMapSensitive unescape_sensitive_map_;
206  StringStringSparseHashMapSensitive escape_map_;
207 
210  StringPieceVector keyword_vector_;
211 
214  KeywordPairVec auto_close_;
215  KeywordPairVec contained_;
216  KeywordVec optionally_closed_;
217 
218 
219 };
220 
221 }
222 
223 #endif
static bool IsOptionallyClosedTag(HtmlName::Keyword keyword)
Definition: html_keywords.h:113
Definition: html_keywords.h:40
static bool WritePre(StringPiece text, StringPiece style, Writer *writer, MessageHandler *handler)
static bool IsAutoClose(HtmlName::Keyword k1, HtmlName::Keyword k2)
Definition: html_keywords.h:93
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
static StringPiece Escape(const StringPiece &unescaped, GoogleString *buf)
Definition: html_keywords.h:63
static StringPiece Unescape(const StringPiece &escaped, GoogleString *buf, bool *decoding_error)
Definition: html_keywords.h:74
Interface for writing bytes to an output stream.
Definition: writer.h:29
static bool IsContained(HtmlName::Keyword k1, HtmlName::Keyword k2)
Definition: html_keywords.h:102
Keyword
Definition: html_name.h:39
Definition: message_handler.h:39
static const StringPiece * KeywordToString(HtmlName::Keyword keyword)
Returns an HTML keyword as a string, or NULL if not a keyword.
Definition: html_keywords.h:53