Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
The pug-walk npm package is designed for walking through the abstract syntax tree (AST) of Pug templates. It allows developers to traverse and manipulate nodes in the AST, enabling operations such as modification, filtering, and analysis of Pug templates.
Walking the AST
This example demonstrates how to traverse a Pug template's AST to find all `div` tags and add an `id` attribute to them. The `walk` function takes the AST and a callback function that is applied to every node. If the node matches the criteria (a `div` tag in this case), a new attribute is added.
const pug = require('pug');
const pugWalk = require('pug-walk');
const ast = pug.parser.parse('div(class=classes)');
pugWalk.walk(ast, (node, replace) => {
if (node.type === 'Tag' && node.name === 'div') {
node.attrs.push({
name: 'id',
val: '"dynamic-id"',
mustEscape: false
});
replace(node);
}
});
Filtering Nodes
This code snippet shows how to filter nodes of a specific type, in this case, `Conditional` nodes, from a Pug template's AST. The `walk` function is used to traverse the AST, and nodes that match the specified condition are added to an array for further processing.
const pug = require('pug');
const pugWalk = require('pug-walk');
const ast = pug.parser.parse('div
if condition
p True
else
p False');
const filteredNodes = [];
pugWalk.walk(ast, node => {
if (node.type === 'Conditional') {
filteredNodes.push(node);
}
}, {includeDependencies: true});
htmlparser2 is a fast HTML parser with a similar API to pug-walk but for HTML instead of Pug templates. It provides a robust and forgiving parser and a flexible tree traversal API, but it does not directly support Pug's syntax or AST structure.
cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It allows for manipulation and traversal of HTML documents with a familiar jQuery-like syntax. While it offers similar DOM manipulation and traversal capabilities, it operates on HTML rather than Pug's AST.
esprima is a high performance, standard-compliant ECMAScript parser that provides a similar AST traversal and manipulation API for JavaScript code. While it serves a similar purpose in traversing and manipulating ASTs, it is focused on JavaScript rather than Pug templates.
Walk and transform a pug AST
npm install pug-walk
var lex = require('pug-lexer');
var parse = require('pug-parser');
var walk = require('pug-walk');
var ast = walk(parse(lex('.my-class foo')), function before(node, replace) {
// called before walking the children of `node`
// to replace the node, call `replace(newNode)`
// return `false` to skip descending
if (node.type === 'Text') {
replace({ type: 'Text', val: 'bar', line: node.line });
}
}, function after(node, replace) {
// called after walking the children of `node`
// to replace the node, call `replace(newNode)`
}, {includeDependencies: true});
assert.deepEqual(parse(lex('.my-class bar')), ast);
MIT
FAQs
Walk and transform a pug AST
We found that pug-walk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.