Files
LCUI/lib/css/tests/test_css_value.c
T
Liu e533853908 feat: update css library
feat(css): add css value definition parser

refactor(css): change the parameter types of css_parse_* functions

refactor(css): migrate to new data types

refactor(css): migrate to new data types (2)

refactor(css): migrate to new data types (3)

test: add valdef test

feat(css): add css_valdef_parser_finish()

refactor(css): split css_library.c

refactor(css): css_data_type.c -> css_data_types.c

fix(css): the parsing error message of sign is not truncated

fix(css): correct initialization of data type library

fix(css): error message should not be empty

feat(css): add css_valdef_to_string()

feat(css): add double bar combinator

feat(css): add double ampersand combinator

feat(css): add curly braces multiplier

feat(css): add brackets combinator parser

docs: update css value matcher section

feat(css): add question mark multiplier

test(css): add test for brackets combinator

docs(css): update libcss_css_value.md

docs: update directory structure

refactor: numberic -> numeric

refactor(css): delete style property parser

refactor: migrate to css property API

docs(css): add css value converter section

feat(css): add css_computed_style_t

refactor(css): rename `css_style` associated identifiers

feat(css): add css_cascade_style()

refactor(css): change type of css_value_matcher_t::value

refactor(css): update args of css_register_property()

refactor(css): move css_computed_style_t

feat(css): update opacity property

feat(css): update z-index property

feat(css): update display property

feat(css): update pointer-events property

feat(css): update flex-wrap property

feat(css): update flex-direction property

feat(css): update flex-grow property

feat(css): update flex-shrink property

feat(css): update align-items property

feat(css): update justify-content property

feat(css): update position property

feat(css): update box-sizing property

feat(css): update align-content property

feat(css): update white-space property

feat(css): update content property

feat(css): update text-align property

feat(css): automatically register new keywords

feat(css): update font-style property

feat(css): update visibility property

feat(css): update border-*-style property

refactor: update css_unit_t definition

feat(css): update border-*-*-radius properties

refactor(css): delete CSS_PERCENTAGE_VALUE

refactor(css): css_cascade_length() -> css_cascade_length_auto()

refactor(css): update enum definitions

feat(css): update border-*-width properties

feat(css): update border-*-color properties

feat(css): add css_array_value_get_length()

feat(css): add border-width shorthand property

feat(css): add border-style shorthand property

feat(css): add border-radius shorthand property

feat(css): add border-color shorthand property

feat(css): add border shorthand property

feat(css): add margin shorthand property

feat(css): add padding shorthand property

feat(css): update size related properties

feat(css): update top, right, bottom, left properties

feat(css): update flex-basis property

feat(css): update line-height property

feat(css): update color property

refactor(css): add css_cascade_bg_border_color()

refactor(css): bits -> keyword_bits

refactor(css): flag -> keyword

refactor(css): update css_color_value_t definition

feat(css): add CSS_COLOR_TRANSPARENT

refactor(css): remove transparent keyword

feat(css): update font-size property

feat(css): update font-family property

feat(css): update background shorthand property

feat(css): update vertical-align property

feat(css): update box-shadow property

feat(css): update the computing of the box-shadow property

 feat(css): update the computing of the background property

feat(css): update the computing of the border property

refactor(css): keyword_bits -> type_bits

refactor(css): enum css_*_e -> uint8_t

feat(css): add absolute size computing

feat(css): add absolute padding computing

feat(css): add absolute margin computing

style(css): format code

feat(css): add top, right, bottom, and left absolute value computing

feat(css): add flex-basis absolute value computing

refactor(ui): replace data type of computed style

feat(ui): update widget background style computing

feat(ui): update widget box-shadow style computing

feat(ui): update widget border style computing

feat(ui): update widget box computing

feat(ui): use tabindex instead of focusable

feat(css): remove focusable property

feat(css): update font-weight property

fix(css): clear compile errors

refactor(ui): adapt to new computed style data structure

refactor(ui): delete ui_widget_box_model_t

fix: clear some compiler warnings

refactor(ui): ui_font_style.c -> ui_text_style.c

feat(ui): remove unnecessary helper functions

fix: clear some compiler warnings

fix(font): correct bitmap cache access

fix(css): correct the key assignment of keywords

fix(css): some memory access errors

fix: missing NULL at the end of font names

fix: clear gcc errors

refactor: clear gcc warnings

fix(css): array subscript is above array bounds

fix(css): incorrect property name memory allocation

fix(css): style parser memory leak

fix(css): prop->valdef memory leak

refactor(css): logger_debug -> DEBUG_MSG

refactor(ui-builder): clear warnings

fix(css): css_properties.list memory leak

fix(css): css_style_link_group_create() memory leak

fix(css): incorrect keyword storage

test(css): correct color_value comparison

test: clear compiler errors

fix(css): correct the return value of css_register_keyword()

fix(css): correct integer type value parsing

chore(css): _DEBUG -> DEBUG

feat(css): add string value parsing

fix(css): correct font-family initial value

test(css): add test_css_computed.c

fix(css): value read error in css_cascade_background()

fix(css): add some missing properties

feat(css): add flex shorthand property

build: update rundir of lcui-css-test

fix(css): font-family parsing error

feat(css): style array value to string

chore(vscode): add compileCommands

fix(css): string value parsing error

fix(css): correct content property initial value

fix(css): the parsed style data is not stored correctly

fix(css): correct cascading of some properties

fix(css): percentage value parsing inccorect

fix(css): <length> should not contain <percentage>

fix(css): css_array_value_duplicate() did not copy the last value

feat(css): add visible keyword

feat(css): cascade each property in order

refactor(css): add style_decl.c

refactor(css): use css_style_decl_t instead of css_style_t

fix(css): incorrect number of resolved values

refactor(css): add css_style.c

feat(css): parse shorthand properties in the parsing phase

refactor(css): add background property parser

refactor(css): add border property parser

refactor(css): css_select_style_decl -> css_find_style_store

refactor(css): add css_select_style_with_cache()

refactor(css): add margin property parser

refactor(css): add padding property parser

refactor(css): add flex property parser

fix(ui): incorrect widget style cascading order

refactor(css): const css_propdef_t* -> css_propdef_t*

refactor(css): update arguments of shorthand property parser

fix(css): incorrect value parsing of shorthand property

fix(css): unexpected truncation while parsing color value

fix(css): correct value matching and style computing

fix(ui): correct css style operation

fix(platform): crash when closing window

refactor(css): delete file prefix

refactor(css): delete style.c

refactor(css): clear compiler warnings

refactor: update directory structure and build configuration

feat(css): add dump functions

fix(css): correct valdef parser cursor operation

fix(css): correct the definition of margin and padding

fix(css): correct flex property parsing and definition

refactor(ui): adjust the use of calculated styles in the layout engine

chore: tests/helloword -> examples/hello

fix(ui): some bugs in block layout

fix(css): correct flex property definition

refactor(ui): simplify the naming of widget style and layout diff API

refactor(ui): update reflow trigger rules

refactor(ui): ui_widget_auto_reflow -> ui_widget_reflow

feat(ui): allow multiple observers to connect to the same widget

refactor(ui): simplify widget update process

refactor(ui): adjust row processing in block layout

refactor(ui): adjust the data structure used in the flexbox layout

fix(ui): correct flex layout algorithm

build: update config
2023-04-01 22:36:07 +08:00

435 lines
13 KiB
C

#include <stdio.h>
#include "test.h"
#include "ctest.h"
#include "../include/css.h"
static void test_css_valdef_string(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "\"abc\"", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
if (ret) {
ret = val.array_value[0].type == CSS_STRING_VALUE;
}
css_style_value_destroy(&val);
it_b("match('\"abc\"')", ret, 1);
it_b("match('auto')", css_parse_value(valdef, "auto", &val) > 0, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_color(const css_valdef_t *valdef)
{
css_color_value_t color;
css_style_value_t val = { 0 };
color = 0;
if (css_parse_value(valdef, "#fff", &val) > 0 &&
val.array_value[0].type == CSS_COLOR_VALUE) {
color = val.array_value[0].color_value;
}
css_style_value_destroy(&val);
it_u("match('#fff')", (int)color, (int)css_color(255, 255, 255, 255));
color = 0;
if (css_parse_value(valdef, "#ffffff", &val) > 0 &&
val.array_value[0].type == CSS_COLOR_VALUE) {
color = val.array_value[0].color_value;
}
css_style_value_destroy(&val);
it_u("match('#ffffff')", color, css_color(255, 255, 255, 255));
color = 0;
if (css_parse_value(valdef, "transparent", &val) > 0 &&
val.array_value[0].type == CSS_COLOR_VALUE) {
color = val.array_value[0].color_value;
}
css_style_value_destroy(&val);
it_u("match('transparent')", color, CSS_COLOR_TRANSPARENT);
color = 0;
if (css_parse_value(valdef, "rgb(100, 200, 255)", &val) > 0 &&
val.array_value[0].type == CSS_COLOR_VALUE) {
color = val.array_value[0].color_value;
}
css_style_value_destroy(&val);
it_u("match('rgb(100, 200, 255)')", color,
css_color(255, 100, 200, 255));
color = 0;
if (css_parse_value(valdef, "rgba(100, 200, 255, 0.5)", &val) > 0 &&
val.array_value[0].type == CSS_COLOR_VALUE) {
color = val.array_value[0].color_value;
}
css_style_value_destroy(&val);
it_u("match('rgba(100, 200, 255, 0.5)')", color,
css_color(127, 100, 200, 255));
}
static void test_css_valdef_none(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "none", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
if (ret) {
ret = val.array_value[0].keyword_value ==
css_get_keyword_key("none");
}
css_style_value_destroy(&val);
it_b("match('none')", ret, 1);
it_b("notMatch('auto')", css_parse_value(valdef, "auto", &val) <= 0, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_none_or_auto(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "none", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
if (ret) {
ret = val.array_value[0].keyword_value ==
css_get_keyword_key("none");
}
css_style_value_destroy(&val);
it_b("match('none')", ret, 1);
ret = css_parse_value(valdef, "auto", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
it_b("match('auto')", ret, 1);
it_b("notMatch('normal')", css_parse_value(valdef, "normal", &val) <= 0,
1);
css_style_value_destroy(&val);
}
static void test_css_valdef_border(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
it_b("match('1px solid #eee')",
css_parse_value(valdef, "1px solid #eee", &val) > 0, 1);
css_style_value_destroy(&val);
it_b("match('#eee 1px solid')",
css_parse_value(valdef, "#eee 1px solid", &val) > 0, 1);
css_style_value_destroy(&val);
it_b("match('solid #eee 1px')",
css_parse_value(valdef, "solid #eee 1px", &val) > 0, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
if (ret) {
ret = val.array_value[0].type == CSS_UNIT_VALUE &&
val.array_value[0].unit_value.value == 1.;
}
it_b("match('1px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "solid", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
if (ret) {
ret = val.array_value[0].type == CSS_KEYWORD_VALUE &&
val.array_value[0].keyword_value ==
css_get_keyword_key("solid");
}
it_b("match('solid')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "#eee", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
ret = val.array_value[0].color_value == 0xffeeeeee;
it_b("match('#eee')", ret, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_border_2(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
it_b("match('1px solid #eee')",
css_parse_value(valdef, "1px solid #eee", &val) > 0, 1);
css_style_value_destroy(&val);
it_b("match('#eee 1px solid')",
css_parse_value(valdef, "#eee 1px solid", &val) > 0, 1);
css_style_value_destroy(&val);
it_b("match('solid #eee 1px')",
css_parse_value(valdef, "solid #eee 1px", &val) > 0, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
it_b("notMatch('1px')", ret, 0);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "solid", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
it_b("notMatch('solid')", ret, 0);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "#eee", &val) > 0 &&
val.type == CSS_ARRAY_VALUE;
it_b("notMatch('#eee')", ret, 0);
css_style_value_destroy(&val);
}
static void test_css_valdef_box_shadow(const css_valdef_t *valdef)
{
css_style_value_t val = { 0 };
it_b("match('none')",
css_parse_value(valdef, "none", &val) > 0 &&
val.array_value[0].keyword_value == CSS_KEYWORD_NONE,
1);
css_style_value_destroy(&val);
it_b("match('0 0')",
css_parse_value(valdef, "0 0", &val) > 0 &&
css_style_value_get_array_length(&val) == 2,
1);
css_style_value_destroy(&val);
it_b("match('0 0 4px')",
css_parse_value(valdef, "0 0 4px", &val) > 0 &&
css_style_value_get_array_length(&val) == 3,
1);
css_style_value_destroy(&val);
it_b("match('0 0 4px #000')",
css_parse_value(valdef, "0 0 4px #000", &val) > 0 &&
css_style_value_get_array_length(&val) == 4,
1);
css_style_value_destroy(&val);
it_b(
"match('0 0 6px rgba(33,150,243,0.4)')",
css_parse_value(valdef, "0 0 6px rgba(33,150,243,0.4)", &val) > 0 &&
css_style_value_get_array_length(&val) == 4 &&
val.array_value[3].color_value ==
css_color((uint8_t)(0.4 * 0xff), 33, 150, 243),
1);
css_style_value_destroy(&val);
}
static void test_css_valdef_length_2(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "1px 2px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 2;
it_b("match('1px 2px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) != 0;
it_b("notMatch('1px')", ret, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_length_1_4(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "", &val) <= 0;
it_b("notMatch('')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('1px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 2;
it_b("match('1px 2px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 3;
it_b("match('1px 2px 3px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px 4px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("match('1px 2px 3px 4px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px 4px 5px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("Match('1px 2px 3px 4px 5px')", ret, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_length_percentage_1_4(const css_valdef_t *valdef)
{
int ret;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "", &val) <= 0;
it_b("notMatch('')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('1px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('50%')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 2;
it_b("match('1px 2px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 2;
it_b("match('50% 50%')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 3;
it_b("match('1px 2px 3px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 3;
it_b("match('50% 50% 50%')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px 4px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("match('1px 2px 3px 4px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px 4px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("match('50% 50% 50% 50%')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "1px 2px 3px 4px 5px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("Match('1px 2px 3px 4px 5px')", ret, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef_background_position(const css_valdef_t *valdef)
{
int ret;
size_t len;
css_style_value_t val = { 0 };
ret = css_parse_value(valdef, "center", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('center')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "10px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('10px')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "50%", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 1;
it_b("match('50%')", ret, 1);
css_style_value_destroy(&val);
if (css_parse_value(valdef, "top center", &val) > 0) {
len = css_style_value_get_array_length(&val);
ret = len == 2;
} else {
ret = 0;
}
it_b("match('top center')", ret, 1);
css_style_value_destroy(&val);
ret = css_parse_value(valdef, "bottom 10px right 20px", &val) > 0 &&
val.type == CSS_ARRAY_VALUE &&
css_style_value_get_array_length(&val) == 4;
it_b("match('bottom 10px right 20px')", ret, 1);
css_style_value_destroy(&val);
}
static void test_css_valdef(const char *definition,
void (*func)(const css_valdef_t *))
{
char str[256] = { 0 };
css_valdef_t *valdef;
snprintf(str, 255, "valdef('%s')", definition);
test_msg("%s\n", str);
test_begin();
valdef = css_compile_valdef(definition);
if (valdef) {
func(valdef);
} else {
it_b("should be able to parse", 0, 1);
}
test_end();
test_msg("\n");
css_valdef_destroy(valdef);
}
void test_css_value(void)
{
css_init();
/**
* TODO: 添加 <font-family> 测试
* 参考:https://developer.mozilla.org/zh-CN/docs/Web/CSS/Value_definition_syntax#%E4%BA%95%E5%8F%B7
* 添加支持用 # 号标识值能出现一次或多次,多次时以逗号分隔
*/
test_css_valdef("<string>", test_css_valdef_string);
test_css_valdef("<color>", test_css_valdef_color);
test_css_valdef("none", test_css_valdef_none);
test_css_valdef("none | auto", test_css_valdef_none_or_auto);
test_css_valdef("<line-width> || <line-style> || <color>",
test_css_valdef_border);
test_css_valdef("<line-width> && <line-style> && <color>",
test_css_valdef_border_2);
test_css_valdef("none | <shadow>", test_css_valdef_box_shadow);
test_css_valdef("<length>{2}", test_css_valdef_length_2);
test_css_valdef("<length>{1, 4}", test_css_valdef_length_1_4);
test_css_valdef("<length-percentage>{1, 4}",
test_css_valdef_length_percentage_1_4);
test_css_valdef("<bg-position>",
test_css_valdef_background_position);
css_destroy();
}