summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2022-08-28 14:55:59 +0100
committerMichael Drake <mdrake.unique@gmail.com>2022-08-28 15:10:21 +0100
commit0eadfb721c47526c7af9f280f41a781e1bc3e3d0 (patch)
treee0d24cb1e982ea39e921a71444784fcb3a56a8a2 /src
parentd05d62752df773c8252dd10060188c35e8b6989a (diff)
downloadlibcss-0eadfb721c47526c7af9f280f41a781e1bc3e3d0.tar.gz
libcss-0eadfb721c47526c7af9f280f41a781e1bc3e3d0.tar.bz2
Select: MQ: Use interned strings for media features
Avoids some strcmps.
Diffstat (limited to 'src')
-rw-r--r--src/select/hash.c32
-rw-r--r--src/select/hash.h3
-rw-r--r--src/select/mq.h36
-rw-r--r--src/select/select.c28
4 files changed, 63 insertions, 36 deletions
diff --git a/src/select/hash.c b/src/select/hash.c
index 0d85d6f..12e82aa 100644
--- a/src/select/hash.c
+++ b/src/select/hash.c
@@ -370,7 +370,8 @@ css_error css__selector_hash_find(css_selector_hash *hash,
head->sel_chain_bloom,
req->node_bloom) &&
mq_rule_good_for_media(head->sel->rule,
- req->unit_ctx, req->media)) {
+ req->unit_ctx, req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -446,11 +447,12 @@ css_error css__selector_hash_find_by_class(css_selector_hash *hash,
_chain_good_for_element_name(
head->sel,
&(req->qname),
- req->uni) &&
+ req->str->universal) &&
mq_rule_good_for_media(
head->sel->rule,
req->unit_ctx,
- req->media)) {
+ req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -527,11 +529,12 @@ css_error css__selector_hash_find_by_id(css_selector_hash *hash,
_chain_good_for_element_name(
head->sel,
&req->qname,
- req->uni) &&
+ req->str->universal) &&
mq_rule_good_for_media(
head->sel->rule,
req->unit_ctx,
- req->media)) {
+ req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -581,7 +584,8 @@ css_error css__selector_hash_find_universal(css_selector_hash *hash,
head->sel_chain_bloom,
req->node_bloom) &&
mq_rule_good_for_media(head->sel->rule,
- req->unit_ctx, req->media)) {
+ req->unit_ctx, req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -924,7 +928,8 @@ css_error _iterate_elements(
head->sel_chain_bloom,
req->node_bloom) &&
mq_rule_good_for_media(head->sel->rule,
- req->unit_ctx, req->media)) {
+ req->unit_ctx, req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -981,11 +986,12 @@ css_error _iterate_classes(
_chain_good_for_element_name(
head->sel,
&(req->qname),
- req->uni) &&
+ req->str->universal) &&
mq_rule_good_for_media(
head->sel->rule,
req->unit_ctx,
- req->media)) {
+ req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -1043,11 +1049,12 @@ css_error _iterate_ids(
_chain_good_for_element_name(
head->sel,
&req->qname,
- req->uni) &&
+ req->str->universal) &&
mq_rule_good_for_media(
head->sel->rule,
req->unit_ctx,
- req->media)) {
+ req->media,
+ req->str)) {
/* Found a match */
break;
}
@@ -1090,7 +1097,8 @@ css_error _iterate_universal(
head->sel_chain_bloom,
req->node_bloom) &&
mq_rule_good_for_media(head->sel->rule,
- req->unit_ctx, req->media)) {
+ req->unit_ctx, req->media,
+ req->str)) {
/* Found a match */
break;
}
diff --git a/src/select/hash.h b/src/select/hash.h
index df4102f..5f48a38 100644
--- a/src/select/hash.h
+++ b/src/select/hash.h
@@ -15,6 +15,7 @@
#include <libcss/functypes.h>
#include "select/bloom.h"
+#include "select/strings.h"
/* Ugh. We need this to avoid circular includes. Happy! */
struct css_selector;
@@ -25,7 +26,7 @@ struct css_hash_selection_requirments {
css_qname qname; /* Element name, or universal "*" */
lwc_string *class; /* Name of class, or NULL */
lwc_string *id; /* Name of id, or NULL */
- lwc_string *uni; /* Universal element string "*" */
+ const css_select_strings *str; /* Selection strings */
const css_media *media; /* Media spec we're selecting for */
const css_unit_ctx *unit_ctx; /* Document unit conversion context. */
const css_bloom *node_bloom; /* Node's bloom filter */
diff --git a/src/select/mq.h b/src/select/mq.h
index 014814b..dd3252e 100644
--- a/src/select/mq.h
+++ b/src/select/mq.h
@@ -10,6 +10,7 @@
#define css_select_mq_h_
#include "select/helpers.h"
+#include "select/strings.h"
#include "select/unit.h"
static inline bool mq_match_feature_range_length_op1(
@@ -114,10 +115,15 @@ static inline bool mq_match_feature_eq_ident_op1(
static inline bool mq_match_feature(
const css_mq_feature *feat,
const css_unit_ctx *unit_ctx,
- const css_media *media)
+ const css_media *media,
+ const css_select_strings *str)
{
+ bool match;
+
/* TODO: Use interned string for comparison. */
- if (strcmp(lwc_string_data(feat->name), "width") == 0) {
+ if (lwc_string_isequal(feat->name,
+ str->width, &match) == lwc_error_ok &&
+ match == true) {
if (!mq_match_feature_range_length_op1(feat->op, &feat->value,
media->width, unit_ctx)) {
return false;
@@ -125,7 +131,9 @@ static inline bool mq_match_feature(
return mq_match_feature_range_length_op2(feat->op2,
&feat->value2, media->width, unit_ctx);
- } else if (strcmp(lwc_string_data(feat->name), "height") == 0) {
+ } else if (lwc_string_isequal(feat->name,
+ str->height, &match) == lwc_error_ok &&
+ match == true) {
if (!mq_match_feature_range_length_op1(feat->op, &feat->value,
media->height, unit_ctx)) {
return false;
@@ -134,7 +142,9 @@ static inline bool mq_match_feature(
return mq_match_feature_range_length_op2(feat->op2,
&feat->value2, media->height, unit_ctx);
- } else if (strcmp(lwc_string_data(feat->name), "prefers-color-scheme") == 0) {
+ } else if (lwc_string_isequal(feat->name,
+ str->prefers_color_scheme, &match) == lwc_error_ok &&
+ match == true) {
if (!mq_match_feature_eq_ident_op1(feat->op, &feat->value,
media->prefers_color_scheme)) {
return false;
@@ -159,7 +169,8 @@ static inline bool mq_match_feature(
static inline bool mq_match_condition(
const css_mq_cond *cond,
const css_unit_ctx *unit_ctx,
- const css_media *media)
+ const css_media *media,
+ const css_select_strings *str)
{
bool matched = !cond->op;
@@ -168,12 +179,12 @@ static inline bool mq_match_condition(
if (cond->parts[i]->type == CSS_MQ_FEATURE) {
part_matched = mq_match_feature(
cond->parts[i]->data.feat,
- unit_ctx, media);
+ unit_ctx, media, str);
} else {
assert(cond->parts[i]->type == CSS_MQ_COND);
part_matched = mq_match_condition(
cond->parts[i]->data.cond,
- unit_ctx, media);
+ unit_ctx, media, str);
}
if (cond->op) {
@@ -208,14 +219,15 @@ static inline bool mq_match_condition(
static inline bool mq__list_match(
const css_mq_query *m,
const css_unit_ctx *unit_ctx,
- const css_media *media)
+ const css_media *media,
+ const css_select_strings *str)
{
for (; m != NULL; m = m->next) {
/* Check type */
if (!!(m->type & media->type) != m->negate_type) {
if (m->cond == NULL ||
mq_match_condition(m->cond,
- unit_ctx, media)) {
+ unit_ctx, media, str)) {
/* We have a match, no need to look further. */
return true;
}
@@ -236,7 +248,8 @@ static inline bool mq__list_match(
static inline bool mq_rule_good_for_media(
const css_rule *rule,
const css_unit_ctx *unit_ctx,
- const css_media *media)
+ const css_media *media,
+ const css_select_strings *str)
{
bool applies = true;
const css_rule *ancestor = rule;
@@ -245,7 +258,8 @@ static inline bool mq_rule_good_for_media(
const css_rule_media *m = (const css_rule_media *) ancestor;
if (ancestor->type == CSS_RULE_MEDIA) {
- applies = mq__list_match(m->media, unit_ctx, media);
+ applies = mq__list_match(m->media,
+ unit_ctx, media, str);
if (applies == false) {
break;
}
diff --git a/src/select/select.c b/src/select/select.c
index ae98ec2..7d8195f 100644
--- a/src/select/select.c
+++ b/src/select/select.c
@@ -124,7 +124,8 @@ static css_error cascade_style(const css_style *style, css_select_state *state);
static css_error select_font_faces_from_sheet(
const css_stylesheet *sheet,
css_origin origin,
- css_select_font_faces_state *state);
+ css_select_font_faces_state *state,
+ const css_select_strings *str);
#ifdef DEBUG_CHAIN_MATCHING
static void dump_chain(const css_selector *selector);
@@ -1244,7 +1245,7 @@ css_error css_select_style(css_select_ctx *ctx, void *node,
for (i = 0; i < ctx->n_sheets; i++) {
const css_select_sheet s = ctx->sheets[i];
- if (mq__list_match(s.media, unit_ctx, media) &&
+ if (mq__list_match(s.media, unit_ctx, media, &ctx->str) &&
s.sheet->disabled == false) {
error = select_from_sheet(ctx, s.sheet,
s.origin, &state);
@@ -1425,10 +1426,10 @@ css_error css_select_font_faces(css_select_ctx *ctx,
for (i = 0; i < ctx->n_sheets; i++) {
const css_select_sheet s = ctx->sheets[i];
- if (mq__list_match(s.media, unit_ctx, media) &&
+ if (mq__list_match(s.media, unit_ctx, media, &ctx->str) &&
s.sheet->disabled == false) {
error = select_font_faces_from_sheet(s.sheet,
- s.origin, &state);
+ s.origin, &state, &ctx->str);
if (error != CSS_OK)
goto cleanup;
}
@@ -1599,7 +1600,8 @@ css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet,
if (import->sheet != NULL &&
mq__list_match(import->media,
state->unit_ctx,
- state->media)) {
+ state->media,
+ &ctx->str)) {
/* It's applicable, so process it */
if (sp >= IMPORT_STACK_SIZE)
return CSS_NOMEM;
@@ -1640,10 +1642,11 @@ css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet,
static css_error _select_font_face_from_rule(
const css_rule_font_face *rule, css_origin origin,
- css_select_font_faces_state *state)
+ css_select_font_faces_state *state,
+ const css_select_strings *str)
{
if (mq_rule_good_for_media((const css_rule *) rule,
- state->unit_ctx, state->media)) {
+ state->unit_ctx, state->media, str)) {
bool correct_family = false;
if (lwc_string_isequal(
@@ -1687,7 +1690,8 @@ static css_error _select_font_face_from_rule(
static css_error select_font_faces_from_sheet(
const css_stylesheet *sheet,
css_origin origin,
- css_select_font_faces_state *state)
+ css_select_font_faces_state *state,
+ const css_select_strings *str)
{
const css_stylesheet *s = sheet;
const css_rule *rule = s->rule_list;
@@ -1709,7 +1713,8 @@ static css_error select_font_faces_from_sheet(
if (import->sheet != NULL &&
mq__list_match(import->media,
state->unit_ctx,
- state->media)) {
+ state->media,
+ str)) {
/* It's applicable, so process it */
if (sp >= IMPORT_STACK_SIZE)
return CSS_NOMEM;
@@ -1727,8 +1732,7 @@ static css_error select_font_faces_from_sheet(
error = _select_font_face_from_rule(
(const css_rule_font_face *) rule,
- origin,
- state);
+ origin, state, str);
if (error != CSS_OK)
return error;
@@ -1858,7 +1862,7 @@ css_error match_selectors_in_sheet(css_select_ctx *ctx,
req.media = state->media;
req.unit_ctx = state->unit_ctx;
req.node_bloom = state->node_data->bloom;
- req.uni = ctx->str.universal;
+ req.str = &ctx->str;
/* Find hash chain that applies to current node */
req.qname = state->element;