All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
FunctionValueMatch.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_VALUE_MATCH_H_INCLUDED
16 #define HEXT_FUNCTION_VALUE_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::FunctionValueMatch
20 
21 #include "hext/CaptureFunction.h"
22 #include "hext/Cloneable.h"
23 #include "hext/Match.h"
24 #include "hext/ValueTest.h"
25 
26 #include <memory>
27 
28 #include <gumbo.h>
29 
30 
31 namespace hext {
32 
33 
34 /// Matches if the result of applying a given CaptureFunction to an HTML node
35 /// passes a ValueTest.
36 ///
37 /// @par Example:
38 /// ~~~~~~~~~~~~~
39 /// GumboNode * foo = ...; // <div>This is a foo!</div>
40 /// GumboNode * bar = ...; // <div>This is a bar!</div>
41 ///
42 /// FunctionValueMatch m_foo(
43 /// InnerHtmlBuiltin, // CaptureFunction
44 /// std::unique_ptr<ContainsTest>("foo") // ValueTest
45 /// );
46 /// FunctionValueMatch m_bar(
47 /// InnerHtmlBuiltin, // CaptureFunction
48 /// std::unique_ptr<ContainsTest>("bar") // ValueTest
49 /// );
50 ///
51 /// assert(m_foo.matches(foo));
52 /// assert(m_bar.matches(bar));
53 ///
54 /// assert(!m_foo.matches(bar));
55 /// assert(!m_bar.matches(foo));
56 /// ~~~~~~~~~~~~~
57 class FunctionValueMatch final : public Cloneable<FunctionValueMatch, Match>
58 {
59 public:
60  /// Constructs a FunctionValueMatch that matches HTML nodes for which a given
61  /// CaptureFunction returns a result that passes a ValueTest.
62  ///
63  /// @param func: The CaptureFunction that will be applied to an HTML
64  /// node.
65  /// @param value_test: The ValueTest that the result of the given
66  /// CaptureFunction must pass.
68  std::unique_ptr<ValueTest> value_test);
69 
70  ~FunctionValueMatch() noexcept = default;
71  FunctionValueMatch(FunctionValueMatch&& other) = default;
75 
76  /// Returns true if the result of calling the given CaptureFunction with node
77  /// as its first argument passes the given ValueTest.
78  ///
79  /// @param node: A pointer to a GumboNode.
80  bool matches(const GumboNode * node) const override;
81 
82 private:
83  /// The CaptureFunction that will be applied to an HTML node.
84  CaptureFunction func_;
85 
86  /// The ValueTest that the result of the given CaptureFunction must pass.
87  std::unique_ptr<ValueTest> test_;
88 };
89 
90 
91 } // namespace hext
92 
93 
94 #endif // HEXT_FUNCTION_VALUE_MATCH_H_INCLUDED
95 
Matches if the result of applying a given CaptureFunction to an HTML node passes a ValueTest...
std::function< std::string(const GumboNode *)> CaptureFunction
A type of std::function that receives an HTML element and returns a string.
bool matches(const GumboNode *node) const override
Returns true if the result of calling the given CaptureFunction with node as its first argument passe...
FunctionValueMatch(CaptureFunction func, std::unique_ptr< ValueTest > value_test)
Constructs a FunctionValueMatch that matches HTML nodes for which a given CaptureFunction returns a r...
FunctionValueMatch & operator=(FunctionValueMatch &&other)=default
~FunctionValueMatch() noexcept=default
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Declares hext::Match.
Declares hext::CaptureFunction.
Declares hext::ValueTest.
Defines template hext::Cloneable.