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.
@thenja/html-parser
Advanced tools
A simple forgiving html parser for javascript (browser and nodejs).
npm install @thenja/html-parser --save
import { HtmlParser } from "@thenja/html-parser";
let htmlParser = new HtmlParser();
<script src="dist/thenja-html-parser.min.js" type="text/javascript"></script>
var htmlParser = new Thenja.HtmlParser();
let html = "<div><p>Hello world!</p></div>";
let output = htmlParser.parse(html);
[
{
"type": "tag",
"tagType": "default",
"name": "div",
"attributes": {},
"children": [
{
"type": "tag",
"tagType": "default",
"name": "p",
"attributes": {},
"children": [
{
"type": "text",
"data": "Hello world!"
}
]
}
]
}
]
let html = "<div><p>Hello world!</p></div>";
let output = htmlParser.parse(html);
let reversedHtml = htmlParser.reverse(output);
let html = "<div><p>Hello world!</p></div>";
let output = htmlParser.parse(html, (err) => {
// handle errors here
});
// In this example we will replace .jpg extensions with .png
let html = "<div><img src='my-picture.jpg' /></div>";
let output = htmlParser.parse(html, null, (node, parentNode) => {
if(node.name === 'img' && node.attributes && node.attributes.src) {
node.attributes.src = node.attributes.src.replace('.jpg', '.png');
}
});
let newHtml = htmlParser.reverse(output);
// newHtml will equal: <div><img src='my-picture.png' /></div>
// In this example we will remove the class attribute
let html = "<div class='my-style'></div>";
let output = htmlParser.parse(html);
let newHtml = htmlParser.reverse(output, (node) => {
if(node.name === 'div') {
delete node.attributes['class'];
}
});
// newHtml will equal: <div></div>
The clean function allows you to remove unwanted html tags (such as empty tags) and empty text nodes.
Available options:
Options | Description |
---|---|
removeEmptyTags | Remove empty html tags, such as <p></p> |
removeEmptyTextNodes | Basically remove a text node if it only contains whitespace |
let html = "<div>Hi there<p></p></div>";
// by default, clean options are true, so this is only here for demo purposes
let cleanOptions = { removeEmptyTags: true, removeEmptyTextNodes: true };
let output = htmlParser.parse(html);
output = htmlParser.clean(output, cleanOptions);
let newHtml = htmlParser.reverse(output);
// newHtml will equal: <div>Hi there</div>
npm run init
- Setup the app for development (run once after cloning)
npm run dev
- Run this command when you want to work on this app. It will
compile typescript, run tests and watch for file changes.
npm run build -- -v <version>
- Create a distribution build of the app.
-v (version) - [Optional] Either "patch", "minor" or "major". Increase the version number in the package.json file.
The build command creates a /compiled directory which has all the javascript compiled code and typescript definitions. As well, a /dist directory is created that contains a minified javascript file.
Tests are automatically ran when you do a build.
npm run test
- Run the tests. The tests will be ran in a nodejs environment.
You can run the tests in a browser environment by opening the file
/spec/in-browser/SpecRunner.html.
MIT © Nathan Anderson
Add in more unit tests
Add in a flattenText() function. This will flatten many nested text nodes into one text node.
<p>My name is <strong>Nathan</strong></p>
Flattened to:
<p>My name is Nathan</p>
FAQs
A simple forgiving html parser
The npm package @thenja/html-parser receives a total of 16 weekly downloads. As such, @thenja/html-parser popularity was classified as not popular.
We found that @thenja/html-parser 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.