libhext: C++ Library Documentation  1.0.9-c8ac8b6
OnlyChildMatch.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_ONLY_CHILD_MATCH_H_INCLUDED
16 #define HEXT_ONLY_CHILD_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::OnlyChildMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 #include "hext/Visibility.h"
24 
25 #include <gumbo.h>
26 
27 
28 namespace hext {
29 
30 
31 /// Matches HTML nodes that are the only child of their parent HTML element.
32 ///
33 /// The intent is to mimic the CSS pseudo-classes only-child and
34 /// only-child-of-type.
35 ///
36 /// See https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child
37 class HEXT_PUBLIC OnlyChildMatch final : public Cloneable<OnlyChildMatch, Match>
38 {
39 public:
40  /// OnlyChildMatch's options.
41  enum Option
42  {
43  /// Count siblings of any type (:only-child).
44  AnyType = 1 << 1,
45 
46  /// Only count siblings of the same type (:only-child-of-type).
47  OfType = 1 << 2
48  };
49 
50  /// Constructs an OnlyChildMatch.
51  ///
52  /// @param options: See OnlyChildMatch::Option.
53  /// Default: Count any element (:only-child).
54  explicit OnlyChildMatch(Option options = Option::AnyType) noexcept;
55 
56  /// Returns true if node is the only child of its parent.
57  bool matches(const GumboNode * node) const noexcept override;
58 
59 private:
60  /// See OnlyChildMatch::Option.
61  Option options_;
62 };
63 
64 
65 /// Applies Bitwise-OR to OnlyChildMatch::Option.
68 {
69  return static_cast<OnlyChildMatch::Option>(static_cast<int>(left) |
70  static_cast<int>(right));
71 }
72 
73 
74 /// Applies Bitwise-AND to OnlyChildMatch::Option.
77 {
78  return static_cast<OnlyChildMatch::Option>(static_cast<int>(left) &
79  static_cast<int>(right));
80 }
81 
82 
83 } // namespace hext
84 
85 
86 #endif // HEXT_ONLY_CHILD_MATCH_H_INCLUDED
87 
Defines template hext::Cloneable.
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 HTML nodes that are the only child of their parent HTML element.
OnlyChildMatch(Option options=Option::AnyType) noexcept
Constructs an OnlyChildMatch.
Option
OnlyChildMatch's options.
bool matches(const GumboNode *node) const noexcept override
Returns true if node is the only child of its parent.
HEXT_PUBLIC NthChildMatch::Option operator|(NthChildMatch::Option left, NthChildMatch::Option right) noexcept
Applies Bitwise-OR to NthChildMatch::Option.
HEXT_PUBLIC NthChildMatch::Option operator&(NthChildMatch::Option left, NthChildMatch::Option right) noexcept
Applies Bitwise-AND to NthChildMatch::Option.