28#include <Objectively.h>
43 release(this->attributes);
44 release(this->selectors);
56 return $((Array *) this->selectors, componentsJoinedByCharacters,
", ");
62static int hash(
const Object *self) {
68 hash = HashForObject(
hash, this->attributes);
69 hash = HashForObject(
hash, this->selectors);
77static bool isEqual(
const Object *self,
const Object *other) {
79 if (super(Object, self,
isEqual, other)) {
83 if (other && $(other, isKindOfClass,
_Style())) {
107 char *key = strtrim(attr);
110 $((Dictionary *) self->
attributes, setObjectForKeyPath, value, key);
123 $((Dictionary *) self->
attributes, addEntriesFromDictionary, attributes);
140 String *
string = $$(String, stringWithCharacters, value);
153 Number *r = $$(Number, numberWithValue, color->r);
154 Number *g = $$(Number, numberWithValue, color->g);
155 Number *b = $$(Number, numberWithValue, color->b);
156 Number *a = $$(Number, numberWithValue, color->a);
158 Array *array = $$(Array, arrayWithObjects, r, g, b, a, NULL);
175 Number *number = $$(Number, numberWithValue, value);
188 String *
string = nameof(names, value);
217 Number *x = $$(Number, numberWithValue, value->x);
218 Number *y = $$(Number, numberWithValue, value->y);
220 Array *array = $$(Array, arrayWithObjects, x, y, NULL);
235 Number *x = $$(Number, numberWithValue, value->x);
236 Number *y = $$(Number, numberWithValue, value->y);
237 Number *w = $$(Number, numberWithValue, value->w);
238 Number *h = $$(Number, numberWithValue, value->h);
240 Array *array = $$(Array, arrayWithObjects, x, y, w, h, NULL);
259 $((Array *) self->
selectors, addObject, selector);
268 Number *w = $$(Number, numberWithValue, value->w);
269 Number *h = $$(Number, numberWithValue, value->h);
271 Array *array = $$(Array, arrayWithObjects, w, h, NULL);
285 return $((Dictionary *) self->
attributes, objectForKeyPath, attr);
310 self = (
Style *) super(Object, self,
init);
316 for (
size_t i = 0; i < self->
selectors->count; i++) {
320 self->
attributes = (Dictionary *) $$(Dictionary, dictionaryWithCapacity, 4);
337 for (
size_t i = 0; i < self->
selectors->count; i++) {
342 if (strcmp(this->rule, that->
rule)) {
362 String *trimmed = $(
string, trimmedString);
365 StringReader *reader = $(alloc(StringReader),
initWithString, trimmed);
368 const Unicode *charset = L
", \n\t";
371 String *token = $(reader, readToken, charset, &stop);
374 NumberFormatter *formatter = $(alloc(NumberFormatter),
initWithFormat, NULL);
377 Number *number = $(formatter, numberFromString, token);
379 if (stop == READER_EOF) {
382 value = retain(number);
383 }
else if (strcmp(
"true", token->chars) == 0) {
384 value = retain($$(Boole, True));
385 }
else if (strcmp(
"false", token->chars) == 0) {
386 value = retain($$(Boole, False));
388 value = $((Object *) token, copy);
392 value = $$(Array, arrayWithCapacity, 4);
397 $((Array *) value, addObject, obj);
401 token = $(reader, readToken, charset, NULL);
404 value = retain(trimmed);
422static Array *
parse(
const char *css) {
424 Array *styles = $$(Array, array);
437 const Unicode *charset = style ? L
"{:;}" : L
"{}";
440 String *token = $(reader, readToken, charset, &stop);
449 attr = strtrim(token->chars);
467 $(styles, addObject, style);
468 style = release(style);
482 return (Array *) styles;
490 $((Dictionary *) self->
attributes, removeObjectForKeyPath, attr);
498 $((Dictionary *) self->
attributes, removeAllObjects);
501#pragma mark - Class lifecycle
508 ((ObjectInterface *) clazz->interface)->dealloc =
dealloc;
509 ((ObjectInterface *) clazz->interface)->description =
description;
510 ((ObjectInterface *) clazz->interface)->hash =
hash;
511 ((ObjectInterface *) clazz->interface)->isEqual =
isEqual;
513 ((StyleInterface *) clazz->interface)->addAttribute =
addAttribute;
514 ((StyleInterface *) clazz->interface)->addAttributes =
addAttributes;
524 ((StyleInterface *) clazz->interface)->addSelector =
addSelector;
526 ((StyleInterface *) clazz->interface)->attributeValue =
attributeValue;
530 ((StyleInterface *) clazz->interface)->parse =
parse;
532 ((StyleInterface *) clazz->interface)->removeAttribute =
removeAttribute;
544 clazz = _initialize(&(
const ClassDef) {
546 .superclass = _Object(),
547 .instanceSize =
sizeof(
Style),
548 .interfaceOffset = offsetof(
Style, interface),
549 .interfaceSize =
sizeof(StyleInterface),
static View * init(View *self)
static void addAttribute(Style *self, const char *attr, ident value)
static void addDoubleAttribute(Style *self, const char *attr, double value)
static bool isComputedEqual(const Style *self, const Style *other)
static void addIntegerAttribute(Style *self, const char *attr, int value)
static void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
static bool isEqual(const Object *self, const Object *other)
static Style * initWithRules(Style *self, const char *rules)
static Array * parse(const char *css)
static String * description(const Object *self)
static void addCharactersAttribute(Style *self, const char *attr, const char *value)
static void removeAttribute(Style *self, const char *attr)
static void addSizeAttribute(Style *self, const char *attr, const SDL_Size *value)
static void removeAllAttributes(Style *self)
static void addColorAttribute(Style *self, const char *attr, const SDL_Color *color)
static Style * initWithAttributes(Style *self, const Dictionary *attributes)
static ident attributeValue(const Style *self, const char *attr)
static void addRectangleAttribute(Style *self, const char *attr, const SDL_Rect *value)
static void dealloc(Object *self)
static void addSelector(Style *self, Selector *selector)
static void addFloatAttribute(Style *self, const char *attr, float value)
static void initialize(Class *clazz)
static ident parseValue(String *string)
static void addBoolAttribute(Style *self, const char *attr, bool value)
static void addPointAttribute(Style *self, const char *attr, const SDL_Point *value)
static void addAttributes(Style *self, const Dictionary *attributes)
static int hash(const Object *self)
static Stylesheet * initWithString(Stylesheet *self, const String *string)
static Stylesheet * initWithCharacters(Stylesheet *self, const char *chars)
static Warning * initWithFormat(Warning *self, WarningType type, const char *fmt,...)
Selectors are comprised of one or more SelectorSequences.
char * rule
The rule, as provided by the user.
Array * selectors
The Selectors.
Style * initWithRules(Style *self, const char *rules)
Initializes this Style with the given CSS selector rules.
bool isComputedEqual(const Style *self, const Style *other)
Performs a fast, rule-based comparison of this Style to the given Style.