From 9403660fb3bd46de282a236f15c42de12fa550a3 Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Thu, 18 Jan 2018 19:44:23 +0700 Subject: [PATCH 1/6] directive: add support for regular expression custom directive. Example: The custom directive {name: /\?(php|=).*/, start: '<', end: '>'} will match any directive with or pattern --- README.md | 2 +- index.js | 11 ++++++++++- test/test.js | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) 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 0ae710e..4e0bc88 100644 --- a/index.js +++ b/index.js @@ -29,11 +29,20 @@ function postHTMLParser(html, options) { var directives = options.directives || defaultDirectives; var last = bufArray.last(); + var tagName; for (var i = 0; i < directives.length; i++) { var directive = directives[i]; var directiveText = directive.start + data + directive.end; + var isDirective = false; - if (name.toLowerCase() === directive.name) { + tagName = name.toLowerCase(); + if ((directive.name instanceof RegExp) && directive.name.test(tagName)) { + isDirective = true; + } else if (tagName === directive.name) { + isDirective = true; + } + + if (isDirective) { if (!last) { results.push(directiveText); return; diff --git a/test/test.js b/test/test.js index fc2e383..2eb9670 100644 --- a/test/test.js +++ b/test/test.js @@ -131,6 +131,15 @@ describe('PostHTML-Parser test', function() { expect(parser('', customDirectives)).to.eql(['']); }); + it('should be parse regular expression directive', function() { + var customDirectives = {directives: [ + {name: /\?(php|=).*/, start: '<', end: '>'} + ]}; + + expect(parser('', customDirectives)).to.eql(['']); + expect(parser('', customDirectives)).to.eql(['']); + }); + it('should be parse directives and tag', function() { var customDirectives = {directives: [ {name: '!doctype', start: '<', end: '>'}, From 1b800f71b4dcad11081fb623fee4b305e4ccc234 Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Thu, 18 Jan 2018 21:32:00 +0700 Subject: [PATCH 2/6] split directive checking per condition --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4e0bc88..9ee14c6 100644 --- a/index.js +++ b/index.js @@ -38,7 +38,9 @@ function postHTMLParser(html, options) { tagName = name.toLowerCase(); if ((directive.name instanceof RegExp) && directive.name.test(tagName)) { isDirective = true; - } else if (tagName === directive.name) { + } + + if (tagName === directive.name) { isDirective = true; } From f7d296cd3f6ff6c837a913ec4cbb8938722b43c6 Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Fri, 19 Jan 2018 04:33:31 +0700 Subject: [PATCH 3/6] refactor directive parser --- index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index d92628f..b59155b 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, 'i'); + + return regex.test(tag); + } + + if (tag !== directive.name) { + return false; + } + + return true; + } + function parserDirective(name, data) { var directives = objectAssign(defaultDirectives, options.directives); var last = bufArray.last(); @@ -33,18 +47,9 @@ function postHTMLParser(html, options) { for (var i = 0; i < directives.length; i++) { var directive = directives[i]; var directiveText = directive.start + data + directive.end; - var isDirective = false; tagName = name.toLowerCase(); - if ((directive.name instanceof RegExp) && directive.name.test(tagName)) { - isDirective = true; - } - - if (tagName === directive.name) { - isDirective = true; - } - - if (isDirective) { + if (isDirective(directive, tagName)) { if (!last) { results.push(directiveText); return; From 01f8cad327cd91eca3ae06f10434e718a756ee7c Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Fri, 19 Jan 2018 04:41:35 +0700 Subject: [PATCH 4/6] fix code style --- test/test.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/test/test.js b/test/test.js index f1b62b0..4b9fc52 100644 --- a/test/test.js +++ b/test/test.js @@ -124,27 +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 customDirectives = {directives: [ - {name: /\?(php|=).*/, start: '<', end: '>'} - ]}; + var options = { + directives: [ + { name: /\?(php|=).*/, start: '<', end: '>' } + ] + }; - expect(parser('', customDirectives)).to.eql(['']); - expect(parser('', customDirectives)).to.eql(['']); + 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 = [ @@ -155,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() { From 4bc2807bf671fb46403a488e1459421f91c49725 Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Fri, 19 Jan 2018 04:48:58 +0700 Subject: [PATCH 5/6] build RegEx from RegEx.source --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index b59155b..fa4f77e 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ function postHTMLParser(html, options) { function isDirective(directive, tag) { if (directive.name instanceof RegExp) { - var regex = RegExp(directive.name, 'i'); + var regex = RegExp(directive.name.source, 'i'); return regex.test(tag); } From 6f046c8330ccf6530444779b69540ad45c0a9636 Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Fri, 19 Jan 2018 20:55:07 +0700 Subject: [PATCH 6/6] lowercase and reassign 'name' var --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4c9cdbc..a9e71bb 100644 --- a/index.js +++ b/index.js @@ -43,13 +43,12 @@ function postHTMLParser(html, options) { var directives = [].concat(defaultDirectives, options.directives || []); var last = bufArray.last(); - var tagName; for (var i = 0; i < directives.length; i++) { var directive = directives[i]; var directiveText = directive.start + data + directive.end; - tagName = name.toLowerCase(); - if (isDirective(directive, tagName)) { + name = name.toLowerCase(); + if (isDirective(directive, name)) { if (!last) { results.push(directiveText); return;