Page Speed Optimization Libraries  1.5.27.2
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 
00043 class HtmlNode {
00044  public:
00045   virtual ~HtmlNode();
00046   friend class HtmlParse;
00047 
00048   HtmlElement* parent() const { return parent_; }
00049   virtual bool live() const = 0;
00050 
00054   virtual void MarkAsDead(const HtmlEventListIterator& end) = 0;
00055 
00056   void* operator new(size_t size, Arena<HtmlNode>* arena) {
00057     return arena->Allocate(size);
00058   }
00059 
00060   void operator delete(void* ptr, Arena<HtmlNode>* arena) {
00061     LOG(FATAL) << "HtmlNode must not be deleted directly.";
00062   }
00063 
00064  protected:
00070   explicit HtmlNode(HtmlElement* parent) : parent_(parent) {}
00071 
00076   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00077                                 HtmlEventList* queue) = 0;
00078 
00080   virtual HtmlEventListIterator begin() const = 0;
00082   virtual HtmlEventListIterator end() const = 0;
00083 
00085   void operator delete(void* ptr) {
00086     LOG(FATAL) << "HtmlNode must not be deleted directly.";
00087   }
00088 
00089  private:
00090   friend class HtmlLexer;
00091   friend class HtmlTestingPeer;
00092 
00096   void set_parent(HtmlElement* parent) { parent_ = parent; }
00097 
00098   HtmlElement* parent_;
00099   DISALLOW_COPY_AND_ASSIGN(HtmlNode);
00100 };
00101 
00102 class HtmlLeafNode : public HtmlNode {
00103  public:
00104   virtual ~HtmlLeafNode();
00105   virtual bool live() const { return (data_.get() != NULL) && data_->is_live_; }
00106   virtual void MarkAsDead(const HtmlEventListIterator& end);
00107 
00108   const GoogleString& contents() const { return data_->contents_; }
00109   virtual HtmlEventListIterator begin() const {
00110     return data_->iter_;
00111   }
00112   virtual HtmlEventListIterator end() const {
00113     return data_->iter_;
00114   }
00115   void set_iter(const HtmlEventListIterator& iter) {
00116     data_->iter_ = iter;
00117   }
00118 
00119   void FreeData() { data_.reset(NULL); }
00120 
00121  protected:
00122   HtmlLeafNode(HtmlElement* parent, const HtmlEventListIterator& iter,
00123                const StringPiece& contents);
00124 
00127   GoogleString* mutable_contents() { return &data_->contents_; }
00128 
00129  private:
00130   struct Data {
00131     Data(const HtmlEventListIterator& iter, const StringPiece& contents)
00132         : contents_(contents.data(), contents.size()),
00133           is_live_(true),
00134           iter_(iter) {
00135     }
00136     GoogleString contents_;
00137     bool is_live_;
00138     HtmlEventListIterator iter_;
00139   };
00140 
00141   scoped_ptr<Data> data_;
00142 };
00143 
00145 class HtmlCdataNode : public HtmlLeafNode {
00146  public:
00147   virtual ~HtmlCdataNode();
00148   friend class HtmlParse;
00149 
00150  protected:
00151   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00152                                 HtmlEventList* queue);
00153 
00154  private:
00155   HtmlCdataNode(HtmlElement* parent,
00156                 const StringPiece& contents,
00157                 const HtmlEventListIterator& iter)
00158       : HtmlLeafNode(parent, iter, contents) {
00159   }
00160 
00161   DISALLOW_COPY_AND_ASSIGN(HtmlCdataNode);
00162 };
00163 
00165 class HtmlCharactersNode : public HtmlLeafNode {
00166  public:
00167   virtual ~HtmlCharactersNode();
00168   void Append(const StringPiece& str) {
00169     mutable_contents()->append(str.data(), str.size());
00170   }
00171   friend class HtmlParse;
00172 
00174   using HtmlLeafNode::mutable_contents;
00175 
00176  protected:
00177   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00178                                 HtmlEventList* queue);
00179 
00180  private:
00181   HtmlCharactersNode(HtmlElement* parent,
00182                      const StringPiece& contents,
00183                      const HtmlEventListIterator& iter)
00184       : HtmlLeafNode(parent, iter, contents) {
00185   }
00186 
00187   DISALLOW_COPY_AND_ASSIGN(HtmlCharactersNode);
00188 };
00189 
00191 class HtmlCommentNode : public HtmlLeafNode {
00192  public:
00193   virtual ~HtmlCommentNode();
00194   friend class HtmlParse;
00195 
00196  protected:
00197   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00198                                 HtmlEventList* queue);
00199 
00200  private:
00201   HtmlCommentNode(HtmlElement* parent,
00202                   const StringPiece& contents,
00203                   const HtmlEventListIterator& iter)
00204       : HtmlLeafNode(parent, iter, contents) {
00205   }
00206 
00207   DISALLOW_COPY_AND_ASSIGN(HtmlCommentNode);
00208 };
00209 
00211 class HtmlIEDirectiveNode : public HtmlLeafNode {
00212  public:
00213   virtual ~HtmlIEDirectiveNode();
00214   friend class HtmlParse;
00215 
00216  protected:
00217   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00218                                 HtmlEventList* queue);
00219 
00220  private:
00221   HtmlIEDirectiveNode(HtmlElement* parent,
00222                       const StringPiece& contents,
00223                       const HtmlEventListIterator& iter)
00224       : HtmlLeafNode(parent, iter, contents) {
00225   }
00226 
00227   DISALLOW_COPY_AND_ASSIGN(HtmlIEDirectiveNode);
00228 };
00229 
00231 class HtmlDirectiveNode : public HtmlLeafNode {
00232  public:
00233   virtual ~HtmlDirectiveNode();
00234   friend class HtmlParse;
00235 
00236  protected:
00237   virtual void SynthesizeEvents(const HtmlEventListIterator& iter,
00238                                 HtmlEventList* queue);
00239 
00240  private:
00241   HtmlDirectiveNode(HtmlElement* parent,
00242                     const StringPiece& contents,
00243                     const HtmlEventListIterator& iter)
00244       : HtmlLeafNode(parent, iter, contents) {
00245   }
00246 
00247   DISALLOW_COPY_AND_ASSIGN(HtmlDirectiveNode);
00248 };
00249 
00250 }  
00251 
00252 #endif  ///< NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_NODE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines