From 06d011418d4467dfc7df662b0b526ff4167b3c0f Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sun, 9 Jun 2019 10:20:38 +0100 Subject: Media queries: Handle errors in a media query list correctly. --- src/parse/mq.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/parse') diff --git a/src/parse/mq.c b/src/parse/mq.c index c05102c..85ddf60 100644 --- a/src/parse/mq.c +++ b/src/parse/mq.c @@ -1059,6 +1059,34 @@ finished: return CSS_OK; } +/** + * Create a `not all` media query. + * + * > 3.2: "A media query that does not match the grammar in the previous + * > section must be replaced by not all during parsing." + * + * https://www.w3.org/TR/mediaqueries-4/#error-handling + * + * \param[out] Returns the created mq on success. + * \return CSS_OK on success, + */ +static css_error css__mq_parse__create_not_all( + css_mq_query **not_all_out) +{ + css_mq_query *not_all; + + not_all = calloc(1, sizeof(*not_all)); + if (not_all == NULL) { + return CSS_NOMEM; + } + + not_all->negate_type = 1; + not_all->type = CSS_MEDIA_ALL; + + *not_all_out = not_all; + return CSS_OK; +} + css_error css__mq_parse_media_list(lwc_string **strings, const parserutils_vector *vector, int *ctx, css_mq_query **media) @@ -1081,18 +1109,21 @@ css_error css__mq_parse_media_list(lwc_string **strings, css_mq_query *query; error = mq_parse_media_query(strings, vector, ctx, &query); + if (error == CSS_INVALID) { + error = css__mq_parse__create_not_all(&query); + } + if (error != CSS_OK) { - /* TODO: error recovery (see above) */ css__mq_query_destroy(result); return error; + } + + if (result == NULL) { + result = last = query; } else { - if (result == NULL) { - result = last = query; - } else { - assert(last != NULL); - last->next = query; - last = query; - } + assert(last != NULL); + last->next = query; + last = query; } consumeWhitespace(vector, ctx); -- cgit v1.2.3