All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
ChildCountMatch.h
Go to the documentation of this file.
1 // Copyright 2015 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_CHILD_COUNT_MATCH_H_INCLUDED
16 #define HEXT_CHILD_COUNT_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::ChildCountMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 
24 #include <gumbo.h>
25 
26 
27 namespace hext {
28 
29 
30 /// Matches HTML elements that have a certain amount of children of type
31 /// element (excluding text nodes, document nodes and others).
32 ///
33 /// @par Example:
34 /// ~~~~~~~~~~~~~
35 /// GumboNode * none = ...; // <div>Text nodes are ignored</div>
36 /// GumboNode * two = ...; // <ul><li>first</li><li>last</li></ul>
37 ///
38 /// ChildCountMatch m_none(/* child count */ 0);
39 /// ChildCountMatch m_two (/* child count */ 2);
40 ///
41 /// assert( m_none.matches(none));
42 /// assert(!m_none.matches(two));
43 ///
44 /// assert( m_two.matches(two));
45 /// assert(!m_two.matches(none));
46 /// ~~~~~~~~~~~~~
47 class ChildCountMatch final : public Cloneable<ChildCountMatch, Match>
48 {
49 public:
50  /// Construct a ChildCountMatch that matches HTML elements with a child_count
51  /// amount of children.
52  explicit ChildCountMatch(unsigned int child_count) noexcept;
53 
54  /// Return true if node has child_count amount of children.
55  ///
56  /// @param node: A pointer to a GumboNode of type GUMBO_NODE_ELEMENT.
57  bool matches(const GumboNode * node) const noexcept override;
58 
59 private:
60  /// Return amount of node's children that have node type GUMBO_NODE_ELEMENT.
61  unsigned int count_child_elements(const GumboNode * node) const noexcept;
62 
63  /// The amount of children an HTML element must have in order to match.
64  unsigned int child_count_;
65 };
66 
67 
68 } // namespace hext
69 
70 
71 #endif // HEXT_CHILD_COUNT_MATCH_H_INCLUDED
72 
ChildCountMatch(unsigned int child_count) noexcept
Construct a ChildCountMatch that matches HTML elements with a child_count amount of children...
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Declares hext::Match.
bool matches(const GumboNode *node) const noexceptoverride
Return true if node has child_count amount of children.
Defines template hext::Cloneable.
Matches HTML elements that have a certain amount of children of type element (excluding text nodes...