npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unclosed-tag-finder

v0.0.18

Published

A library to find unclosed html5 tags, that are normally optional.

Downloads

127

Readme

Unclosed Tag Finder

A library that finds unclosed html5 tags that are normally optional (via W3C check). These optional tags could be:

  • html
  • head
  • body
  • p
  • dt
  • dd
  • li
  • option
  • thead
  • th
  • tbody
  • tr
  • td
  • tfoot
  • colgroup

Installation

user$ npm install unclosed-tag-finder -g

Usage (the command line version)

Create a valid html5 file (but with some unclosed tags)

user$ vi valid-utf8-unclosed.html
<!DOCTYPE html>
<html>
    <head>
        <title>Das sind Umlaute: öäü</title>
    </head>
    <body>
        <p  
            class="test"
        >Das ist ein Test.<br />                                                                                                                                                                          
        <p>äöü
        <ul>
            <li>123
        </ul>
    </body>
</html>

Check the html5 file with a w3c checker of your choice

user$ npm install html-validator-cli -g
user$ html-validator --file=valid-utf8-unclosed.html
Page is valid

or

user$ xmllint --html --noout valid-utf8-unclosed.html

The page is valid.

Find unclosed tags within this valid html5 file

user$ unclosed-tag-finder valid-utf8-unclosed.html                                                                 
valid-utf8-unclosed.html:7 (missing close tag: <p/>)
opening tag: <p
            class="test"
        >

valid-utf8-unclosed.html:10 (missing close tag: <p/>)
opening tag: <p>

valid-utf8-unclosed.html:12 (missing close tag: <li/>)
opening tag: <li>

Although the html file is valid, we found some unclosed html5 tags.

Use this library inside your own scripts

#!/usr/bin/env node

/* load some libraries */
var unclosedTagFinder = require('unclosed-tag-finder');
var fs = require('fs');

var finder = new unclosedTagFinder.builder();

/* check command line arguments */
if (process.argv.length < 3) {
    console.error('No file to validate was given. Abort..');
    process.exit(1);
}

/* read given file */
fs.readFile(process.argv[2], 'utf-8', function(err, html) {                                                                                                                                               
    if (err) {
        console.log(err);
        process.exit(1);
        return;
    }   

    var unclosedTags = finder.getUnclosedTags(html, process.argv[2]);

    if (unclosedTags.length == 0) {
        return;
    }   

    /* prints out missing close tag issues */
    for (var i = 0; i < unclosedTags.length; i++) {
        if (unclosedTags[i].hasNoCloseTag) {
            console.info(unclosedTags[i].filename + ':' + unclosedTags[i].line + ' (missing close tag: <' + unclosedTags[i].name + '/>)');
            console.info(unclosedTags[i].tag);
            console.info('');
        }
    }

    /* prints out missing open tag issues */
    for (var i = 0; i < unclosedTags.length; i++) {
        if (unclosedTags[i].hasNoOpenTag) {
            console.info(unclosedTags[i].filename + ':' + unclosedTags[i].line + ' (missing open tag: <' + unclosedTags[i].name + '>)');
            console.info(unclosedTags[i].tag);
            console.info('');
        }
    }
});
user$ chmod 775 listUnclosedTags.js

Now check the file with the listUnclosedTags.js script:

user$ ./listUnclosedTags.js valid-utf8-unclosed.html
valid-utf8-unclosed.html:7 (missing close tag: <p/>)
opening tag: <p
            class="test"
        >

valid-utf8-unclosed.html:10 (missing close tag: <p/>)
opening tag: <p>

valid-utf8-unclosed.html:12 (missing close tag: <li/>)
opening tag: <li>

More informations and source code

You will find more informations and the source code at GitHub.

For an executable npm package please have a look at npmjs.com.

License

ISC © Björn Hempel