All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
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 
24 #include <gumbo.h>
25 
26 
27 namespace hext {
28 
29 
30 /// Matches HTML nodes that are the only child of their parent HTML element.
31 ///
32 /// The intent is to mimic the CSS pseudo-classes only-child and
33 /// only-child-of-type.
34 ///
35 /// See https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child
36 class OnlyChildMatch final : public Cloneable<OnlyChildMatch, Match>
37 {
38 public:
39  /// OnlyChildMatch's options.
40  enum Option
41  {
42  /// Count siblings of any type (:only-child).
43  AnyType = 1 << 1,
44 
45  /// Only count siblings of the same type (:only-child-of-type).
46  OfType = 1 << 2
47  };
48 
49  /// Constructs an OnlyChildMatch.
50  ///
51  /// @param options: See OnlyChildMatch::Option.
52  /// Default: Count any element (:only-child).
53  explicit OnlyChildMatch(Option options = Option::AnyType) noexcept;
54 
55  /// Returns true if node is the only child of its parent.
56  bool matches(const GumboNode * node) const noexcept override;
57 
58 private:
59  /// See OnlyChildMatch::Option.
60  Option options_;
61 };
62 
63 
64 /// Applies Bitwise-OR to OnlyChildMatch::Option.
65 inline OnlyChildMatch::Option
66 operator|(OnlyChildMatch::Option left, OnlyChildMatch::Option right) noexcept
67 {
68  return static_cast<OnlyChildMatch::Option>(static_cast<int>(left) |
69  static_cast<int>(right));
70 }
71 
72 
73 /// Applies Bitwise-AND to OnlyChildMatch::Option.
76 {
77  return static_cast<OnlyChildMatch::Option>(static_cast<int>(left) &
78  static_cast<int>(right));
79 }
80 
81 
82 } // namespace hext
83 
84 
85 #endif // HEXT_ONLY_CHILD_MATCH_H_INCLUDED
86 
Count siblings of any type (:only-child).
OnlyChildMatch(Option options=Option::AnyType) noexcept
Constructs an OnlyChildMatch.
NthChildMatch::Option operator&(NthChildMatch::Option left, NthChildMatch::Option right) noexcept
Applies Bitwise-AND to NthChildMatch::Option.
bool matches(const GumboNode *node) const noexceptoverride
Returns true if node is the only child of its parent.
Matches HTML nodes that are the only child of their parent HTML element.
Option
OnlyChildMatch's options.
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Declares hext::Match.
Defines template hext::Cloneable.
Only count siblings of the same type (:only-child-of-type).