summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Drake <michael.drake@codethink.co.uk>2019-03-10 14:50:02 +0000
committerMichael Drake <michael.drake@codethink.co.uk>2019-03-10 14:54:57 +0000
commite42684ed2ee84301a0a458fe846e8e107d19bf96 (patch)
tree3077fedf0273e80954783ac50b46e232276c5f80 /src
parent0da84ebf34d68cc1f89d057086fdd0f24747b258 (diff)
downloadlibcss-e42684ed2ee84301a0a458fe846e8e107d19bf96.tar.gz
libcss-e42684ed2ee84301a0a458fe846e8e107d19bf96.tar.bz2
Media queries: Move css_parse_media_query to parse/mq.c file.
It's now exposed in the mq.h header too.
Diffstat (limited to 'src')
-rw-r--r--src/parse/mq.c103
-rw-r--r--src/parse/mq.h4
-rw-r--r--src/stylesheet.c106
3 files changed, 107 insertions, 106 deletions
diff --git a/src/parse/mq.c b/src/parse/mq.c
index 214b5a2..e7227f7 100644
--- a/src/parse/mq.c
+++ b/src/parse/mq.c
@@ -1040,3 +1040,106 @@ css_error css__mq_parse_media_list(lwc_string **strings,
return CSS_OK;
}
+typedef struct css_mq_parse_ctx {
+ lwc_string **strings;
+ css_mq_query *media;
+} css_mq_parse_ctx;
+
+static css_error css_parse_media_query_handle_event(
+ css_parser_event type,
+ const parserutils_vector *tokens,
+ void *pw)
+{
+ int idx = 0;
+ css_error err;
+ css_mq_query *media;
+ const css_token *tok;
+ css_mq_parse_ctx *ctx = pw;
+ lwc_string **strings = ctx->strings;
+
+ UNUSED(type);
+
+ /* Skip @media */
+ tok = parserutils_vector_iterate(tokens, &idx);
+ assert(tok->type == CSS_TOKEN_ATKEYWORD);
+ UNUSED(tok);
+
+ /* Skip whitespace */
+ tok = parserutils_vector_iterate(tokens, &idx);
+ assert(tok->type == CSS_TOKEN_S);
+ UNUSED(tok);
+
+ err = css__mq_parse_media_list(strings, tokens, &idx, &media);
+ if (err != CSS_OK) {
+ return CSS_OK;
+ }
+
+ ctx->media = media;
+ return CSS_OK;
+}
+
+css_error css_parse_media_query(lwc_string **strings,
+ const uint8_t *mq, size_t len,
+ css_mq_query **media_out)
+{
+ css_error err;
+ css_parser *parser;
+ css_mq_parse_ctx ctx = {
+ .strings = strings,
+ };
+ css_parser_optparams params_quirks = {
+ .quirks = false,
+ };
+ css_parser_optparams params_handler = {
+ .event_handler = {
+ .handler = css_parse_media_query_handle_event,
+ .pw = &ctx,
+ },
+ };
+
+ err = css__parser_create_for_media_query(NULL,
+ CSS_CHARSET_DEFAULT, &parser);
+ if (err != CSS_OK) {
+ return err;
+ }
+
+ err = css__parser_setopt(parser, CSS_PARSER_QUIRKS,
+ &params_quirks);
+ if (err != CSS_OK) {
+ css__parser_destroy(parser);
+ return err;
+ }
+
+ err = css__parser_setopt(parser, CSS_PARSER_EVENT_HANDLER,
+ &params_handler);
+ if (err != CSS_OK) {
+ css__parser_destroy(parser);
+ return err;
+ }
+
+ err = css__parser_parse_chunk(parser,
+ (const uint8_t *)"@media ",
+ strlen("@media "));
+ if (err != CSS_OK && err != CSS_NEEDDATA) {
+ css__parser_destroy(parser);
+ return err;
+ }
+
+ err = css__parser_parse_chunk(parser, mq, len);
+ if (err != CSS_OK && err != CSS_NEEDDATA) {
+ css__parser_destroy(parser);
+ return err;
+ }
+
+ err = css__parser_completed(parser);
+ if (err != CSS_OK) {
+ css__parser_destroy(parser);
+ return err;
+ }
+
+ css__parser_destroy(parser);
+
+ *media_out = ctx.media;
+ return CSS_OK;
+}
+
diff --git a/src/parse/mq.h b/src/parse/mq.h
index 2940032..0e2f845 100644
--- a/src/parse/mq.h
+++ b/src/parse/mq.h
@@ -87,6 +87,10 @@ typedef struct css_mq_query {
css_mq_cond *cond;
} css_mq_query;
+css_error css_parse_media_query(lwc_string **strings,
+ const uint8_t *mq, size_t len,
+ css_mq_query **media_out);
+
css_error css__mq_parse_media_list(lwc_string **strings,
const parserutils_vector *vector, int *ctx,
css_mq_query **media);
diff --git a/src/stylesheet.c b/src/stylesheet.c
index 7c1fb83..26ba0f3 100644
--- a/src/stylesheet.c
+++ b/src/stylesheet.c
@@ -113,112 +113,6 @@ css_error css__stylesheet_string_get(css_stylesheet *sheet,
return CSS_OK;
}
-typedef struct css_mq_parse_ctx {
- lwc_string **strings;
- css_mq_query *media;
-} css_mq_parse_ctx;
-
-static css_error css_parse_media_query_handle_event(
- css_parser_event type,
- const parserutils_vector *tokens,
- void *pw)
-{
- int idx = 0;
- css_error err;
- css_mq_query *media;
- const css_token *tok;
- css_mq_parse_ctx *ctx = pw;
- lwc_string **strings = ctx->strings;
-
- UNUSED(type);
-
- /* Skip @media */
- tok = parserutils_vector_iterate(tokens, &idx);
- assert(tok->type == CSS_TOKEN_ATKEYWORD);
- UNUSED(tok);
-
- /* Skip whitespace */
- tok = parserutils_vector_iterate(tokens, &idx);
- assert(tok->type == CSS_TOKEN_S);
- UNUSED(tok);
-
- err = css__mq_parse_media_list(strings, tokens, &idx, &media);
- if (err != CSS_OK) {
- return CSS_OK;
- }
-
- ctx->media = media;
- return CSS_OK;
-}
-
-static css_error css_parse_media_query(
- lwc_string **strings,
- const uint8_t *mq,
- size_t len,
- css_mq_query **media_out)
-{
- css_error err;
- css_parser *parser;
- css_mq_parse_ctx ctx = {
- .strings = strings,
- };
- css_parser_optparams params_quirks = {
- .quirks = false,
- };
- css_parser_optparams params_handler = {
- .event_handler = {
- .handler = css_parse_media_query_handle_event,
- .pw = &ctx,
- },
- };
-
- err = css__parser_create_for_media_query(NULL,
- CSS_CHARSET_DEFAULT, &parser);
- if (err != CSS_OK) {
- return err;
- }
-
- err = css__parser_setopt(parser, CSS_PARSER_QUIRKS,
- &params_quirks);
- if (err != CSS_OK) {
- css__parser_destroy(parser);
- return err;
- }
-
- err = css__parser_setopt(parser, CSS_PARSER_EVENT_HANDLER,
- &params_handler);
- if (err != CSS_OK) {
- css__parser_destroy(parser);
- return err;
- }
-
- err = css__parser_parse_chunk(parser,
- (const uint8_t *)"@media ",
- strlen("@media "));
- if (err != CSS_OK && err != CSS_NEEDDATA) {
- css__parser_destroy(parser);
- return err;
- }
-
- err = css__parser_parse_chunk(parser, mq, len);
- if (err != CSS_OK && err != CSS_NEEDDATA) {
- css__parser_destroy(parser);
- return err;
- }
-
- err = css__parser_completed(parser);
- if (err != CSS_OK) {
- css__parser_destroy(parser);
- return err;
- }
-
- css__parser_destroy(parser);
-
- *media_out = ctx.media;
- return CSS_OK;
-}
-
-
/**
* Create a stylesheet
*