r5376 jmb - in /trunk/libcss/src: stylesheet.c stylesheet.h

netsurf at semichrome.net netsurf at semichrome.net
Sat Sep 20 12:37:46 BST 2008


Author: jmb
Date: Sat Sep 20 06:37:45 2008
New Revision: 5376

URL: http://source.netsurf-browser.org?rev=5376&view=rev
Log:
Implement a bunch of css_stylesheet_*

Modified:
    trunk/libcss/src/stylesheet.c
    trunk/libcss/src/stylesheet.h

Modified: trunk/libcss/src/stylesheet.c
URL: http://source.netsurf-browser.org/trunk/libcss/src/stylesheet.c?rev=5376&r1=5375&r2=5376&view=diff
==============================================================================
--- trunk/libcss/src/stylesheet.c (original)
+++ trunk/libcss/src/stylesheet.c Sat Sep 20 06:37:45 2008
@@ -5,6 +5,8 @@
  * Copyright 2008 John-Mark Bell <jmb at netsurf-browser.org>
  */
 
+#include <string.h>
+
 #include "stylesheet.h"
 #include "utils/utils.h"
 
@@ -20,12 +22,27 @@
 css_selector *css_stylesheet_selector_create(css_stylesheet *sheet,
 		css_selector_type type, css_string *name, css_string *value)
 {
-	UNUSED(sheet);
-	UNUSED(type);
-	UNUSED(name);
-	UNUSED(value);
-
-	return NULL;
+	css_selector *sel;
+
+	if (sheet == NULL || name == NULL)
+		return NULL;
+
+	sel = sheet->alloc(NULL, sizeof(css_selector), sheet->pw);
+	if (sel == NULL)
+		return NULL;
+
+	memset(sel, 0, sizeof(css_selector));
+
+	sel->type = type;
+	sel->data.name = *name;
+	if (value != NULL) {
+		sel->data.value = *value;
+	}
+	/** \todo specificity */
+	sel->specificity = 0;
+	sel->combinator_type = CSS_COMBINATOR_NONE;
+
+	return sel;
 }
 
 /**
@@ -56,9 +73,22 @@
 css_error css_stylesheet_selector_append_specific(css_stylesheet *sheet,
 		css_selector *parent, css_selector *specific)
 {
-	UNUSED(sheet);
-	UNUSED(parent);
-	UNUSED(specific);
+	css_selector *s;
+
+	if (sheet == NULL || parent == NULL || specific == NULL)
+		return CSS_BADPARM;
+
+	/** \todo this may want optimising */
+	for (s = parent->specifics; s != NULL && s->next != NULL; s = s->next)
+		/* do nothing */;
+	if (s == NULL) {
+		specific->prev = specific->next = NULL;
+		parent->specifics = specific;
+	} else {
+		s->next = specific;
+		specific->prev = s;
+		specific->next = NULL;
+	}
 
 	return CSS_OK;
 }
@@ -81,12 +111,15 @@
 css_error css_stylesheet_selector_combine(css_stylesheet *sheet,
 		css_combinator type, css_selector *a, css_selector *b)
 {
-	UNUSED(sheet);
-	UNUSED(type);
-	UNUSED(a);
-	UNUSED(b);
-
-	/** \todo Need to ensure that there is no existing combinator on B */
+	if (sheet == NULL || a == NULL || b == NULL)
+		return CSS_BADPARM;
+
+	/* Ensure that there is no existing combinator on B */
+	if (b->combinator != NULL)
+		return CSS_INVALID;
+
+	b->combinator = a;
+	b->combinator_type = type;
 
 	return CSS_OK;
 }
@@ -100,10 +133,20 @@
  */
 css_rule *css_stylesheet_rule_create(css_stylesheet *sheet, css_rule_type type)
 {
-	UNUSED(sheet);
-	UNUSED(type);
-
-	return NULL;
+	css_rule *rule;
+
+	if (sheet == NULL)
+		return NULL;
+
+	rule = sheet->alloc(NULL, sizeof(css_rule), sheet->pw);
+	if (rule == NULL)
+		return NULL;
+
+	memset(rule, 0, sizeof(css_rule));
+
+	rule->type = type;
+
+	return rule;
 }
 
 /**
@@ -132,12 +175,29 @@
 css_error css_stylesheet_rule_add_selector(css_stylesheet *sheet, 
 		css_rule *rule, css_selector *selector)
 {
-	UNUSED(sheet);
-	UNUSED(rule);
-	UNUSED(selector);
-
-	/** \todo Ensure rule is a CSS_RULE_SELECTOR */
-	/** \todo Set selector's rule field */
+	css_selector **sels;
+
+	if (sheet == NULL || rule == NULL || selector == NULL)
+		return CSS_BADPARM;
+
+	/* Ensure rule is a CSS_RULE_SELECTOR */
+	if (rule->type != CSS_RULE_SELECTOR)
+		return CSS_INVALID;
+
+	sels = sheet->alloc(rule->data.selector.selectors, 
+			(rule->data.selector.selector_count + 1) * 
+				sizeof(css_selector *), 
+			sheet->pw);
+	if (sels == NULL)
+		return CSS_NOMEM;
+
+	/* Insert into rule's selector list */
+	sels[rule->data.selector.selector_count] = selector;
+	rule->data.selector.selector_count++;
+	rule->data.selector.selectors = sels;
+
+	/* Set selector's rule field */
+	selector->rule = rule;
 	
 	return CSS_OK;
 }
@@ -151,10 +211,29 @@
  */
 css_error css_stylesheet_add_rule(css_stylesheet *sheet, css_rule *rule)
 {
-	UNUSED(sheet);
-	UNUSED(rule);
-
-	/** \todo Fill in rule's index and owner fields */
+	css_rule *r;
+
+	if (sheet == NULL || rule == NULL)
+		return CSS_BADPARM;
+
+	/* Fill in rule's index and owner fields */
+	rule->index = sheet->rule_count;
+	rule->owner = sheet;
+
+	/* Add rule to sheet */
+	sheet->rule_count++;
+	/** \todo this may need optimising */
+	for (r = sheet->rule_list; r != NULL && r->next != NULL; r = r->next)
+		/* do nothing */;
+	if (r == NULL) {
+		rule->prev = rule->next = NULL;
+		sheet->rule_list = rule;
+	} else {
+		r->next = rule;
+		rule->prev = r;
+		rule->next = NULL;
+	}
+
 	/** \todo If there are selectors in the rule, add them to the hash 
 	 * (this needs to recurse over child rules, too) */
 

Modified: trunk/libcss/src/stylesheet.h
URL: http://source.netsurf-browser.org/trunk/libcss/src/stylesheet.h?rev=5376&r1=5375&r2=5376&view=diff
==============================================================================
--- trunk/libcss/src/stylesheet.h (original)
+++ trunk/libcss/src/stylesheet.h Sat Sep 20 06:37:45 2008
@@ -11,6 +11,7 @@
 #include <inttypes.h>
 
 #include <libcss/errors.h>
+#include <libcss/functypes.h>
 #include <libcss/types.h>
 
 typedef struct css_rule css_rule;
@@ -131,6 +132,9 @@
 	css_stylesheet *last_child;		/**< Last in child list */
 	css_stylesheet *next;			/**< Next in sibling list */
 	css_stylesheet *prev;			/**< Previous in sibling list */
+
+	css_alloc alloc;			/**< Allocation function */
+	void *pw;				/**< Private word */
 };
 
 css_selector *css_stylesheet_selector_create(css_stylesheet *sheet,




More information about the netsurf-commits mailing list