diff --git a/README.md b/README.md index 5b7da22..b540400 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Tag objects can contain three keys. The `tag` key takes the name of the tag as t ### `directives` Type: `Array` Default: `[{name: '!doctype', start: '<', end: '>'}]` -Description: *Adds processing of custom directives* +Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type* ## License diff --git a/index.js b/index.js index 904ec6b..a9e71bb 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,20 @@ function postHTMLParser(html, options) { return this[this.length - 1]; }; + function isDirective(directive, tag) { + if (directive.name instanceof RegExp) { + var regex = RegExp(directive.name.source, 'i'); + + return regex.test(tag); + } + + if (tag !== directive.name) { + return false; + } + + return true; + } + function parserDirective(name, data) { var directives = [].concat(defaultDirectives, options.directives || []); var last = bufArray.last(); @@ -33,7 +47,8 @@ function postHTMLParser(html, options) { var directive = directives[i]; var directiveText = directive.start + data + directive.end; - if (name.toLowerCase() === directive.name) { + name = name.toLowerCase(); + if (isDirective(directive, name)) { if (!last) { results.push(directiveText); return; diff --git a/test/test.js b/test/test.js index b3cc39d..4b9fc52 100644 --- a/test/test.js +++ b/test/test.js @@ -124,18 +124,33 @@ describe('PostHTML-Parser test', function() { }); it('should be parse directive', function() { - var customDirectives = {directives: [ - {name: '?php', start: '<', end: '>'} - ]}; + var options = { + directives: [ + { name: '?php', start: '<', end: '>' } + ] + }; - expect(parser('', customDirectives)).to.eql(['']); + expect(parser('', options)).to.eql(['']); + }); + + it('should be parse regular expression directive', function() { + var options = { + directives: [ + { name: /\?(php|=).*/, start: '<', end: '>' } + ] + }; + + expect(parser('', options)).to.eql(['']); + expect(parser('', options)).to.eql(['']); }); it('should be parse directives and tag', function() { - var customDirectives = {directives: [ - {name: '!doctype', start: '<', end: '>'}, - {name: '?php', start: '<', end: '>'} - ]}; + var options = { + directives: [ + { name: '!doctype', start: '<', end: '>' }, + { name: '?php', start: '<', end: '>' } + ] + }; var html = ''; var tree = [ @@ -146,7 +161,7 @@ describe('PostHTML-Parser test', function() { } ]; - expect(parser(html, customDirectives)).to.eql(tree); + expect(parser(html, options)).to.eql(tree); }); it('should be parse tag', function() {