OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / html / html.h
1 #ifndef HTML_H
2 #define HTML_H
3
4 #include <QByteArray>
5 #include <QString>
6 #include <QMutex>
7
8 #include <memory>
9 #include <iterator>
10 #include <map>
11 #include <list>
12 #include <vector>
13 #include <stack>
14
15 QT_BEGIN_NAMESPACE
16 QT_END_NAMESPACE
17
18 //class HtmlHandler;
19 class HtmlDocument;
20 class HtmlNode;
21 //class HtmlElement;
22 class HtmlPrivate;
23 struct HtmlAttribute;
24
25 class Html
26 {
27 public:
28         typedef QString string_type;
29         typedef HtmlDocument document_type;
30         typedef HtmlNode node_type;
31
32         typedef std::shared_ptr<HtmlPrivate> pointer;
33         typedef std::shared_ptr<HtmlPrivate const> const_pointer;
34         typedef std::weak_ptr<HtmlPrivate> weak_pointer;
35         typedef std::weak_ptr<HtmlPrivate const> const_weak_pointer;
36
37         typedef std::list<HtmlAttribute> attribute_list_type;
38         typedef attribute_list_type::iterator attribute_iterator;
39         typedef attribute_list_type::const_iterator const_attribute_iterator;
40
41 public:
42         enum : int { Unknown = 0, };
43
44         enum Type : int
45         {
46                 Null                  = 0,
47                 Element               = 1,
48                 Attribute             = 2,
49                 Text                  = 3,
50                 CDATASection          = 4,
51                 EntityReference       = 5,
52                 Entity                = 6,
53                 ProcessingInstruction = 7,
54                 Comment               = 8,
55                 Document              = 9,
56                 DocumentType          = 10,
57                 DocumentFragment      = 11,
58                 Notation              = 12,
59         };
60
61         enum Class : int
62         {
63                 A = 10000, Abbr, Acronym, Address, Applet, Area, Article, Aside, Audio,
64                 B, Base, Basefont, Bdi, Bdo, Bgsound, Big, Blink, Blockquote, Body, Br,
65                 Button,
66                 Canvas, Caption, Center, Cite, Code, Col, Colgroup, Command,
67                 Datalist, Dd, Del, Details, Dfn, Dir, Div, Dl, Dt,
68                 Em, Embed,
69                 Fieldset, Figcaption, Figure, Font, Footer, Form, Frame, Frameset,
70                 H1, H2, H3, H4, H5, H6, Head, Header, Hgroup, Hr, Htm,
71                 I, Iframe, Img, Input, Ins, Isindex,
72                 Kbd, Keygen,
73                 Label, Legend, Li, Link, Listing,
74                 Map, Mark, Marquee, Menu, Meta, Meter,
75                 Nav, Nobr, Noframes, Noscript,
76                 Object, Ol, Optgroup, Option, Output,
77                 P, Param, Plaintext, Pre, Progress,
78                 Q,
79                 Rp, Rt, Ruby,
80                 S, Samp, Script, Section, Select, Small, Source, Spacer, Span, Strike,
81                 Strong, Style, Sub, Summary, Sup,
82                 Table, Tbody, Td, Textarea, Tfoot, Th, Thead, Time, Title, Tr, Track, Tt,
83                 U, Ul,
84                 Var, Video,
85                 Wbr,
86                 Xmp,
87         };
88
89         enum Category : int
90         {
91                 Metadata = 0x1, Flow = 0x2, Sectioning = 0x4, Heading = 0x8,
92                 Phrasing = 0x10, Embeded = 0x20, Interactive = 0x40,
93                 FormAssociated = 0x80, Transparent = 0x100, Other = 0x200,
94
95                 Inline = 0x400, Block = 0x800,
96                 Splitter = 0x1000,
97                 Empty = 0x2000,
98                 Ignore = 0x4000,
99         };
100
101         enum Information : int
102         {
103                 Whitespace = 0x1,
104         };
105
106         enum Place : int { Open, Close, Single, };
107 };
108
109 struct HtmlAttribute : public Html
110 {
111         HtmlAttribute(string_type const &nuri_, string_type const &lname_,
112                                   string_type const &qname_, string_type const &value_);
113         HtmlAttribute(string_type const &lname_, string_type const &value_);
114
115         bool operator ==(string_type const &lname_) const;
116
117         string_type namespace_uri;
118         string_type local_name;
119         string_type q_name;
120         string_type value;
121 };
122
123 class HtmlAttributes : public Html
124 {
125 public:
126         HtmlAttributes(pointer node);
127
128         attribute_iterator begin();
129         attribute_iterator end();
130
131 private:
132         pointer m_node;
133 };
134
135 class HtmlRange : public Html
136 {
137 public:
138         class iterator : public std::iterator<std::bidirectional_iterator_tag, node_type>
139         {
140         public:
141                 iterator();
142                 iterator(Html::pointer const &current);
143
144                 iterator& operator ++();
145                 iterator operator ++(int);
146
147                 bool operator ==(iterator const &rhs) const;
148                 bool operator !=(iterator const &rhs) const;
149
150                 HtmlNode& operator *() const;
151                 HtmlNode* operator ->() const;
152
153         private:
154                 Html::pointer m_current;
155         };
156
157 public:
158         HtmlRange(){}
159         HtmlRange(pointer begin_, pointer tail_);
160         HtmlRange(HtmlNode begin_, HtmlNode tail_);
161
162         iterator begin();
163         iterator end();
164         //pointer common_ancestor();
165         //HtmlRange& remove();
166
167         pointer wrap(Html::string_type const &name_);
168         pointer unwrap();
169         void expand();
170
171         string_type debug_dump();
172
173 private:
174         void adjust();
175         void check(std::vector<pointer> &under_, std::vector<pointer> &over_);
176         void reparent(pointer begin_, pointer tail_);
177
178 private:
179         pointer m_begin;
180         pointer m_tail;
181 };
182
183 class HtmlNode : public Html
184 {
185         friend class HtmlDocument;
186         typedef HtmlNode self_type;
187
188 public:
189         class child_iterator : public std::iterator<
190                         std::bidirectional_iterator_tag, node_type>
191         {
192                 friend class HtmlNode;
193         public:
194                 child_iterator();
195                 child_iterator(Html::pointer const &current);
196
197                 child_iterator& operator ++();
198                 child_iterator operator ++(int);
199
200                 bool operator ==(child_iterator const &rhs) const;
201                 bool operator !=(child_iterator const &rhs) const;
202
203                 HtmlNode& operator *() const;
204                 HtmlNode* operator ->() const;
205
206         private:
207                 Html::pointer m_current;
208         };
209
210 public:
211         HtmlNode();
212         HtmlNode(weak_pointer const &pointer_);
213         HtmlNode(HtmlNode const &rhs);
214         virtual ~HtmlNode();
215
216         // baseUri();
217
218         bool equals(HtmlNode const &rhs) const;
219         bool containes(HtmlNode const &node) const; // 未実装
220
221         HtmlAttributes attributes();
222
223         child_iterator begin();
224         child_iterator end();
225
226         HtmlRange range();
227
228         HtmlNode parent();
229         HtmlNode first();
230         HtmlNode first(string_type const &name_);
231         HtmlNode last();
232         HtmlNode last(string_type const &name_);
233         HtmlNode next();
234         HtmlNode previous();
235
236         string_type content() const; // 未実装
237
238         HtmlNode clone(bool deep = true) const; // 未実装
239
240         HtmlNode insert(string_type const &name_, child_iterator place);
241         HtmlNode insert_text(string_type const &text, child_iterator place);
242         HtmlNode insert_comment(string_type const &comment, child_iterator place);
243         HtmlNode insert_cdata(string_type const &cdata, child_iterator place);
244         HtmlNode insert(HtmlRange range);
245
246         HtmlNode remove(HtmlNode node);
247         HtmlNode replace(HtmlNode const &newChild, const HtmlNode &oldChild);
248
249         bool operator ==(HtmlNode const &rhs) const;
250         bool operator !=(HtmlNode const &rhs) const;
251         operator bool() const;
252
253         void operator =(HtmlNode const &rhs);
254
255         // ------------------------------------------------------------------------
256
257         virtual document_type* document();
258         virtual document_type const* document() const;
259
260         virtual int type() const;
261         virtual int place() const;
262
263         virtual int tclass() const;
264         virtual string_type tname() const;
265         virtual int tcategory() const;
266         virtual int tinfo() const;
267
268         virtual string_type name() const;
269         virtual string_type value() const;
270         virtual void set_value(string_type const &value_);
271
272         // 属性
273         virtual attribute_iterator abegin();
274         virtual attribute_iterator aend();
275         virtual const_attribute_iterator abegin() const;
276         virtual const_attribute_iterator aend() const;
277
278         virtual bool has_attribute(string_type const &name_) const;
279         virtual string_type attribute(string_type const &name_) const;
280         virtual HtmlNode& set_attribute(string_type const &name_, string_type const &value_);
281         virtual HtmlNode& set_attribute(string_type const &name_, int value_);
282
283         // 文字列化
284         virtual string_type to_string() const;
285         virtual string_type debug_dump() const;
286
287         virtual pointer lself();
288         virtual const_pointer lself() const;
289         virtual pointer lbegin();
290         virtual pointer ltail();
291
292 protected:
293         pointer m_next; /*!< 次のデータ実体 */
294 };
295
296
297 class HtmlDocument : public HtmlNode
298 {
299         //friend class HtmlHandler;
300         //friend class HtmlDocumentOpenPrivate;
301
302 public:
303         enum Mode : int
304         {
305                 Xhtml, Xml,
306         };
307
308 public:
309         HtmlDocument();
310         HtmlDocument(QByteArray html);
311         ~HtmlDocument();
312
313         void clear();
314
315         bool set_content(QByteArray const &html, int mode = Xhtml);
316         QByteArray to_byte_array() const;
317         string_type to_string() const;
318 private:
319         bool parse(QByteArray bytes);
320 public:
321         pointer lbegin();
322         pointer set_lbegin(pointer const &begin_);
323
324         int find_tclass(string_type const &tname_) const;
325 private:
326         void setup_tclasses();
327
328 private:
329         static QMutex s_mutex;
330         std::map<string_type const, int const> m_tclasses;
331 };
332
333 #endif // HTML_H