Skip to content

Conversation

oantoro
Copy link
Contributor

@oantoro oantoro commented Jan 18, 2018

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 touch onprocessinginstruction event. So this is an exception.

Example:
The custom directive {name: /\?(php|=).*/, start: '<', end: '>'} will match any directive with <?php ?> or <?= ?> pattern
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 9403660 on okyantoro:regex-directive into 923b16a on posthtml:master.

index.js Outdated
isDirective = true;
} else if (tagName === directive.name) {
isDirective = true;
}
Copy link
Member

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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@Scrum Scrum changed the title directive: add support for regular expression custom directive. [perf]: add support for regular expression custom directive. Jan 18, 2018
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 1b800f7 on okyantoro:regex-directive into 923b16a on posthtml:master.

@Scrum Scrum modified the milestones: 0.3.3, 0.4.0 Jan 18, 2018
@Scrum Scrum self-assigned this Jan 18, 2018
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 2ca0e4e on okyantoro:regex-directive into 29944dc on posthtml:master.

@michael-ciniawsky michael-ciniawsky changed the title [perf]: add support for regular expression custom directive. feat: add support for {RegExp} directives (options.directives) Jan 18, 2018
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;
Copy link
Member

@michael-ciniawsky michael-ciniawsky Jan 18, 2018

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
}

Copy link
Member

@michael-ciniawsky michael-ciniawsky Jan 18, 2018

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

Copy link
Contributor Author

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

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.

Yep, const sadly needs to var here 😞

var pattern = /a/

var regex = RegExp(pattern, 'i')

console.log(regex.test('Abc'))

⚠️ Note that it's RegExp(pattern, flags) not new RegExp(string, flags) (No new)

test/test.js Outdated
});

it('should be parse regular expression directive', function() {
var customDirectives = {directives: [
Copy link
Member

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: '>'}
Copy link
Member

Choose a reason for hiding this comment

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

\s after { && } please

@michael-ciniawsky michael-ciniawsky changed the title feat: add support for {RegExp} directives (options.directives) feat(index): add support for {RegExp} directives (options.directives) Jan 18, 2018
@Scrum Scrum removed the request for review from voischev January 18, 2018 17:46
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 0da0c81 on okyantoro:regex-directive into 04200e6 on posthtml:master.

index.js Outdated
var directiveText = directive.start + data + directive.end;
var isDirective = false;

tagName = name.toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

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

Just reassign name here

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 6f046c8 on okyantoro:regex-directive into 04200e6 on posthtml:master.

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a 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) {
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

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Jan 19, 2018

@gitscrum Is this really a semver major ? :) Looks like a minor (feature)...

@Scrum
Copy link
Member

Scrum commented Jan 23, 2018

@okyantoro thanks for your contribution, we really appreciate this and are looking forward to a new pool of requisites 👍

@oantoro
Copy link
Contributor Author

oantoro commented Jan 24, 2018

Thank you @gitscrum

@oantoro oantoro deleted the regex-directive branch January 24, 2018 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants