SEO rules checking in natural language style.
npm install
npm run build
<!DOCTYPE html>
<head>
<meta charset='UTF-8'>
</head>
const CharSeo = require('lib/index');
const doc = CharSeo(html1)
doc.hasTag('head')
.hasChild('meta')
.hasAttribute({name: 'keywords'})
.does.not.exist(); // true
// prints: <head>, <meta name=keywords> do not exists
CharSeo API has been inspired by Chai and tries to mimic the natural language as much as possible. The grammar words can be broadly grouped in to 3 categories; subject, connective and action.
Subjects are built as a tree path, specifying the relative relationship between DOM nodes.
isStrict
: specify if attribute values are check. default to true
print
: will print check result
Sets the root DOM node to check for.
.hasTag('div')
Similar to hasTag
but appends the child to the existing path
.hasTag('div').hasChild('div') // will look for nested div tag
Similar to hasChild
.
// These 2 statements are equivalent
.hasChildren(['div', 'div'])
.hasChild('div').hasChild('div')
Specify attribute for last added tag. A subset check rather than a equivalent set check is performed
// <div style='color:black'>
.hasTag('div').hasAttribute({style: 'color:black'})
Specify attribute that should not be present for last added tag. A null intersection check rather than a equivalent set check is performed
// any <div> without style='color:black'
.hasTag('div').hasNoAttribute({style: 'color:black'})
Flips the result of checking
- appear
- does
The only purpose of connectives is to allow the constructor statement to be read like a natural language. It has not impact on the outcome of the check.
// will yield same result
.hasTag('div').exist()
.hasTag('div').to.exist()
.hasTag('div').to.appear.to.exist()
Note: Ungrammatical sentence are not forbidden
Action does all the heavy lifting and check for the subjects that have been specified.
Note action words are terminal words; no other words should come after them.
Checks if the subjects specified in the order exists
Check if the subjects specified in the order appear more than given number of times
Currently only relative ordering is supported, i.e. immediate children relationship cannot be specified.
<div>
<p></p>
<div></div>
</div>
.hasTag('div').hasChild('div').exist() // will yield true for the given HTML
However, it can be easily extended to support such a relationship by adding more words such as .hasImmediateChild
and specify the check routine.
Use Chai to assert for the outcomes. New tests should be named with *.test.js
e.g. foo.test.js
// run the tests with
npm test