Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
gonzales-pe
Advanced tools
The gonzales-pe npm package is a CSS parser that can parse CSS, SCSS, LESS, and other CSS-like syntaxes. It is useful for tasks such as linting, formatting, and transforming stylesheets.
Parsing CSS
This feature allows you to parse CSS code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple CSS string and convert it into a JSON representation of the AST.
const gonzales = require('gonzales-pe');
const ast = gonzales.parse('.class { color: red; }', { syntax: 'css' });
console.log(ast.toJson());
Parsing SCSS
This feature allows you to parse SCSS code into an AST. The code sample shows how to parse a simple SCSS string and convert it into a JSON representation of the AST.
const gonzales = require('gonzales-pe');
const ast = gonzales.parse('.class { color: red; }', { syntax: 'scss' });
console.log(ast.toJson());
Parsing LESS
This feature allows you to parse LESS code into an AST. The code sample demonstrates how to parse a simple LESS string and convert it into a JSON representation of the AST.
const gonzales = require('gonzales-pe');
const ast = gonzales.parse('.class { color: red; }', { syntax: 'less' });
console.log(ast.toJson());
Transforming AST
This feature allows you to traverse and transform the AST. The code sample shows how to change the color property from 'red' to 'blue' in a CSS AST.
const gonzales = require('gonzales-pe');
let ast = gonzales.parse('.class { color: red; }', { syntax: 'css' });
ast.traverse(function(node) {
if (node.type === 'declaration' && node.content[0].content === 'color') {
node.content[1].content = 'blue';
}
});
console.log(ast.toString());
PostCSS is a tool for transforming CSS with JavaScript plugins. It is more versatile and has a larger ecosystem of plugins compared to gonzales-pe. PostCSS can be used for linting, autoprefixing, and many other CSS transformations.
CSSTree is a tool for working with CSS, including parsing, generating, and analyzing CSS. It provides a more detailed and accurate AST compared to gonzales-pe and includes utilities for working with the AST.
Rework is a plugin framework for CSS preprocessing. It allows you to transform CSS with plugins, similar to PostCSS. Rework is simpler and less feature-rich compared to PostCSS but can be easier to use for basic transformations.
Creates a new node.
Parameters:
{{type: String, content: String|Array}} options
Returns:
{Object} node
Example:
var css = 'a {color: tomato}';
var ast = gonzales.parse(css);
var node = gonzales.createNode({ type: 'animal', content: 'panda' });
ast.content.push(node);
Parse CSS.
Parameters:
{String} css
{{syntax: String, rule: String}} options
Returns:
{Object} ast
.Example:
var css = 'a {color: tomato}';
var ast = gonzales.parse(css);
Example:
var less = 'a {$color: tomato}';
var ast = gonzales.parse(less, {syntax: 'less'});
Example:
var less = '$color: tomato';
var ast = gonzales.parse(less, {syntax: 'less', rule: 'declaration'});
Checks whether there is a child node of given type.
Parameters:
{String} type
Returns:
{Boolean}
Example:
if (ast.contains('panda'))
doSomething();
Returns the first child node of given type.
Parameters:
{String=} type
Returns:
{Node} node
Example:
var node = ast.first();
node.content = 'panda';
Example:
var node = ast.first('commentML');
node.content = 'panda';
Calls the function for every child node of given type.
Parameters:
{String=} type
{Function} function
Example:
ast.forEach('commentML', function(node) {
node.content = 'panda';
});
Checks whether the node is of given type.
Parameters:
{String} type
Returns:
{Boolean}
Example:
if (ast.is('s'))
ast.content = '';
Returns the last child node of given type.
Parameters:
{String=} type
Returns:
{Node} node
Example:
var node = ast.last()
node.content = 'panda';
Example:
var node = ast.last('commentML');
node.content = 'panda';
Converts AST to code.
Parameters:
{String} syntax
Returns:
{String} css
Example:
var css = ast.toCSS('css');
var less = ast.toCSS('less');
Calls the function for every node in a tree. Modifies the tree!
Parameters:
{Function} function
Example:
ast.map(function(node) {
if (node.type === 'commentML') node.content = 'panda';
});
To run tests:
npm test
This command will build library files from sources and run tests on all files in syntax directories.
Every test has 3 files: source stylesheet, expected AST and expected string compiled back from AST to css.
If some tests fail, you can find information in test logs:
log/test.log
contains all information from stdout;log/expected.txt
contains only expected text;log/result.txt
contains only result text.The last two are made for your convenience: you can use any diff app to see the defference between them.
If you want to test one specific string or get a general idea of how Gonzales
works, you can use test/ast.js
file.
Simply change the first two strings (css
and syntax
vars) and run:
node test/single-test.js
If you find a bug or want to add a feature, welcome to Issues.
If you are shy but have a question, feel free to drop me a line.
05.10.2015, version 3.0.0-beta
:star: Added --simple
flag for printing a simplified tree structure.
:green_apple: CLI now prints parse tree to stdout.
:japanese_ogre: Parse tree is now represented as objects, not arrays.
:japanese_ogre: Renamed gonzales.srcToAST()
to gonzales.parse()
.
See Readme.
:japanese_ogre: Renamed gonzales.astToSrc()
to parseTree.toString()
.
See Readme.
:japanese_ogre: Renamed gonzales.astToString()
to parseTree.toJson()
.
See Readme.
:star: Added information about column number to nodes.
:star: Added information about end position to nodes.
:green_apple: Made empty strings to be parsed as empty nodes.
:japanese_ogre: In Sass renamed interpolatedVariable
to interpolation
.
:japanese_ogre: Separated include
and extend
nodes.
:japanese_ogre: Replaced filter
with declaration
.
:japanese_ogre: Replaced braces
with brackets
and parentheses
.
:japanese_ogre: Replaced atrulers
with block
.
:japanese_ogre: Renamed nthSelector
to pseudoClass
.
:japanese_ogre: Renamed atrules
, atruler
and atruleb
to atrule
.
:japanese_ogre: Renamed functionBody
to arguments
.
:japanese_ogre: Renamed functionExpression
to expression
.
:japanese_ogre: Renamed attrib
to attributeSelector
.
:japanese_ogre: Renamed attrselector
to attributeMatch
.
:japanese_ogre: Renamed commentSL
to singlelineComment
.
:japanese_ogre: Renamed commentML
to multilineComment
.
:japanese_ogre: Renamed declDelim
to declarationDelimiter
.
:japanese_ogre: Renamed delim
to delimiter
.
:japanese_ogre: Renamed propertyDelim
to propertyDelimiter
.
:japanese_ogre: Renamed pseudoc
to pseudoClass
.
:japanese_ogre: Renamed pseudoe
to pseudoElement
.
:japanese_ogre: Renamed s
to space
.
:japanese_ogre: Renamed shash
to color
.
:japanese_ogre: Renamed vhash
to id
.
:japanese_ogre: Removed atrulerq
, unary
and unknown
.
:star: Added attributeFlags
.
:star: Added attributeName
.
:star: Added attributeValue
.
:star: Added conditionalStatement
.
:star: Added namePrefix
.
:star: Added namespacePrefix
.
:star: Added namespaceSeparator
.
:star: Added typeSelector
.
:japanese_ogre: Spaces that separate two nodes are now put between those
nodes in parse tree.
:star: Added support for extend
nodes in Less.
:star: Added support for equality and inequality signs in Sass and SCSS.
:star: Added support for /deep/
combinator.
:star: Added support for !optional
and !global
in Sass and SCSS.
:green_apple: Fixed parsing of interpolations in Sass and SCSS.
:green_apple: Fixed parsing of arguments in Sass, SCSS and Less.
:green_apple: Fixed parsing of declaration delimiters in Sass.
:green_apple: Fixed the issue when pseudo-classes were parsed like declarations.
:green_apple: Fixed parsing of selectors on multiple lines in Sass.
:green_apple: Fixed parsing of percent sign as operator in SCSS.
:green_apple: Fixed parsing of pseudo-elements as selectors in Sass.
:star: Added Babel to build source files.
:star: Used mocha for tests.
:star: Added helper scripts.
:star: Added Travis config.
:rocket: Improved tests structure.
:rocket: Separated log and test scripts.
:rocket: Improved error messages.
:rocket: Removed benchmark tests.
:rocket: Moved source files from lib
to src
directory.
:rocket: Made package availbale for install from GitHub.
FAQs
Gonzales Preprocessor Edition (fast CSS parser)
The npm package gonzales-pe receives a total of 1,779,719 weekly downloads. As such, gonzales-pe popularity was classified as popular.
We found that gonzales-pe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.