summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/parse/Makefile2
-rw-r--r--src/parse/css21.c96
-rw-r--r--src/parse/css21.h25
-rw-r--r--src/parse/parse.c10
-rw-r--r--src/parse/parse.h5
-rw-r--r--test/INDEX4
-rw-r--r--test/Makefile2
-rw-r--r--test/css21.c90
-rw-r--r--test/parse.c4
9 files changed, 224 insertions, 14 deletions
diff --git a/src/parse/Makefile b/src/parse/Makefile
index 9e1c6dd..fc27e8e 100644
--- a/src/parse/Makefile
+++ b/src/parse/Makefile
@@ -35,7 +35,7 @@ d := $(DIR)
CFLAGS := $(CFLAGS) -I$(d)
# Sources
-SRCS_$(d) := parse.c
+SRCS_$(d) := parse.c css21.c
# Append to sources for component
SOURCES += $(addprefix $(d), $(SRCS_$(d)))
diff --git a/src/parse/css21.c b/src/parse/css21.c
new file mode 100644
index 0000000..63e8df7
--- /dev/null
+++ b/src/parse/css21.c
@@ -0,0 +1,96 @@
+/*
+ * This file is part of LibCSS.
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
+ */
+
+#include "parse/css21.h"
+#include "parse/parse.h"
+
+#include "utils/utils.h"
+
+/**
+ * Context for a CSS 2.1 parser
+ */
+struct css_css21 {
+ css_stylesheet *sheet; /**< The stylesheet to parse for */
+ css_parser *parser; /**< The underlying core parser */
+
+ css_alloc alloc; /**< Memory (de)allocation function */
+ void *pw; /**< Client's private data */
+};
+
+static bool css21_handle_event(css_parser_event type,
+ const parserutils_vector *tokens, void *pw);
+
+/**
+ * Create a CSS 2.1 parser
+ *
+ * \param sheet The stylesheet object to parse for
+ * \param parser The core parser object to use
+ * \param alloc Memory (de)allocation function
+ * \param pw Pointer to client-specific private data
+ * \return Pointer to parser object, or NULL on failure
+ */
+css_css21 *css_css21_create(css_stylesheet *sheet, css_parser *parser,
+ css_alloc alloc, void *pw)
+{
+ css_css21 *css21;
+ css_parser_optparams params;
+ css_error error;
+
+ if (sheet == NULL || parser == NULL || alloc == NULL)
+ return NULL;
+
+ css21 = alloc(NULL, sizeof(css_css21), pw);
+ if (css21 == NULL)
+ return NULL;
+
+ params.event_handler.handler = css21_handle_event;
+ params.event_handler.pw = css21;
+ error = css_parser_setopt(parser, CSS_PARSER_EVENT_HANDLER, &params);
+ if (error != CSS_OK) {
+ alloc(css21, 0, pw);
+ return NULL;
+ }
+
+ css21->sheet = sheet;
+ css21->parser = parser;
+ css21->alloc = alloc;
+ css21->pw = pw;
+
+ return css21;
+}
+
+/**
+ * Destroy a CSS 2.1 parser
+ *
+ * \param css21 The parser to destroy
+ */
+void css_css21_destroy(css_css21 *css21)
+{
+ if (css21 == NULL)
+ return;
+
+ css21->alloc(css21, 0, css21->pw);
+}
+
+/**
+ * Handler for core parser events
+ *
+ * \param type The event type
+ * \param tokens Vector of tokens read since last event, or NULL
+ * \param pw Pointer to handler context
+ * \return False to signal parse error, true otherwise
+ */
+bool css21_handle_event(css_parser_event type, const parserutils_vector *tokens,
+ void *pw)
+{
+ UNUSED(type);
+ UNUSED(tokens);
+ UNUSED(pw);
+
+ return true;
+}
+
diff --git a/src/parse/css21.h b/src/parse/css21.h
new file mode 100644
index 0000000..c1b1fda
--- /dev/null
+++ b/src/parse/css21.h
@@ -0,0 +1,25 @@
+/*
+ * This file is part of LibCSS.
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
+ */
+
+#ifndef css_parse_css21_h_
+#define css_parse_css21_h_
+
+#include <parserutils/utils/vector.h>
+
+#include <libcss/functypes.h>
+#include <libcss/types.h>
+
+#include "parse/parse.h"
+
+typedef struct css_css21 css_css21;
+
+css_css21 *css_css21_create(css_stylesheet *sheet, css_parser *parser,
+ css_alloc alloc, void *pw);
+void css_css21_destroy(css_css21 *css21);
+
+#endif
+
diff --git a/src/parse/parse.c b/src/parse/parse.c
index dedccae..e19bd15 100644
--- a/src/parse/parse.c
+++ b/src/parse/parse.c
@@ -78,8 +78,6 @@ typedef struct parser_state
*/
struct css_parser
{
- css_stylesheet *sheet; /**< The sheet we're parsing */
-
parserutils_inputstream *stream; /**< The inputstream */
css_lexer *lexer; /**< The lexer to use */
@@ -171,21 +169,20 @@ static css_error (*parseFuncs[])(css_parser *parser) = {
/**
* Create a CSS parser
*
- * \param sheet The sheet to parse data for
* \param charset Charset of data, if known, or NULL
* \param cs_source Source of charset information, or CSS_CHARSET_DEFAULT
* \param alloc Memory (de)allocation function
* \param pw Pointer to client-specific private data
* \return Pointer to parser instance, or NULL on memory exhaustion
*/
-css_parser *css_parser_create(css_stylesheet *sheet, const char *charset,
- css_charset_source cs_source, css_alloc alloc, void *pw)
+css_parser *css_parser_create(const char *charset, css_charset_source cs_source,
+ css_alloc alloc, void *pw)
{
css_parser *parser;
parser_state initial = { sStart, 0 };
parserutils_error perror;
- if (sheet == NULL || alloc == NULL)
+ if (alloc == NULL)
return NULL;
parser = alloc(NULL, sizeof(css_parser), pw);
@@ -259,7 +256,6 @@ css_parser *css_parser_create(css_stylesheet *sheet, const char *charset,
return NULL;
}
- parser->sheet = sheet;
parser->quirks = false;
parser->pushback = NULL;
parser->parseError = false;
diff --git a/src/parse/parse.h b/src/parse/parse.h
index c1b6830..4137c65 100644
--- a/src/parse/parse.h
+++ b/src/parse/parse.h
@@ -10,6 +10,7 @@
#include <parserutils/utils/vector.h>
+#include <libcss/errors.h>
#include <libcss/functypes.h>
#include <libcss/types.h>
@@ -55,8 +56,8 @@ typedef union css_parser_optparams {
} event_handler;
} css_parser_optparams;
-css_parser *css_parser_create(css_stylesheet *sheet, const char *charset,
- css_charset_source cs_source, css_alloc alloc, void *pw);
+css_parser *css_parser_create(const char *charset, css_charset_source cs_source,
+ css_alloc alloc, void *pw);
void css_parser_destroy(css_parser *parser);
css_error css_parser_setopt(css_parser *parser, css_parser_opttype type,
diff --git a/test/INDEX b/test/INDEX
index fcf08f5..91031c8 100644
--- a/test/INDEX
+++ b/test/INDEX
@@ -6,7 +6,9 @@ libcss Library initialisation/finalisation
csdetect Character set detection csdetect
lex Lexing css
lex-auto Automated lexer tests lex
-parse Parsing css
+parse Parsing (core syntax) css
+css21 Parsing (CSS2.1 specifics) css
+
# Regression tests
diff --git a/test/Makefile b/test/Makefile
index a8363f4..eecf6ad 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -35,7 +35,7 @@ d := $(DIR)
CFLAGS := $(CFLAGS) -I$(TOP)/src/ -I$(d)
# Tests
-TESTS_$(d) := csdetect lex lex-auto libcss parse
+TESTS_$(d) := csdetect css21 lex lex-auto libcss parse
TESTS_$(d) := $(TESTS_$(d))
# Items for top-level makefile to use
diff --git a/test/css21.c b/test/css21.c
new file mode 100644
index 0000000..7a8d078
--- /dev/null
+++ b/test/css21.c
@@ -0,0 +1,90 @@
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <libcss/libcss.h>
+
+#include "charset/detect.h"
+#include "utils/utils.h"
+
+#include "lex/lex.h"
+#include "parse/parse.h"
+#include "parse/css21.h"
+
+#include "testutils.h"
+
+static void *myrealloc(void *ptr, size_t len, void *pw)
+{
+ UNUSED(pw);
+
+ return realloc(ptr, len);
+}
+
+int main(int argc, char **argv)
+{
+ css_parser *parser;
+ css_css21 *css21;
+ FILE *fp;
+ size_t len, origlen;
+#define CHUNK_SIZE (4096)
+ uint8_t buf[CHUNK_SIZE];
+ css_error error;
+
+ if (argc != 3) {
+ printf("Usage: %s <aliases_file> <filename>\n", argv[0]);
+ return 1;
+ }
+
+ /* Initialise library */
+ assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK);
+
+ parser = css_parser_create("UTF-8", CSS_CHARSET_DICTATED,
+ myrealloc, NULL);
+ assert(parser != NULL);
+
+ css21 = css_css21_create((css_stylesheet *) 10, parser,
+ myrealloc, NULL);
+ assert(css21 != NULL);
+
+ fp = fopen(argv[2], "rb");
+ if (fp == NULL) {
+ printf("Failed opening %s\n", argv[2]);
+ return 1;
+ }
+
+ fseek(fp, 0, SEEK_END);
+ origlen = len = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ while (len >= CHUNK_SIZE) {
+ fread(buf, 1, CHUNK_SIZE, fp);
+
+ error = css_parser_parse_chunk(parser, buf, CHUNK_SIZE);
+ assert(error == CSS_OK || error == CSS_NEEDDATA);
+
+ len -= CHUNK_SIZE;
+ }
+
+ if (len > 0) {
+ fread(buf, 1, len, fp);
+
+ error = css_parser_parse_chunk(parser, buf, len);
+ assert(error == CSS_OK || error == CSS_NEEDDATA);
+
+ len = 0;
+ }
+
+ fclose(fp);
+
+ assert(css_parser_completed(parser) == CSS_OK);
+
+ css_css21_destroy(css21);
+
+ css_parser_destroy(parser);
+
+ assert(css_finalise(myrealloc, NULL) == CSS_OK);
+
+ printf("PASS\n");
+
+ return 0;
+}
+
diff --git a/test/parse.c b/test/parse.c
index ac89a4e..7ee705b 100644
--- a/test/parse.c
+++ b/test/parse.c
@@ -73,8 +73,8 @@ int main(int argc, char **argv)
/* Initialise library */
assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK);
- parser = css_parser_create((css_stylesheet *) 10,
- "UTF-8", CSS_CHARSET_DICTATED, myrealloc, NULL);
+ parser = css_parser_create("UTF-8", CSS_CHARSET_DICTATED,
+ myrealloc, NULL);
assert(parser != NULL);
params.event_handler.handler = event_handler;