
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
@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
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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.