From df51048f730c1b8c15b9884844ea21c9a810303a Mon Sep 17 00:00:00 2001 From: iChenLei Date: Tue, 7 Dec 2021 21:07:03 +0800 Subject: [PATCH 1/3] feat: add support for case-insensitive attribute selectors --- packages/less/src/less/parser/parser.js | 10 +++++++++- packages/less/src/less/tree/attribute.js | 7 ++++++- packages/test-data/css/_main/selectors.css | 15 +++++++++++++++ packages/test-data/less/_main/selectors.less | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/less/src/less/parser/parser.js b/packages/less/src/less/parser/parser.js index 41b7d01a5..c7a65cb07 100644 --- a/packages/less/src/less/parser/parser.js +++ b/packages/less/src/less/parser/parser.js @@ -1405,6 +1405,11 @@ const Parser = function Parser(context, imports, fileInfo) { let key; let val; let op; + // + // case-insensitive flag + // e.g. [attr operator value i] + // + let cif; if (!(key = entities.variableCurly())) { key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/); @@ -1413,11 +1418,14 @@ const Parser = function Parser(context, imports, fileInfo) { op = parserInput.$re(/^[|~*$^]?=/); if (op) { val = entities.quoted() || parserInput.$re(/^[0-9]+%/) || parserInput.$re(/^[\w-]+/) || entities.variableCurly(); + if (val) { + cif = parserInput.$re(/^[iIsS]/); + } } expectChar(']'); - return new(tree.Attribute)(key, op, val); + return new(tree.Attribute)(key, op, val, cif); }, // diff --git a/packages/less/src/less/tree/attribute.js b/packages/less/src/less/tree/attribute.js index cc78b4b22..20d0cd8a5 100644 --- a/packages/less/src/less/tree/attribute.js +++ b/packages/less/src/less/tree/attribute.js @@ -1,9 +1,10 @@ import Node from './node'; -const Attribute = function(key, op, value) { +const Attribute = function(key, op, value, cif) { this.key = key; this.op = op; this.value = value; + this.cif = cif; } Attribute.prototype = Object.assign(new Node(), { @@ -26,6 +27,10 @@ Attribute.prototype = Object.assign(new Node(), { value += (this.value.toCSS ? this.value.toCSS(context) : this.value); } + if (this.cif) { + value = value + " " + this.cif; + } + return `[${value}]`; } }); diff --git a/packages/test-data/css/_main/selectors.css b/packages/test-data/css/_main/selectors.css index 10716d45c..9e0a31579 100644 --- a/packages/test-data/css/_main/selectors.css +++ b/packages/test-data/css/_main/selectors.css @@ -143,6 +143,21 @@ p a span { [p] { attributes: yes; } +/** + * https://www.w3.org/TR/selectors-4/#attribute-case + */ +a[href*="insensitive" i] { + color: cyan; +} +a[href*="cAsE" s] { + color: pink; +} +a[href*="less" I] { + background-color: silver; +} +a[href*="less" S] { + background-color: red; +} /* Large comment means chunk will be emitted after } which means chunk will begin with whitespace... blank blank blank blank blank blank blank blank blank blank blank blank blank blank blank blank diff --git a/packages/test-data/less/_main/selectors.less b/packages/test-data/less/_main/selectors.less index 005524cc5..bfdd82004 100644 --- a/packages/test-data/less/_main/selectors.less +++ b/packages/test-data/less/_main/selectors.less @@ -147,6 +147,23 @@ a { attributes: yes; } +/** + * https://www.w3.org/TR/selectors-4/#attribute-case + */ +@pattern: less; +a[href*="insensitive" i] { + color: cyan; +} +a[href*="cAsE" s] { + color: pink; +} +a[href*="@{pattern}" I] { + background-color: silver; +} +a[href*="@{pattern}" S] { + background-color: silver; +} + /* Large comment means chunk will be emitted after } which means chunk will begin with whitespace... blank blank blank blank blank blank blank blank blank blank blank blank blank blank blank blank From e3d1d7b3c5c588cc44fd0d837b57a401ef88471f Mon Sep 17 00:00:00 2001 From: iChenLei Date: Tue, 7 Dec 2021 21:56:02 +0800 Subject: [PATCH 2/3] ci: fix unit test failure --- packages/less/src/less/tree/attribute.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/less/src/less/tree/attribute.js b/packages/less/src/less/tree/attribute.js index 20d0cd8a5..0fa07eaba 100644 --- a/packages/less/src/less/tree/attribute.js +++ b/packages/less/src/less/tree/attribute.js @@ -11,8 +11,12 @@ Attribute.prototype = Object.assign(new Node(), { type: 'Attribute', eval(context) { - return new Attribute(this.key.eval ? this.key.eval(context) : this.key, - this.op, (this.value && this.value.eval) ? this.value.eval(context) : this.value); + return new Attribute( + this.key.eval ? this.key.eval(context) : this.key, + this.op, + (this.value && this.value.eval) ? this.value.eval(context) : this.value, + this.cif + ); }, genCSS(context, output) { From de4422a5a9b6725f5a6d74d7b8158b9147f3ad98 Mon Sep 17 00:00:00 2001 From: iChenLei Date: Tue, 7 Dec 2021 21:59:22 +0800 Subject: [PATCH 3/3] ci: fix snapshot mismatch --- packages/test-data/less/_main/selectors.less | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-data/less/_main/selectors.less b/packages/test-data/less/_main/selectors.less index bfdd82004..8f8bc6f1a 100644 --- a/packages/test-data/less/_main/selectors.less +++ b/packages/test-data/less/_main/selectors.less @@ -161,7 +161,7 @@ a[href*="@{pattern}" I] { background-color: silver; } a[href*="@{pattern}" S] { - background-color: silver; + background-color: red; } /*