Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
* testing.
  • Loading branch information
puckowski committed Nov 22, 2024
1 parent b09ad0f commit 9bf20e8
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 29 deletions.
159 changes: 149 additions & 10 deletions packages/less/dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@
_hasIndexed = true;
}
}
Visitor.prototype.visit = function (node) {
Visitor.prototype.visit = function (node, syntaxOptions) {
if (!node) {
return node;
}
Expand All @@ -1396,6 +1396,13 @@
var func = this._visitInCache[nodeTypeIndex];
var funcOut = this._visitOutCache[nodeTypeIndex];
var visitArgs = _visitArgs;
if (syntaxOptions) {
visitArgs = __assign(__assign({}, syntaxOptions), visitArgs);
}
else if (this._visitArgs) {
visitArgs = __assign(__assign({}, this._visitArgs), visitArgs);
}
this._visitArgs = visitArgs;
var fnName;
visitArgs.visitDeeper = true;
if (!func) {
Expand Down Expand Up @@ -1428,7 +1435,7 @@
}
return node;
};
Visitor.prototype.visitArray = function (nodes, nonReplacing) {
Visitor.prototype.visitArray = function (nodes, nonReplacing, syntaxOptions) {
if (!nodes) {
return nodes;
}
Expand All @@ -1437,7 +1444,7 @@
// Non-replacing
if (nonReplacing || !this._implementation.isReplacing) {
for (i = 0; i < cnt; i++) {
this.visit(nodes[i]);
this.visit(nodes[i], syntaxOptions);
}
return nodes;
}
Expand Down Expand Up @@ -2349,7 +2356,7 @@
selectors = selectors.filter(function (selector) { return selector.getIsOutput(); });
rulesetNode.selectors = selectors.length ? selectors : (selectors = null);
if (selectors) {
rulesetNode.joinSelectors(paths, context, selectors);
rulesetNode.joinSelectors(paths, context, selectors, visitArgs);
}
}
if (!selectors) {
Expand Down Expand Up @@ -3237,6 +3244,9 @@
var ContainerSyntaxOptions = {
queryInParens: true
};
var ScopeSyntaxOptions = {
queryInParens: true
};

//
// less.js - parser
Expand Down Expand Up @@ -3602,6 +3612,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 @@ -4902,7 +4918,14 @@
parserInput.restore();
e = this.value();
}
if (parserInput.$char(')')) {
if (!p && syntaxOptions.queryInParens) {
parserInput.restore();
p = this.selector();
if (p) {
nodes.push(p);
}
}
else if (parserInput.$char(')')) {
if (p && !e) {
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;
Expand Down Expand Up @@ -4975,9 +4998,12 @@
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}
if (parserInput.$str('@container')) {
else if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
}
else if (parserInput.$str('@scope')) {
return this.prepareAndGetNestableAtRule(tree.Scope, index, debugInfo, ScopeSyntaxOptions);
}
}
parserInput.restore();
},
Expand Down Expand Up @@ -6389,12 +6415,12 @@
output.add('\n');
}
},
joinSelectors: function (paths, context, selectors) {
joinSelectors: function (paths, context, selectors, visitArgs) {
for (var s = 0; s < selectors.length; s++) {
this.joinSelector(paths, context, selectors[s]);
this.joinSelector(paths, context, selectors[s], visitArgs);
}
},
joinSelector: function (paths, context, selector) {
joinSelector: function (paths, context, selector, visitArgs) {
function createParenthesis(elementsToPak, originalElement) {
var replacementParen, j;
if (elementsToPak.length === 0) {
Expand Down Expand Up @@ -6546,6 +6572,9 @@
newSelectors = replacedNewSelectors;
currentElements = [];
}
else if (el.value === '&' && el.combinator.value === '' && visitArgs && visitArgs.preserve) {
currentElements.push(new Element(el.value));
}
else {
currentElements.push(el);
}
Expand Down Expand Up @@ -8046,6 +8075,112 @@
media.evalNested(context);
} }));

var Scope = function (value, features, index, currentFileInfo, visibilityInfo) {
this._index = index;
this._fileInfo = currentFileInfo;
var selectors = (new Selector([], null, null, this._index, this._fileInfo)).createEmptySelectors();
this.features = new Value(features);
this.rules = [new Ruleset(selectors, value)];
this.rules[0].allowImports = true;
this.copyVisibilityInfo(visibilityInfo);
this.allowRoot = true;
this.setParent(selectors, this);
this.setParent(this.features, this);
this.setParent(this.rules, this);
};
Scope.prototype = Object.assign(new AtRule(), __assign(__assign({ type: 'Scope' }, NestableAtRulePrototype), { accept: function (visitor) {
if (this.features) {
this.features = visitor.visit(this.features, { preserve: true });
}
if (this.rules) {
this.rules = visitor.visitArray(this.rules, undefined, { preserve: true });
}
}, genCSS: function (context, output) {
if (this.rules && (Array.isArray(this.rules) && this.rules.length > 0) || (Array.isArray(this.rules[0]) && this.rules[0].length > 0)) {
output.add('@scope ', this._fileInfo, this._index);
this.features.genCSS(context, output);
this.outputRuleset(context, output, this.rules);
}
}, eval: function (context) {
if (!context.mediaBlocks) {
context.mediaBlocks = [];
context.mediaPath = [];
}
var media = new Scope(null, [], this._index, this._fileInfo, this.visibilityInfo());
if (this.debugInfo) {
this.rules[0].debugInfo = this.debugInfo;
media.debugInfo = this.debugInfo;
}
media.features = this.features.eval(context);
context.mediaPath.push(media);
context.mediaBlocks.push(media);
this.rules[0].functionRegistry = context.frames[0].functionRegistry.inherit();
context.frames.unshift(this.rules[0]);
media.rules = [this.rules[0].eval(context)];
context.frames.shift();
context.mediaPath.pop();
return context.mediaPath.length === 0 ? media.evalTop(context) :
media.evalNested(context);
}, getNestedElementValue: function (pathNode) {
var tmp = pathNode.value.trim();
if (tmp.startsWith('(')) {
tmp = tmp.substring(1);
}
if (tmp.endsWith(')')) {
tmp = tmp.substring(0, tmp.length - 1);
}
if (tmp.startsWith(':scope')) {
tmp = tmp.substring(6).trim();
}
return tmp;
}, evalNested: function (context) {
var i, n;
var value;
var path = context.mediaPath.concat([this]);
// Extract the media-query conditions separated with `,` (OR).
for (i = 0; i < path.length; i++) {
value = path[i].features instanceof Value ?
path[i].features.value : path[i].features;
path[i] = Array.isArray(value) ? value : [value];
}
var fromCss = '', toCss = '', tmp;
for (i = 0; i < path.length; ++i) {
var buildTo = true;
for (n = 0; n < path[i].length; ++n) {
for (var e = 0; e < path[i][n].elements.length; ++e) {
if (path[i][n].elements[e].value === 'to') {
buildTo = false;
}
else if (buildTo) {
tmp = this.getNestedElementValue(path[i][n].elements[e]);
if (fromCss.length > 0 && !tmp.startsWith('>')) {
fromCss += ' > ';
}
else {
fromCss += ' ';
}
fromCss += tmp;
}
else {
tmp = this.getNestedElementValue(path[i][n].elements[e]);
if (toCss.length > 0 && !tmp.startsWith('>')) {
toCss += ' > ';
}
else {
toCss += ' ';
}
toCss += tmp;
}
}
}
}
path = new Value(new Expression([new Selector('(' + fromCss + ')'), new Anonymous(' to '), new Selector('(' + fromCss + ' > ' + toCss + ')')]));
this.features = path;
this.setParent(this.features, this);
// Fake a tree-node that doesn't output anything.
return new Ruleset([], []);
} }));

var UnicodeDescriptor = function (value) {
this.value = value;
};
Expand Down Expand Up @@ -8650,6 +8785,7 @@
Extend: Extend,
VariableCall: VariableCall,
NamespaceValue: NamespaceValue,
Scope: Scope,
mixin: {
Call: MixinCall,
Definition: Definition
Expand Down Expand Up @@ -9710,8 +9846,11 @@
if (!(current instanceof Dimension)) {
if (Array.isArray(args[i].value)) {
Array.prototype.push.apply(args, Array.prototype.slice.call(args[i].value));
continue;
}
else {
throw { type: 'Argument', message: 'incompatible types' };
}
continue;
}
currentUnified = current.unit.toString() === '' && unitClone !== undefined ? new Dimension(current.value, unitClone).unify() : current.unify();
unit = currentUnified.unit.toString() === '' && unitStatic !== undefined ? unitStatic : currentUnified.unit.toString();
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.

24 changes: 20 additions & 4 deletions packages/less/src/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import visitors from '../visitors';
import getParserInput from './parser-input';
import * as utils from '../utils';
import functionRegistry from '../functions/function-registry';
import { ContainerSyntaxOptions, MediaSyntaxOptions } from '../tree/atrule-syntax';
import { ContainerSyntaxOptions, MediaSyntaxOptions, ScopeSyntaxOptions } from '../tree/atrule-syntax';

//
// less.js - parser
Expand Down 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]))+\]?/);
if (k) {
return tree.Color.fromKeyword(k) || new (tree.Keyword)(k);
}
},

//
// A function call
//
Expand Down Expand Up @@ -1820,7 +1827,14 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
parserInput.restore();
e = this.value();
}
if (parserInput.$char(')')) {

if (!p && syntaxOptions.queryInParens) {
parserInput.restore();
p = this.selector();
if (p) {
nodes.push(p);
}
} else if (parserInput.$char(')')) {
if (p && !e) {
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;
Expand Down Expand Up @@ -1896,10 +1910,12 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}

if (parserInput.$str('@container')) {
else if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
}
else if (parserInput.$str('@scope')) {
return this.prepareAndGetNestableAtRule(tree.Scope, index, debugInfo, ScopeSyntaxOptions);
}
}

parserInput.restore();
Expand Down
4 changes: 4 additions & 0 deletions packages/less/src/less/tree/atrule-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export const MediaSyntaxOptions = {
export const ContainerSyntaxOptions = {
queryInParens: true
};

export const ScopeSyntaxOptions = {
queryInParens: true
};
3 changes: 2 additions & 1 deletion packages/less/src/less/tree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import QueryInParens from './query-in-parens';
import Paren from './paren';
import Media from './media';
import Container from './container';
import Scope from './scope';
import UnicodeDescriptor from './unicode-descriptor';
import Negative from './negative';
import Extend from './extend';
Expand All @@ -47,7 +48,7 @@ export default {
Comment, Anonymous, Value, JavaScript, Assignment,
Condition, Paren, Media, Container, QueryInParens,
UnicodeDescriptor, Negative, Extend, VariableCall,
NamespaceValue,
NamespaceValue, Scope,
mixin: {
Call: MixinCall,
Definition: MixinDefinition
Expand Down
Loading

0 comments on commit 9bf20e8

Please sign in to comment.