-
-
Notifications
You must be signed in to change notification settings - Fork 22
feat(index): add support for {RegExp}
directives (options.directives
)
#27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Example: The custom directive {name: /\?(php|=).*/, start: '<', end: '>'} will match any directive with <?php ?> or <?= ?> pattern
index.js
Outdated
isDirective = true; | ||
} else if (tagName === directive.name) { | ||
isDirective = true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one condition is better:
if ((directive.name instanceof RegExp) && directive.name.test(tagName)) {
isDirective = true;
}
if (tagName === directive.name) {
isDirective = true;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
{RegExp}
directives (options.directives
)
index.js
Outdated
for (var i = 0; i < directives.length; i++) { | ||
var directive = directives[i]; | ||
var directiveText = directive.start + data + directive.end; | ||
var isDirective = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function isDirective (directive, tag) {
if (directive.name instanceof RegExp) {
const regex = RegExp(directive.name, 'i')
return regex.test(tag)
}
if (tag !== directive.name) {
return false
}
return true
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Untested, but please a make this a private helper function instead of n
variables and {Boolean}
switches/assignments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line const regex = RegExp(directive.name, 'i')
const
is not supported by es5 and RegExp(directive.name, 'i')
only works in ES6.
Related to MDN
Starting with ECMAScript 6, new RegExp(/ab+c/, 'i') no longer throws a TypeError ("can't supply flags when constructing one RegExp from another") when the first argument is a RegExp and the second flags argument is present. A new RegExp from the arguments is created instead.
So we change the const
to var
and RegExp(directive.name, 'i')
to RegExp(directive.name.source, 'i')
Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, const
sadly needs to var
here 😞
var pattern = /a/
var regex = RegExp(pattern, 'i')
console.log(regex.test('Abc'))
⚠️ Note that it'sRegExp(pattern, flags)
notnew RegExp(string, flags)
(Nonew
)
test/test.js
Outdated
}); | ||
|
||
it('should be parse regular expression directive', function() { | ||
var customDirectives = {directives: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customDirectives
=> options
&& \n
after {
test/test.js
Outdated
|
||
it('should be parse regular expression directive', function() { | ||
var customDirectives = {directives: [ | ||
{name: /\?(php|=).*/, start: '<', end: '>'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\s
after {
&& }
please
{RegExp}
directives (options.directives
){RegExp}
directives (options.directives
)
index.js
Outdated
var directiveText = directive.start + data + directive.end; | ||
var isDirective = false; | ||
|
||
tagName = name.toLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just reassign name
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@okyantoro Thx
|
||
function isDirective(directive, tag) { | ||
if (directive.name instanceof RegExp) { | ||
var regex = RegExp(directive.name.source, 'i'); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@gitscrum Is this really a semver major ? :) Looks like a minor (feature)... |
@okyantoro thanks for your contribution, we really appreciate this and are looking forward to a new pool of requisites 👍 |
Thank you @gitscrum |
Example:
The custom directive
{name: /\?(php|=).*/, start: '<', end: '>'}
will match any directive with<?php ?>
or<?= ?>
pattern.Note:
I thought that this commit will solve #26 (comment) if we use regular expression directive started with
<%
but after I checked the library, I found that any tag started with%
for example<%=name%>
will be treated as a tag by the library, so this tag will never touchonprocessinginstruction
event. So this is an exception.