querycomments

querySelector like but for selecting HTML Comments like any other DOM Node

APACHE-2.0 License

Downloads
84
Stars
3
Committers
1

queryComments.js

Select HTML Comments like any other DOM Node, with this fast comment selector implementation.

Installation

Yarn

yarn add querycomments

NPM

npm install querycomments

If you don't use a package manager, you can access querycomments via unpkg (CDN), download the source, or point your package manager to the url.

<!-- This is a comment -->

JSFiddle Example DEMO

API

getComments(context:DOMNode, filter:function|RegExp):Array

Examples

get all comments

getComments();

get all comments inside a element

getComments(document.getElementById('someDiv'));

get the first comment that match a regex pattern

getComments(document, /hey/i);

get the first comment is filtered by a function

getComments(document, (text, comment) => {
  this.breakOnFirst = true; //I only need the first comment
  return text.substring(0, 4) === 'Lorem'; //does it starts with "Lorem"
});

get All comments that match a regex pattern

getComments(document, /foo/g); // notice the global flag on the regex

get all comments filtered by a function

getComments(document, (text, comment) => {
  const possibleValues = ['foo', 'bar', 'baz'];

  for (var i = 0; i < possibleValues.length; i++) {
    if (text.indexOf(possibleValues[i]) > -1) return true;
  }
  return false;
});

Suggestions / Questions

File a issue on this repository