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