ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Selector Struct Reference

Selectors are comprised of one or more SelectorSequences. More...

#include <Selector.h>

Inheritance diagram for Selector:

Public Member Functions

Class * _Selector (void)
 The Selector archetype.
 
Order compareTo (const Selector *self, const Selector *other)
 Compares this Selector to other, ordering by specificity.
 
void enumerateSelection (const Selector *self, View *view, ViewEnumerator enumerator, ident data)
 Selects from view and applies the given ViewEnumerator to all matched Views.
 
SelectorinitWithRule (Selector *self, const char *rule)
 Initializes this Selector with the given rule.
 
bool matchesView (const Selector *self, View *view)
 
Array * parse (const char *rules)
 Parses the null-terminated C string of Selector rules into an Array of Selectors.
 
Set * select (const Selector *self, View *view)
 
Set * selectFirst (const Selector *self, View *view)
 

Data Fields

Object object
 The superclass.
 
char * rule
 The rule, as provided by the user.
 
Array * sequences
 The sequences.
 
int specificity
 The specificity.
 
Stylestyle
 The Style.
 

Protected Attributes

SelectorInterface * interface
 The interface.
 

Detailed Description

Selectors are comprised of one or more SelectorSequences.

Remarks
This implementation of Selectors is based on the W3's specification for CSS3.
See also
https://www.w3.org/TR/2011/REC-css3-selectors-20110929/

Definition at line 49 of file Selector.h.

Member Function Documentation

◆ _Selector()

Class * _Selector ( void  )

The Selector archetype.

Returns
The Selector Class.

Definition at line 504 of file Selector.c.

504 {
505 static Class *clazz;
506 static Once once;
507
508 do_once(&once, {
509 clazz = _initialize(&(const ClassDef) {
510 .name = "Selector",
511 .superclass = _Object(),
512 .instanceSize = sizeof(Selector),
513 .interfaceOffset = offsetof(Selector, interface),
514 .interfaceSize = sizeof(SelectorInterface),
516 });
517 });
518
519 return clazz;
520}
static void initialize(Class *clazz)
Definition Box.c:123
Selectors are comprised of one or more SelectorSequences.
Definition Selector.h:49
SelectorInterface * interface
The interface.
Definition Selector.h:60

◆ compareTo()

Order compareTo ( const Selector self,
const Selector other 
)

Compares this Selector to other, ordering by specificity.

Parameters
selfThe Selector.
otherThe Selector to compare.
Returns
The comparison result.

Definition at line 151 of file Selector.c.

151 {
152
153 assert(other);
154
155 if (self->specificity < other->specificity) {
156 return OrderAscending;
157 } else if (self->specificity > other->specificity) {
158 return OrderDescending;
159 }
160
161 return OrderSame;
162}
int specificity
The specificity.
Definition Selector.h:76

◆ enumerateSelection()

void enumerateSelection ( const Selector self,
View view,
ViewEnumerator  enumerator,
ident  data 
)

Selects from view and applies the given ViewEnumerator to all matched Views.

Parameters
selfThe Selector.
viewThe View to select from.
enumeratorThe ViewEnumerator to apply to matched Views.
dataUser data.

Definition at line 168 of file Selector.c.

168 {
169
170 assert(enumerator);
171
172 Set *selection = $(self, select, view);
173 assert(selection);
174
175 Array *array = $(selection, allObjects);
176 assert(array);
177
178 for (size_t i = 0; i < array->count; i++) {
179 enumerator($(array, objectAtIndex, i), data);
180 }
181
182 release(array);
183 release(selection);
184}
Set * select(const Selector *self, View *view)

◆ initWithRule()

Selector * initWithRule ( Selector self,
const char *  rule 
)

Initializes this Selector with the given rule.

Parameters
selfThe Selector.
ruleThe rule.
Returns
The initialized Selector, or NULL on error.

Definition at line 190 of file Selector.c.

190 {
191
192 self = (Selector *) super(Object, self, init);
193 if (self) {
194
195 self->rule = strtrim(rule);
196 assert(self->rule);
197
198 self->sequences = $$(SelectorSequence, parse, self->rule);
199 assert(self->sequences->count);
200
201 self->specificity = specificity(self);
202 }
203
204 return self;
205}
static View * init(View *self)
Definition Box.c:67
char * rule
The rule, as provided by the user.
Definition Selector.h:70
Array * sequences
The sequences.
Definition Selector.h:65
Array * parse(const char *rules)
Parses the null-terminated C string of Selector rules into an Array of Selectors.
Definition Selector.c:291
SelectorSequences are comprised of one or more SimpleSelectors.

◆ matchesView()

bool matchesView ( const Selector self,
View view 
)
Parameters
selfThe Selector.
viewThe View.
Returns
True if this Selector matches the given View, false otherwise.

◆ parse()

Array * parse ( const char *  rules)

Parses the null-terminated C string of Selector rules into an Array of Selectors.

Parameters
rulesA null-terminated C string of Selector rules.
Returns
An Array of Selectors.

Definition at line 291 of file Selector.c.

291 {
292
293 Array *selectors = $$(Array, arrayWithCapacity, 4);
294 assert(selectors);
295
296 if (rules) {
297
298 const char *c = rules;
299 while (*c) {
300 const size_t size = strcspn(c, ",");
301 if (size) {
302 char *rule = calloc(1, size + 1);
303 assert(rule);
304
305 strncpy(rule, c, size);
306
307 Selector *selector = $(alloc(Selector), initWithRule, rule);
308 assert(selector);
309
310 $(selectors, addObject, selector);
311
312 release(selector);
313 free(rule);
314 }
315 c += size;
316 c += strspn(c, ", \t\n");
317 }
318 }
319
320 return (Array *) selectors;
321}
static SDL_Size size(const Image *self)
Definition Image.c:181
Selector * initWithRule(Selector *self, const char *rule)
Initializes this Selector with the given rule.
Definition Selector.c:190

◆ select()

Set * select ( const Selector self,
View view 
)
Parameters
selfThe Selector.
viewThe View.
Returns
The Set of view's descendants that match this Selector.

◆ selectFirst()

View * selectFirst ( const Selector self,
View view 
)
Parameters
selfThe Selector.
viewThe View.
Returns
The first of view's descendants that matches this Selector.

Definition at line 467 of file Selector.c.

467 {
468
469 assert(view);
470
471 Selection selection = {
472 .sequences = self->sequences,
473 };
474
475 _selectFirst(&selection, view);
476 return selection.first;
477}
static void _selectFirst(Selection *selection, View *view)
Recursively selects the first View by iterating the SelectorSequences in the given Selection.
Definition Selector.c:417
A context for View selection.
Definition Selector.c:326
const Array * sequences
Definition Selector.c:327
View * first
Definition Selector.c:330

Field Documentation

◆ interface

SelectorInterface* Selector::interface
protected

The interface.

Definition at line 60 of file Selector.h.

◆ object

Object Selector::object

The superclass.

Definition at line 54 of file Selector.h.

◆ rule

char* Selector::rule

The rule, as provided by the user.

Definition at line 70 of file Selector.h.

◆ sequences

Array* Selector::sequences

The sequences.

Definition at line 65 of file Selector.h.

◆ specificity

int Selector::specificity

The specificity.

See also
https://www.w3.org/TR/2011/REC-css3-selectors-20110929/#specificity

Definition at line 76 of file Selector.h.

◆ style

Style* Selector::style

The Style.

Remarks
This is a weak reference to the Style if this Selector belongs to a Stylesheet.

Definition at line 82 of file Selector.h.


The documentation for this struct was generated from the following files: