libhext: C++ Library Documentation  1.0.12-3ea013c
Html.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_HTML_H_INCLUDED
16 #define HEXT_HTML_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::Html
20 
21 #include "hext/Visibility.h"
22 
23 #include <cstddef>
24 
25 #include <gumbo.h>
26 
27 
28 namespace hext {
29 
30 
31 /// A RAII wrapper for Gumbo.
32 ///
33 /// All HTML is expected to be UTF-8 encoded.
34 /// Gumbo will parse anything you throw at it. When given invalid or incomplete
35 /// HTML it will even fix it for you.
36 ///
37 /// @par Example:
38 /// ~~~~~~~~~~~~~
39 /// Html page("<html><body>This is a string containing html</body></html>");
40 /// const GumboNode * root = page.root();
41 /// // root now points to the top most HTML element (<html>).
42 /// assert(root);
43 /// Rule html_root(HtmlTag::HTML);
44 /// assert(html_root.matches(root));
45 /// ~~~~~~~~~~~~~
47 {
48 public:
49  /// Constructs an Html from a non-owning null-terminated string.
50  ///
51  /// @warning The buffer must stay alive until the destruction of this
52  /// instance.
53  ///
54  /// @param buffer: A null-terminated string containing HTML.
55  explicit Html(const char * buffer) noexcept;
56 
57  /// Constructs an Html from a non-owning pointer.
58  ///
59  /// @warning The buffer must stay alive until the destruction of this
60  /// instance.
61  ///
62  /// @param buffer: A string containing HTML.
63  /// @param size: The length of the given buffer.
64  Html(const char * buffer, std::size_t size) noexcept;
65 
66  ~Html() noexcept;
67  Html(Html&&) noexcept = default;
68  Html& operator=(Html&&) noexcept = default;
69 
70  /// Returns a non-owning pointer to the root node of the HTML document.
71  ///
72  /// @warning The pointer may not be used after the destruction of this
73  /// instance.
74  const GumboNode * root() const noexcept;
75 
76 private:
77  Html(const Html&) = delete;
78  Html& operator=(const Html&) = delete;
79 
80  /// Gumbo's resource handle.
81  GumboOutput * g_outp_;
82 };
83 
84 
85 } // namespace hext
86 
87 
88 #endif // HEXT_HTML_H_INCLUDED
89 
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PUBLIC
Definition: Visibility.h:26
A RAII wrapper for Gumbo.
Definition: Html.h:47
Html(const char *buffer, std::size_t size) noexcept
Constructs an Html from a non-owning pointer.
~Html() noexcept
Html(const char *buffer) noexcept
Constructs an Html from a non-owning null-terminated string.