Skip to content

Commit 0752b3d

Browse files
authored
Merge pull request #27 from okyantoro/regex-directive
feat(index): add support for `{RegExp}` directives (`options.directives`)
2 parents 04200e6 + 6f046c8 commit 0752b3d

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Tag objects can contain three keys. The `tag` key takes the name of the tag as t
8282
### `directives`
8383
Type: `Array`
8484
Default: `[{name: '!doctype', start: '<', end: '>'}]`
85-
Description: *Adds processing of custom directives*
85+
Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*
8686

8787
## License
8888

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ function postHTMLParser(html, options) {
2525
return this[this.length - 1];
2626
};
2727

28+
function isDirective(directive, tag) {
29+
if (directive.name instanceof RegExp) {
30+
var regex = RegExp(directive.name.source, 'i');
31+
32+
return regex.test(tag);
33+
}
34+
35+
if (tag !== directive.name) {
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
2842
function parserDirective(name, data) {
2943
var directives = [].concat(defaultDirectives, options.directives || []);
3044
var last = bufArray.last();
@@ -33,7 +47,8 @@ function postHTMLParser(html, options) {
3347
var directive = directives[i];
3448
var directiveText = directive.start + data + directive.end;
3549

36-
if (name.toLowerCase() === directive.name) {
50+
name = name.toLowerCase();
51+
if (isDirective(directive, name)) {
3752
if (!last) {
3853
results.push(directiveText);
3954
return;

test/test.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,33 @@ describe('PostHTML-Parser test', function() {
124124
});
125125

126126
it('should be parse directive', function() {
127-
var customDirectives = {directives: [
128-
{name: '?php', start: '<', end: '>'}
129-
]};
127+
var options = {
128+
directives: [
129+
{ name: '?php', start: '<', end: '>' }
130+
]
131+
};
130132

131-
expect(parser('<?php echo "Hello word"; ?>', customDirectives)).to.eql(['<?php echo "Hello word"; ?>']);
133+
expect(parser('<?php echo "Hello word"; ?>', options)).to.eql(['<?php echo "Hello word"; ?>']);
134+
});
135+
136+
it('should be parse regular expression directive', function() {
137+
var options = {
138+
directives: [
139+
{ name: /\?(php|=).*/, start: '<', end: '>' }
140+
]
141+
};
142+
143+
expect(parser('<?php echo "Hello word"; ?>', options)).to.eql(['<?php echo "Hello word"; ?>']);
144+
expect(parser('<?="Hello word"?>', options)).to.eql(['<?="Hello word"?>']);
132145
});
133146

134147
it('should be parse directives and tag', function() {
135-
var customDirectives = {directives: [
136-
{name: '!doctype', start: '<', end: '>'},
137-
{name: '?php', start: '<', end: '>'}
138-
]};
148+
var options = {
149+
directives: [
150+
{ name: '!doctype', start: '<', end: '>' },
151+
{ name: '?php', start: '<', end: '>' }
152+
]
153+
};
139154

140155
var html = '<!doctype html><html><?php echo \"Hello word\"; ?></html>';
141156
var tree = [
@@ -146,7 +161,7 @@ describe('PostHTML-Parser test', function() {
146161
}
147162
];
148163

149-
expect(parser(html, customDirectives)).to.eql(tree);
164+
expect(parser(html, options)).to.eql(tree);
150165
});
151166

152167
it('should be parse tag', function() {

0 commit comments

Comments
 (0)