Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final nitpick directive.name.source => directive.name (But both work)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

travis gives this error if we give regex parameter for regex and supply flag.

1) PostHTML-Parser test should be parse regular expression directive:
     TypeError: Cannot supply flags when constructing one RegExp from another
      at new RegExp (native)
      at RegExp (native)
      at isDirective (index.js:9:991)
      at Object.parserDirective [as onprocessinginstruction] (index.js:9:2022)
      at Parser.onprocessinginstruction (node_modules/htmlparser2/lib/Parser.js:275:13)
      at Tokenizer._stateInProcessingInstruction (node_modules/htmlparser2/lib/Tokenizer.js:345:13)
      at Tokenizer._parse (node_modules/htmlparser2/lib/Tokenizer.js:686:9)
      at Tokenizer.write (node_modules/htmlparser2/lib/Tokenizer.js:632:7)
      at Parser.write (node_modules/htmlparser2/lib/Parser.js:334:18)
      at postHTMLParser (index.js:9:5732)
      at parser (index.js:9:6155)
      at parserWrapper (index.js:9:6665)
      at Context.<anonymous> (test/test.js:143:16)

I have tried in f5249b2 and the test result https://travis-ci.org/posthtml/posthtml-parser/builds/330561437 is failed.

Copy link
Member

@michael-ciniawsky michael-ciniawsky Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇 I misread the MDN snippet which clearly states starting from ECMAScript 2015 (6) it no longer throws. You're right for the current targets this definitely needs to be handled via pattern.source

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any plan to move to ES6?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we support the node> = 0.10


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();
Expand All @@ -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;
Expand Down
33 changes: 24 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php echo "Hello word"; ?>', customDirectives)).to.eql(['<?php echo "Hello word"; ?>']);
expect(parser('<?php echo "Hello word"; ?>', options)).to.eql(['<?php echo "Hello word"; ?>']);
});

it('should be parse regular expression directive', function() {
var options = {
directives: [
{ name: /\?(php|=).*/, start: '<', end: '>' }
]
};

expect(parser('<?php echo "Hello word"; ?>', options)).to.eql(['<?php echo "Hello word"; ?>']);
expect(parser('<?="Hello word"?>', options)).to.eql(['<?="Hello word"?>']);
});

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 = '<!doctype html><html><?php echo \"Hello word\"; ?></html>';
var tree = [
Expand All @@ -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() {
Expand Down