libhext: C++ Library Documentation  1.0.12-3ea013c
ChildCountMatch.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_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 #include "hext/Visibility.h"
24 
25 #include <gumbo.h>
26 
27 
28 namespace hext {
29 
30 
31 /// Matches HTML elements that have a certain amount of children of type
32 /// element (excluding text nodes, document nodes and others).
33 ///
34 /// @par Example:
35 /// ~~~~~~~~~~~~~
36 /// GumboNode * none = ...; // <div>Text nodes are ignored</div>
37 /// GumboNode * two = ...; // <ul><li>first</li><li>last</li></ul>
38 ///
39 /// ChildCountMatch m_none(/* child count */ 0);
40 /// ChildCountMatch m_two (/* child count */ 2);
41 ///
42 /// assert( m_none.matches(none));
43 /// assert(!m_none.matches(two));
44 ///
45 /// assert( m_two.matches(two));
46 /// assert(!m_two.matches(none));
47 /// ~~~~~~~~~~~~~
49  : public Cloneable<ChildCountMatch, Match>
50 {
51 public:
52  /// Construct a ChildCountMatch that matches HTML elements with a child_count
53  /// amount of children.
54  explicit ChildCountMatch(unsigned int child_count) noexcept;
55 
56  /// Return true if node has child_count amount of children.
57  ///
58  /// @param node: A pointer to a GumboNode of type GUMBO_NODE_ELEMENT.
59  bool matches(const GumboNode * node) const noexcept override;
60 
61 private:
62  /// Return amount of node's children that have node type GUMBO_NODE_ELEMENT.
64  unsigned int count_child_elements(const GumboNode * node) const noexcept;
65 
66  /// The amount of children an HTML element must have in order to match.
67  unsigned int child_count_;
68 };
69 
70 
71 } // namespace hext
72 
73 
74 #endif // HEXT_CHILD_COUNT_MATCH_H_INCLUDED
75 
Defines template hext::Cloneable.
Declares hext::Match.
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PRIVATE
Definition: Visibility.h:27
#define HEXT_PUBLIC
Definition: Visibility.h:26
Matches HTML elements that have a certain amount of children of type element (excluding text nodes,...
ChildCountMatch(unsigned int child_count) noexcept
Construct a ChildCountMatch that matches HTML elements with a child_count amount of children.
bool matches(const GumboNode *node) const noexcept override
Return true if node has child_count amount of children.
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:37