Socket
Socket
Sign inDemoInstall

validate-html-nesting

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate-html-nesting

Validate parent-child nesting for HTML elements


Version published
Maintainers
1
Created
Source

validate-html-nesting

A parent-child nesting validation library for HTML

This is not a full-blown HTML spec validation (intentionally). The parent-child nesting is considered valid if the Browser does not modify it, regardless of whether or not the HTML spec considers it valid or invalid. So, the library is purely for detecting the kind of element nesting which result in altered DOM.

Example

<p>
  hello
  <hr/>
</p>

The Browser modifies the above-shown structure to the below-shown structure, which is why the library considers the p > hr nesting invalid.

<p>hello</p>
<hr />
<p></p>

And though <h1> <div> hi </div> </h1> markup is technically invalid as per HTML spec, it's still considered valid because the browser does not modify it, so h1 > div nesting is considered valid by the library.


Install

npm i validate-html-nesting

API

isValidHTMLNesting(parentTag: string, childTag: string) : boolean

Usage Example

import { isValidHTMLNesting } from 'validate-html-nesting';

isValidHTMLNesting('a', 'a'); // false
// because <a> can not be child of <a>

isValidHTMLNesting('p', 'hr'); // false
// because <hr> can not be child of <p>

isValidHTMLNesting('div', 'a'); // true
// because <a> can be child of <div>

See also

  • eslint-plugin-validate-jsx-nesting - ESLint plugin for JSX nesting validation
  • babel-plugin-validate-jsx-nesting - Compile time JSX nesting validation

Who is this library for?

This library is mostly useful for UI framework authors who need a way to make sure that the DOM structure rendered by the browser matches the authored markup so that there are no unexpected behaviors


Test Suite

Refer to validation.test.js to see the full test suite


Contributing

PRs are welcome. Create an issue if you found a bug.

Keywords

FAQs

Package last updated on 23 Apr 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc