Page Speed Optimization Libraries  1.2.24.1
net/instaweb/htmlparse/public/html_node.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_NODE_H_
00020 #define NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_NODE_H_
00021 
00022 #include <cstddef>
00023 #include <list>
00024 
00025 #include "base/logging.h"
00026 #include "net/instaweb/util/public/arena.h"
00027 #include "net/instaweb/util/public/basictypes.h"
00028 #include "net/instaweb/util/public/scoped_ptr.h"
00029 #include "net/instaweb/util/public/string.h"
00030 #include "net/instaweb/util/public/string_util.h"
00031 
00032 namespace net_instaweb {
00033 
00034 class HtmlElement;
00035 class HtmlEvent;
00036 
00037 typedef std::list<HtmlEvent*> HtmlEventList;
00038 typedef HtmlEventList::iterator HtmlEventListIterator;
00039 
00041 class HtmlNode {
00042  public:
00043   virtual ~HtmlNode();
00044   friend class HtmlParse;
00045 
00046   HtmlElement* parent() const { return parent_; }
00047   virtual bool live() const = 0;
00048 
00052   virtual void MarkAsDead(const HtmlEventListIterator& end) = 0;
00053 
00054   void* operator new(size_t size, Arena<HtmlNode>* arena) {
00055     return arena->Allocate(size);
00056   }
00057 
00058   void operator delete(void* ptr, Arena<HtmlNode>* arena) {
00059     LOG(FATAL) << "HtmlNode must not be deleted directly.";
00060   }
00061 
00062  protected:
00068   explicit HtmlNode(HtmlElement* parent) : parent_(parent) {}
00069 
00074   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00075                                 HtmlEventList* queue) = 0;
00076 
00078   virtual HtmlEventListIterator begin() const = 0;
00080   virtual HtmlEventListIterator end() const = 0;
00081 
00083   void operator delete(void* ptr) {
00084     LOG(FATAL) << "HtmlNode must not be deleted directly.";
00085   }
00086 
00087  private:
00088   friend class HtmlLexer;
00089   friend class HtmlTestingPeer;
00090 
00094   void set_parent(HtmlElement* parent) { parent_ = parent; }
00095 
00096   HtmlElement* parent_;
00097   DISALLOW_COPY_AND_ASSIGN(HtmlNode);
00098 };
00099 
00100 class HtmlLeafNode : public HtmlNode {
00101  public:
00102   virtual ~HtmlLeafNode();
00103   virtual bool live() const { return (data_.get() != NULL) && data_->is_live_; }
00104   virtual void MarkAsDead(const HtmlEventListIterator& end);
00105 
00106   const GoogleString& contents() const { return data_->contents_; }
00107   virtual HtmlEventListIterator begin() const {
00108     return data_->iter_;
00109   }
00110   virtual HtmlEventListIterator end() const {
00111     return data_->iter_;
00112   }
00113   void set_iter(const HtmlEventListIterator& iter) {
00114     data_->iter_ = iter;
00115   }
00116 
00117   void FreeData() { data_.reset(NULL); }
00118 
00119  protected:
00120   HtmlLeafNode(HtmlElement* parent, const HtmlEventListIterator& iter,
00121                const StringPiece& contents);
00122 
00125   GoogleString* mutable_contents() { return &data_->contents_; }
00126 
00127  private:
00128   struct Data {
00129     Data(const HtmlEventListIterator& iter, const StringPiece& contents)
00130         : contents_(contents.data(), contents.size()),
00131           is_live_(true),
00132           iter_(iter) {
00133     }
00134     GoogleString contents_;
00135     bool is_live_;
00136     HtmlEventListIterator iter_;
00137   };
00138 
00139   scoped_ptr<Data> data_;
00140 };
00141 
00143 class HtmlCdataNode : public HtmlLeafNode {
00144  public:
00145   virtual ~HtmlCdataNode();
00146   friend class HtmlParse;
00147 
00148  protected:
00149   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00150                                 HtmlEventList* queue);
00151 
00152  private:
00153   HtmlCdataNode(HtmlElement* parent,
00154                 const StringPiece& contents,
00155                 const HtmlEventListIterator& iter)
00156       : HtmlLeafNode(parent, iter, contents) {
00157   }
00158 
00159   DISALLOW_COPY_AND_ASSIGN(HtmlCdataNode);
00160 };
00161 
00163 class HtmlCharactersNode : public HtmlLeafNode {
00164  public:
00165   virtual ~HtmlCharactersNode();
00166   void Append(const StringPiece& str) {
00167     mutable_contents()->append(str.data(), str.size());
00168   }
00169   friend class HtmlParse;
00170 
00172   using HtmlLeafNode::mutable_contents;
00173 
00174  protected:
00175   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00176                                 HtmlEventList* queue);
00177 
00178  private:
00179   HtmlCharactersNode(HtmlElement* parent,
00180                      const StringPiece& contents,
00181                      const HtmlEventListIterator& iter)
00182       : HtmlLeafNode(parent, iter, contents) {
00183   }
00184 
00185   DISALLOW_COPY_AND_ASSIGN(HtmlCharactersNode);
00186 };
00187 
00189 class HtmlCommentNode : public HtmlLeafNode {
00190  public:
00191   virtual ~HtmlCommentNode();
00192   friend class HtmlParse;
00193 
00194  protected:
00195   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00196                                 HtmlEventList* queue);
00197 
00198  private:
00199   HtmlCommentNode(HtmlElement* parent,
00200                   const StringPiece& contents,
00201                   const HtmlEventListIterator& iter)
00202       : HtmlLeafNode(parent, iter, contents) {
00203   }
00204 
00205   DISALLOW_COPY_AND_ASSIGN(HtmlCommentNode);
00206 };
00207 
00209 class HtmlIEDirectiveNode : public HtmlLeafNode {
00210  public:
00211   virtual ~HtmlIEDirectiveNode();
00212   friend class HtmlParse;
00213 
00214  protected:
00215   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00216                                 HtmlEventList* queue);
00217 
00218  private:
00219   HtmlIEDirectiveNode(HtmlElement* parent,
00220                       const StringPiece& contents,
00221                       const HtmlEventListIterator& iter)
00222       : HtmlLeafNode(parent, iter, contents) {
00223   }
00224 
00225   DISALLOW_COPY_AND_ASSIGN(HtmlIEDirectiveNode);
00226 };
00227 
00229 class HtmlDirectiveNode : public HtmlLeafNode {
00230  public:
00231   virtual ~HtmlDirectiveNode();
00232   friend class HtmlParse;
00233 
00234  protected:
00235   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00236                                 HtmlEventList* queue);
00237 
00238  private:
00239   HtmlDirectiveNode(HtmlElement* parent,
00240                     const StringPiece& contents,
00241                     const HtmlEventListIterator& iter)
00242       : HtmlLeafNode(parent, iter, contents) {
00243   }
00244 
00245   DISALLOW_COPY_AND_ASSIGN(HtmlDirectiveNode);
00246 };
00247 
00248 }  
00249 
00250 #endif  ///< NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_NODE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines