All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
FunctionMatch.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_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 
25 #include <gumbo.h>
26 
27 
28 namespace hext {
29 
30 
31 /// Matches if the result of applying a given MatchFunction to an HTML node
32 /// returns true.
33 ///
34 /// @par Example:
35 /// ~~~~~~~~~~~~~
36 /// GumboNode * div = ...; // <div>This is a div!</div>
37 /// GumboNode * span = ...; // <span>This is a span!</span>
38 ///
39 /// MatchFunction is_div = [](const GumboNode * node) {
40 /// return node->type == GUMBO_NODE_ELEMENT &&
41 /// node->v.element.tag == GUMBO_TAG_DIV;
42 /// };
43 ///
44 /// FunctionMatch m_is_div(is_div);
45 ///
46 /// assert( m_is_div.matches(div));
47 /// assert(!m_is_div.matches(span));
48 /// ~~~~~~~~~~~~~
49 class FunctionMatch final : public Cloneable<FunctionMatch, Match>
50 {
51 public:
52  /// Constructs a FunctionMatch that matches HTML nodes for which a given
53  /// MatchFunction returns true.
54  ///
55  /// @param func: The MatchFunction that will be applied to an HTML
56  /// node.
57  explicit FunctionMatch(MatchFunction func);
58 
59  /// Returns true if the result of calling the given MatchFunction with node
60  /// as its first argument returns true.
61  ///
62  /// @param node: The node which is to be matched.
63  bool matches(const GumboNode * node) const override;
64 
65 private:
66  /// The MatchFunction that will be applied to an HTML node.
67  MatchFunction func_;
68 };
69 
70 
71 } // namespace hext
72 
73 
74 #endif // HEXT_FUNCTION_MATCH_H_INCLUDED
75 
Declares hext::MatchFunction.
FunctionMatch(MatchFunction func)
Constructs a FunctionMatch that matches HTML nodes for which a given MatchFunction returns true...
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 override
Returns true if the result of calling the given MatchFunction with node as its first argument returns...
Defines template hext::Cloneable.
Matches if the result of applying a given MatchFunction to an HTML node returns true.
Definition: FunctionMatch.h:49
std::function< bool(const GumboNode *)> MatchFunction
A type of std::function that receives an HTML element and returns a bool.
Definition: MatchFunction.h:30