Skip to content

Commit

Permalink
feat(media-queries-level-4) update media query
Browse files Browse the repository at this point in the history
* Add support for Media Queries Level 4.
* Add tests for Media Queries Level 4.
  • Loading branch information
puckowski committed Aug 2, 2023
1 parent 856ca74 commit ab78905
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 39 deletions.
51 changes: 40 additions & 11 deletions dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@
var functionRegistry = makeRegistry(null);

var MediaSyntaxOptions = {
queryInParens: false
queryInParens: true
};
var ContainerSyntaxOptions = {
queryInParens: true
Expand Down Expand Up @@ -3583,6 +3583,12 @@
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},
mediaKeyword: function () {
var k = parserInput.$char('%') || parserInput.$re(/^\[?(?:[\&\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+\]?/);
if (k) {
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},
//
// A function call
//
Expand Down Expand Up @@ -4830,26 +4836,32 @@
var nodes = [];
var e;
var p;
var rangeP;
parserInput.save();
do {
e = entities.keyword() || entities.variable() || entities.mixinLookup();
e = entities.mediaKeyword() || entities.variable() || entities.mixinLookup();
if (e) {
nodes.push(e);
}
else if (parserInput.$char('(')) {
p = this.property();
parserInput.save();
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[0-9a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
parserInput.restore();
p = this.condition();
parserInput.save();
rangeP = this.atomicCondition(null, p.rvalue);
if (!rangeP) {
parserInput.restore();
}
}
else {
parserInput.restore();
e = this.value();
}
if (parserInput.$char(')')) {
if (p && !e) {
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, p._index)));
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, rangeP ? rangeP.op : null, rangeP ? rangeP.rvalue : null, p._index)));
e = p;
}
else if (p && e) {
Expand Down Expand Up @@ -5294,7 +5306,7 @@
parserInput.forget();
return body;
},
atomicCondition: function () {
atomicCondition: function (needsParens, preparsedCond) {
var entities = this.entities;
var index = parserInput.i;
var a;
Expand All @@ -5304,7 +5316,12 @@
var cond = (function () {
return this.addition() || entities.keyword() || entities.quoted() || entities.mixinLookup();
}).bind(this);
a = cond();
if (preparsedCond) {
a = preparsedCond;
}
else {
a = cond();
}
if (a) {
if (parserInput.$char('>')) {
if (parserInput.$char('=')) {
Expand Down Expand Up @@ -5342,7 +5359,7 @@
error('expected expression');
}
}
else {
else if (!preparsedCond) {
c = new (tree.Condition)('=', a, new (tree.Keyword)('true'), index + currentIndex, false);
}
return c;
Expand Down Expand Up @@ -7946,27 +7963,39 @@
}
});

var QueryInParens = function (op, l, r, i) {
var QueryInParens = function (op, l, m, op2, r, i) {
this.op = op.trim();
this.lvalue = l;
this.mvalue = m;
this.op2 = op2 ? op2.trim() : null;
this.rvalue = r;
this._index = i;
};
QueryInParens.prototype = Object.assign(new Node(), {
type: 'QueryInParens',
accept: function (visitor) {
this.lvalue = visitor.visit(this.lvalue);
this.rvalue = visitor.visit(this.rvalue);
this.mvalue = visitor.visit(this.mvalue);
if (this.rvalue) {
this.rvalue = visitor.visit(this.rvalue);
}
},
eval: function (context) {
this.lvalue = this.lvalue.eval(context);
this.rvalue = this.rvalue.eval(context);
this.mvalue = this.mvalue.eval(context);
if (this.rvalue) {
this.rvalue = this.rvalue.eval(context);
}
return this;
},
genCSS: function (context, output) {
this.lvalue.genCSS(context, output);
output.add(' ' + this.op + ' ');
this.rvalue.genCSS(context, output);
this.mvalue.genCSS(context, output);
if (this.rvalue) {
output.add(' ' + this.op2 + ' ');
this.rvalue.genCSS(context, output);
}
},
});

Expand Down
2 changes: 1 addition & 1 deletion dist/less.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/less.min.js.map

Large diffs are not rendered by default.

51 changes: 40 additions & 11 deletions packages/less/dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@
var functionRegistry = makeRegistry(null);

var MediaSyntaxOptions = {
queryInParens: false
queryInParens: true
};
var ContainerSyntaxOptions = {
queryInParens: true
Expand Down Expand Up @@ -3583,6 +3583,12 @@
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},
mediaKeyword: function () {
var k = parserInput.$char('%') || parserInput.$re(/^\[?(?:[\&\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+\]?/);
if (k) {
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},
//
// A function call
//
Expand Down Expand Up @@ -4830,26 +4836,32 @@
var nodes = [];
var e;
var p;
var rangeP;
parserInput.save();
do {
e = entities.keyword() || entities.variable() || entities.mixinLookup();
e = entities.mediaKeyword() || entities.variable() || entities.mixinLookup();
if (e) {
nodes.push(e);
}
else if (parserInput.$char('(')) {
p = this.property();
parserInput.save();
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[0-9a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
parserInput.restore();
p = this.condition();
parserInput.save();
rangeP = this.atomicCondition(null, p.rvalue);
if (!rangeP) {
parserInput.restore();
}
}
else {
parserInput.restore();
e = this.value();
}
if (parserInput.$char(')')) {
if (p && !e) {
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, p._index)));
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, rangeP ? rangeP.op : null, rangeP ? rangeP.rvalue : null, p._index)));
e = p;
}
else if (p && e) {
Expand Down Expand Up @@ -5294,7 +5306,7 @@
parserInput.forget();
return body;
},
atomicCondition: function () {
atomicCondition: function (needsParens, preparsedCond) {
var entities = this.entities;
var index = parserInput.i;
var a;
Expand All @@ -5304,7 +5316,12 @@
var cond = (function () {
return this.addition() || entities.keyword() || entities.quoted() || entities.mixinLookup();
}).bind(this);
a = cond();
if (preparsedCond) {
a = preparsedCond;
}
else {
a = cond();
}
if (a) {
if (parserInput.$char('>')) {
if (parserInput.$char('=')) {
Expand Down Expand Up @@ -5342,7 +5359,7 @@
error('expected expression');
}
}
else {
else if (!preparsedCond) {
c = new (tree.Condition)('=', a, new (tree.Keyword)('true'), index + currentIndex, false);
}
return c;
Expand Down Expand Up @@ -7946,27 +7963,39 @@
}
});

var QueryInParens = function (op, l, r, i) {
var QueryInParens = function (op, l, m, op2, r, i) {
this.op = op.trim();
this.lvalue = l;
this.mvalue = m;
this.op2 = op2 ? op2.trim() : null;
this.rvalue = r;
this._index = i;
};
QueryInParens.prototype = Object.assign(new Node(), {
type: 'QueryInParens',
accept: function (visitor) {
this.lvalue = visitor.visit(this.lvalue);
this.rvalue = visitor.visit(this.rvalue);
this.mvalue = visitor.visit(this.mvalue);
if (this.rvalue) {
this.rvalue = visitor.visit(this.rvalue);
}
},
eval: function (context) {
this.lvalue = this.lvalue.eval(context);
this.rvalue = this.rvalue.eval(context);
this.mvalue = this.mvalue.eval(context);
if (this.rvalue) {
this.rvalue = this.rvalue.eval(context);
}
return this;
},
genCSS: function (context, output) {
this.lvalue.genCSS(context, output);
output.add(' ' + this.op + ' ');
this.rvalue.genCSS(context, output);
this.mvalue.genCSS(context, output);
if (this.rvalue) {
output.add(' ' + this.op2 + ' ');
this.rvalue.genCSS(context, output);
}
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/less/dist/less.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/less/dist/less.min.js.map

Large diffs are not rendered by default.

31 changes: 25 additions & 6 deletions packages/less/src/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
}
},

mediaKeyword: function () {
const k = parserInput.$char('%') || parserInput.$re(/^\[?(?:[\&\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+\]?/);

Check failure on line 407 in packages/less/src/less/parser/parser.js

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest with node18

Unnecessary escape character: \&

Check failure on line 407 in packages/less/src/less/parser/parser.js

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest with node10

Unnecessary escape character: \&

Check failure on line 407 in packages/less/src/less/parser/parser.js

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest with node12

Unnecessary escape character: \&
if (k) {
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},

//
// A function call
//
Expand Down Expand Up @@ -1758,24 +1765,31 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
const nodes = [];
let e;
let p;
let rangeP;
parserInput.save();
do {
e = entities.keyword() || entities.variable() || entities.mixinLookup();
e = entities.mediaKeyword() || entities.variable() || entities.mixinLookup();
if (e) {
nodes.push(e);
} else if (parserInput.$char('(')) {
p = this.property();
parserInput.save();
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
if (!p && syntaxOptions.queryInParens && parserInput.$re(/^[0-9a-z-]*\s*([<>]=|<=|>=|[<>]|=)/)) {
parserInput.restore();
p = this.condition();

parserInput.save();
rangeP = this.atomicCondition(null, p.rvalue);
if (!rangeP) {
parserInput.restore();
}
} else {
parserInput.restore();
e = this.value();
}
if (parserInput.$char(')')) {
if (p && !e) {
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, p._index)));
nodes.push(new (tree.Paren)(new (tree.QueryInParens)(p.op, p.lvalue, p.rvalue, rangeP ? rangeP.op : null, rangeP ? rangeP.rvalue : null, p._index)));
e = p;
} else if (p && e) {
nodes.push(new (tree.Paren)(new (tree.Declaration)(p, e, null, null, parserInput.i + currentIndex, fileInfo, true)));
Expand Down Expand Up @@ -2248,7 +2262,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
parserInput.forget();
return body;
},
atomicCondition: function () {
atomicCondition: function (needsParens, preparsedCond) {
const entities = this.entities;
const index = parserInput.i;
let a;
Expand All @@ -2260,7 +2274,12 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
return this.addition() || entities.keyword() || entities.quoted() || entities.mixinLookup();
}).bind(this)

a = cond();
if (preparsedCond) {
a = preparsedCond;
} else {
a = cond();
}

if (a) {
if (parserInput.$char('>')) {
if (parserInput.$char('=')) {
Expand Down Expand Up @@ -2292,7 +2311,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
} else {
error('expected expression');
}
} else {
} else if (!preparsedCond) {
c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index + currentIndex, false);
}
return c;
Expand Down
2 changes: 1 addition & 1 deletion packages/less/src/less/tree/atrule-syntax.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const MediaSyntaxOptions = {
queryInParens: false
queryInParens: true
};

export const ContainerSyntaxOptions = {
Expand Down
Loading

0 comments on commit ab78905

Please sign in to comment.