Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript Evaluation #22

Closed
SomMeri opened this issue Oct 3, 2012 · 10 comments
Closed

JavaScript Evaluation #22

SomMeri opened this issue Oct 3, 2012 · 10 comments

Comments

@SomMeri
Copy link
Owner

SomMeri commented Oct 3, 2012

Known syntaxes:

  color: `javascript`;
@SomMeri
Copy link
Owner Author

SomMeri commented Feb 8, 2013

Postponed since may deprecate it in the future. This will be implemented only if they will decide that they do want it.

@SomMeri
Copy link
Owner Author

SomMeri commented Feb 27, 2013

Removed javascript tests from less.js 1.3.3 test suit. It needs to be added back before closing this issue.

@SomMeri
Copy link
Owner Author

SomMeri commented Mar 25, 2013

Escaping in JavaScript is also supported:

~`"@{arguments}".replace(/[\[\]]|\,\sX/g, '')`;

Found in twitter bootstrap v2.1.1 #127

@karlvr
Copy link
Contributor

karlvr commented Aug 22, 2013

We have a need for javascript evaluation in backticks. I could look into implementing this. If you have a moment, would you be able to do a brain dump of thoughts about implementing it? Where things will need to be added etc. Does ` need to be added to the grammar?

@SomMeri
Copy link
Owner Author

SomMeri commented Aug 23, 2013

Hi, that would be really awesome.

Braindump has two parts, one is about API and another explains less4j code. This comment has only API part in it, less4j architecture will go into second comment.

API:
There has been some discussion on less.js side about whether keep javascript in less, making it possible to turn it off or at least advise against it.

So, maybe we could do it plugginable? The new Configuration object could expose some setExpressionCompiler(OtherCompiler compiler) method. JavaScript interpreter would implement OtherCompiler interface and compile JavaScript strings into strings.

If the compiler would be unavailable, less4j would issue warning/error and leave expression unchanged.

Advantages:

  • Flexibility, anybody could add compiler (python) of his choice.
  • Keeping number of dependencies low. JavaScript executor dependency would be optional in this case. Only those that really need it would have to download it.

I do not mind publishing that into maven or maintaining that other repo, if you do not want to bother with bureaucracy.

@SomMeri
Copy link
Owner Author

SomMeri commented Aug 23, 2013

Less4j uses ANTLR. If you never used it, here are tutorials:

The grammar file:

  • /less4j/src/main/antlr3/com/github/sommeri/less4j/core/parser/Less.g

The general overwiew of how less4j works is explained here. You do not have to read the whole post, only the chapter I linked.

JavaScript expressions could be parsed and evaluated as other expressions, e.g. almost like functions. Advantage: they would be automatically allowed everywhere where expressions goes (variable values, selectors, functions, properties.) Plus, they are expressions.

You have to:

  • Modify grammar in Less.g. New parser rule should probably be referenced as term alternative.
  • Create new node in AstCssNode which would correspond to JavaScript expressions. Most likely as subclass of Expression.
  • Modify ASTBuilderSwitch to transform new antlr tokens into new AstCssNode nodes and put them into tree.
  • Assuming new node is a subclass of Expression: add new public Expression evaluate(NewNode input) method into ExpressionEvaluator node. It could return an instance of CssString.

You can look at how functions are done, it should be almost the same.

Unit tests:

  • Add new javascript sub-folder into src/test/resources/compile-basic-features/.
  • Add some .less, .css, .err files into it.
  • Create test class similar to FunctionsTest. It will compile all .less files, compare results with .css and issued errors with .err.

Good luck :)

@SomMeri
Copy link
Owner Author

SomMeri commented Mar 24, 2014

This is going to be possible using custom functions https://github.com/SomMeri/less4j/wiki/Extending-Less-Language which are already implemented, but do not support ~... and ... syntaxes.

Starting with next release:

  • ~... will be treated as function named ~`
  • ... will be treated as function named `

The advantage is that anyone will be able to implement them however he likes and use any language he finds useful.

I'm not sure whether or when I will create "official" javascript implementation, but anyone will be able to do so. In any case, it will be stored in separate repository. I do not want less4j to depend on Rhino or other javascript runtime, because only some projects will use it.

@SomMeri
Copy link
Owner Author

SomMeri commented Mar 24, 2014

It might make sense to test lesshat compatibility #192 once custom javaascript function is released.

@SomMeri SomMeri removed the postponed label Mar 24, 2014
SomMeri pushed a commit that referenced this issue Mar 24, 2014
SomMeri pushed a commit that referenced this issue Mar 27, 2014
SomMeri pushed a commit that referenced this issue Mar 27, 2014
SomMeri pushed a commit that referenced this issue Mar 27, 2014
SomMeri pushed a commit that referenced this issue Mar 27, 2014
SomMeri pushed a commit that referenced this issue Mar 31, 2014
corresponds to comma or space separated lists. ComposedExpression will now
be used only for binary expressions.
@SomMeri
Copy link
Owner Author

SomMeri commented Apr 1, 2014

This will have to wait until decisions for less/less.js#1939 , less/less.js#1941 and less/less.js#1943 are taken.

I will do some javascript plugin, but it will be subject to changes later on.

@SomMeri SomMeri added this to the 1.5.2 milestone Apr 3, 2014
@SomMeri
Copy link
Owner Author

SomMeri commented Apr 3, 2014

JavaScript plugin is here: https://github.com/SomMeri/less4j-javascript

@SomMeri SomMeri closed this as completed Apr 3, 2014
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
…n which

corresponds to comma or space separated lists. ComposedExpression will now
be used only for binary expressions.
martin-g pushed a commit to martin-g/less4j that referenced this issue Apr 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants