Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/manantank/validate-html-nesting

Validate parent-child nesting for HTML elements
https://github.com/manantank/validate-html-nesting

Last synced: 1 day ago
JSON representation

Validate parent-child nesting for HTML elements

Awesome Lists containing this project

README

        

# 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

```html


hello


```

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

```html

hello





```

And though `

hi

` 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

```bash
npm i validate-html-nesting
```


## API

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


## Usage Example

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

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

isValidHTMLNesting('p', 'hr'); // false
// because


can not be child of

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


```


## See also

- [eslint-plugin-validate-jsx-nesting](https://github.com/MananTank/eslint-plugin-validate-jsx-nesting) - ESLint plugin for JSX nesting validation
- [babel-plugin-validate-jsx-nesting](https://github.com/MananTank/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](/tests/validation.test.js) to see the full test suite


## Contributing

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