All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Public Types | Public Member Functions | List of all members
hext::NthChildMatch Class Referencefinal

Matches HTML nodes having a certain position within their parent HTML element. More...

Inheritance diagram for hext::NthChildMatch:
Inheritance graph
[legend]
Collaboration diagram for hext::NthChildMatch:
Collaboration graph
[legend]

Public Types

enum  Option { First = 1 << 1, Last = 1 << 2, OfType = 1 << 3 }
 NthChildMatch's options. More...
 

Public Member Functions

 NthChildMatch (int step, int shift, Option options=Option::First) noexcept
 Constructs an NthChildMatch with the pattern <step * n + shift>. More...
 
 NthChildMatch (std::pair< int, int > step_and_shift, Option options=Option::First) noexcept
 Construct an NthChildMatch with step and shift given as std::pair. More...
 
bool matches (const GumboNode *node) const noexceptoverride
 Returns true if HTML node matches pattern <step * n + shift>. More...
 
- Public Member Functions inherited from hext::Cloneable< NthChildMatch, Match >
virtual std::unique_ptr< Matchclone () const
 Clones objects of template type Derived and returns an owning pointer to the newly allocated Base. More...
 
- Public Member Functions inherited from hext::Match
 Match () noexcept=default
 
 Match (const Match &)=default
 
 Match (Match &&) noexcept=default
 
Matchoperator= (const Match &)=default
 
Matchoperator= (Match &&) noexcept=default
 
virtual ~Match () noexcept=default
 

Detailed Description

Matches HTML nodes having a certain position within their parent HTML element.

The intent is to mimic the following CSS pseudo-classes: nth-child, nth-of-type, nth-last-of-type, first-of-type, last-of-type, first-child, last-child, nth-last-child, nth-last-of-type

See https://developer.mozilla.org/en-US/docs/Web/CSS/%3Anth-child for a detailed explanation.

Example:
// Assume first, second, third are children of the following <ul> element:
// <ul><li>1</li><li>2</li><li>3</li></ul>
const GumboNode * first = ...;
const GumboNode * second = ...;
const GumboNode * third = ...;
NthChildMatch m_even(2, 0); // :nth-child(2n)
NthChildMatch m_odd (2, 1); // :nth-child(2n+1)
assert(!m_even.matches(first));
assert( m_even.matches(second));
assert(!m_even.matches(third));
assert( m_odd.matches(first));
assert(!m_odd.matches(second));
assert( m_odd.matches(third));
last-of-type Example:
// Assume first, second, third are children of the following <a> element:
// <a>
// <b>1</b>
// <u>2</u>
// <b>3</b>
// </a>
const GumboNode * first = ...;
const GumboNode * second = ...;
const GumboNode * third = ...;
NthChildMatch m_last_of_type(
0, // no repetition
1, // last element
NthChildMatch::Last | // count following siblings and
NthChildMatch::OfType // only count elements with
// same tag.
);
assert(!m_last_of_type.matches(first));
assert( m_last_of_type.matches(second));
assert( m_last_of_type.matches(third));

Definition at line 87 of file NthChildMatch.h.

Member Enumeration Documentation

NthChildMatch's options.

Enumerator
First 

Count preceding siblings (:nth-child).

Last 

Count following siblings (:nth-last-child).

OfType 

Only count siblings of the same type (:nth-of-type).

Definition at line 91 of file NthChildMatch.h.

Constructor & Destructor Documentation

hext::NthChildMatch::NthChildMatch ( int  step,
int  shift,
Option  options = Option::First 
)
noexcept

Constructs an NthChildMatch with the pattern <step * n + shift>.

Parameters
stepThe step of the pattern.
shiftThe shift of the pattern.
optionsSee NthChildMatch::Option. Default: Count any preceding HTML element.
hext::NthChildMatch::NthChildMatch ( std::pair< int, int >  step_and_shift,
Option  options = Option::First 
)
explicitnoexcept

Construct an NthChildMatch with step and shift given as std::pair.

Constructs an NthChildMatch with the pattern <step * n + shift>, where step and shift is given as a std::pair.

Parameters
step_and_shiftThe step and shift of the pattern.
optionsSee NthChildMatch::Option. Default: Count any preceding HTML element.

Member Function Documentation

bool hext::NthChildMatch::matches ( const GumboNode *  node) const
overridevirtualnoexcept

Returns true if HTML node matches pattern <step * n + shift>.

Parameters
nodeA pointer to a GumboNode.

Implements hext::Match.


The documentation for this class was generated from the following file: