libhext: C++ Library Documentation  1.0.12-3ea013c
FunctionMatch.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_FUNCTION_MATCH_H_INCLUDED
16 #define HEXT_FUNCTION_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::FunctionMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 #include "hext/MatchFunction.h"
24 #include "hext/Visibility.h"
25 
26 #include <gumbo.h>
27 
28 
29 namespace hext {
30 
31 
32 /// Matches if the result of applying a given MatchFunction to an HTML node
33 /// returns true.
34 ///
35 /// @par Example:
36 /// ~~~~~~~~~~~~~
37 /// GumboNode * div = ...; // <div>This is a div!</div>
38 /// GumboNode * span = ...; // <span>This is a span!</span>
39 ///
40 /// MatchFunction is_div = [](const GumboNode * node) {
41 /// return node->type == GUMBO_NODE_ELEMENT &&
42 /// node->v.element.tag == GUMBO_TAG_DIV;
43 /// };
44 ///
45 /// FunctionMatch m_is_div(is_div);
46 ///
47 /// assert( m_is_div.matches(div));
48 /// assert(!m_is_div.matches(span));
49 /// ~~~~~~~~~~~~~
50 class HEXT_PUBLIC FunctionMatch final : public Cloneable<FunctionMatch, Match>
51 {
52 public:
53  /// Constructs a FunctionMatch that matches HTML nodes for which a given
54  /// MatchFunction returns true.
55  ///
56  /// @param func: The MatchFunction that will be applied to an HTML
57  /// node.
58  explicit FunctionMatch(MatchFunction func);
59 
60  /// Returns true if the result of calling the given MatchFunction with node
61  /// as its first argument returns true.
62  ///
63  /// @param node: The node which is to be matched.
64  bool matches(const GumboNode * node) const override;
65 
66 private:
67  /// The MatchFunction that will be applied to an HTML node.
68  MatchFunction func_;
69 };
70 
71 
72 } // namespace hext
73 
74 
75 #endif // HEXT_FUNCTION_MATCH_H_INCLUDED
76 
Defines template hext::Cloneable.
Declares hext::MatchFunction.
Declares hext::Match.
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PUBLIC
Definition: Visibility.h:26
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:37
Matches if the result of applying a given MatchFunction to an HTML node returns true.
Definition: FunctionMatch.h:51
FunctionMatch(MatchFunction func)
Constructs a FunctionMatch that matches HTML nodes for which a given MatchFunction returns true.
bool matches(const GumboNode *node) const override
Returns true if the result of calling the given MatchFunction with node as its first argument returns...
std::function< bool(const GumboNode *)> MatchFunction
A type of std::function that receives an HTML element and returns a bool.
Definition: MatchFunction.h:30