Friday 2 June 2017

How to check if an element has duplicated attributes with cheerio js

I'm parsing HTML files with cheerio (to later test with Mocha), and the HTML elements in these files can have lots of attributes, I want to check if the attribute is repeated within the same element:

example partial file that has an element with repeated "class" attribute:

<div class="logo-center" data-something-very-long="something long" ... class="logo" data-more-stuff>

Here is the code that loads the file:

var fileContents = fs.readFileSync(file, "utf8");
var $ = cheerio.load(fileContents);

Note: it doesn't have to be a class attribute, it could be any other attribute that repeats.



via camou

No comments:

Post a Comment