All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AttributeMatch.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_ATTRIBUTE_MATCH_H_INCLUDED
16 #define HEXT_ATTRIBUTE_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::AttributeMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 #include "hext/ValueTest.h"
24 #include "hext/Visibility.h"
25 
26 #include <memory>
27 #include <string>
28 
29 #include <gumbo.h>
30 
31 
32 namespace hext {
33 
34 
35 /// Matches HTML elements having an HTML attribute with a certain name
36 /// and, optionally, whose value is matched by a ValueTest.
37 ///
38 /// @par Example:
39 /// ~~~~~~~~~~~~~
40 /// GumboNode * node_row = ...; // <td data-row=""></td>
41 /// GumboNode * node_desolation = ...; // <td data-row="desolation"></td>
42 ///
43 /// // Match nodes containing an attribute called data-row
44 /// AttributeMatch attr_row(
45 /// "data-row" // attribute name
46 /// );
47 /// attr_row.matches(node_row);
48 /// attr_row.matches(node_desolation);
49 ///
50 /// // Match nodes containing an attribute called data-row whose value equals
51 /// // "desolation"
52 /// AttributeMatch attr_desolation(
53 /// "data-row", // attribute name
54 /// std::make_unique<EqualsTest>("desolation") // ValueTest
55 /// );
56 /// assert(attr_desolation.matches(node_desolation));
57 /// assert(!attr_desolation.matches(node_row));
58 /// ~~~~~~~~~~~~~
59 class HEXT_PUBLIC AttributeMatch final : public Cloneable<AttributeMatch, Match>
60 {
61 public:
62  /// Constructs an AttributeMatch with an optional ValueTest.
63  ///
64  /// @param attr_name: The name of the HTML attribute.
65  /// @param value_test: An optional ValueTest which, if given, must return
66  /// true for the matched HTML attribute's value.
67  explicit AttributeMatch(std::string attr_name,
68  std::unique_ptr<ValueTest> value_test = {}) noexcept;
69 
70  ~AttributeMatch() noexcept = default;
71  AttributeMatch(AttributeMatch&& other) noexcept = default;
72  AttributeMatch(const AttributeMatch& other);
73  AttributeMatch& operator=(AttributeMatch&& other) noexcept = default;
74  AttributeMatch& operator=(const AttributeMatch& other);
75 
76  /// Return true if node has an HTML attribute called attr_name (as given in
77  /// the constructor). If a ValueTest was supplied, the ValueTest must return
78  /// true for the attribute's value.
79  ///
80  /// @param node: A pointer to a GumboNode of type GUMBO_NODE_ELEMENT.
81  bool matches(const GumboNode * node) const override;
82 
83 private:
84  /// The name of the HTML attribute.
85  std::string attr_name_;
86 
87  /// An optional ValueTest that, if not null, will be applied to the HTML
88  /// attribute's value.
89  std::unique_ptr<ValueTest> test_;
90 };
91 
92 
93 } // namespace hext
94 
95 
96 #endif // HEXT_ATTRIBUTE_MATCH_H_INCLUDED
97 
Defines HEXT_PUBLIC and HEXT_PRIVATE.
Matches HTML elements having an HTML attribute with a certain name and, optionally, whose value is matched by a ValueTest.
#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:36
Declares hext::Match.
Declares hext::ValueTest.
Defines template hext::Cloneable.