Skip to content
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

@exec parameters are not parsed correctly #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

anseki
Copy link

@anseki anseki commented Jul 9, 2015

The string parameters that include the quotes or commas are not parsed correctly.

For example:

var src = 'foo\n<!-- @exec FNC(' +
  'param1, ' +                            // 0. bare string
  '\'param2\', ' +                        // 1. quoted plain string
  '\'Click "START" button.\', ' +         // 2. double-quotes in single-quotes
  '"Click \'START\' button.", ' +         // 3. single-quotes in double-quotes
  '\'Thu, 09 Jul 2015\', ' +              // 4. comma in quoted string
  '\'Say "1, 2, 3".\', ' +                // 5. comma in quoted string in quoted string
  '\'Click "START", and select one.\'' +  // 6. comma next of quote
  ') -->\nbar';

console.log('==== SRC: ' + src);
console.log('==== RES: ' +
  require('preprocess').preprocess(src, {
    FNC : function() {
      // List all arguments
      return Array.prototype.slice.call(arguments)
        .map(function(arg, i) { return i + ': <' + arg + '>'; }).join('\n');
    },
    param1: 'p1'
  })
);

Result:

==== SRC: foo
<!-- @exec FNC(param1, 'param2', 'Click "START" button.', "Click 'START' button.", 'Thu, 09 Jul 2015', 'Say "1, 2, 3".', 'Click "START", and select one.') -->
bar
==== RES: foo
0: <p1>
1: <param2>
2: <Click "START" button.>
3: <Click 'START' button.>
4: <undefined>
5: <undefined>
6: <undefined>
7: <undefined>
8: <undefined>
9: <Click "START>
10: <undefined>
bar

0, 1, 2, 3 are OK.
But others are not and number of parameters is incorrect.

Result by fixed code:

==== SRC: foo
<!-- @exec FNC(param1, 'param2', 'Click "START" button.', "Click 'START' button.", 'Thu, 09 Jul 2015', 'Say "1, 2, 3".', 'Click "START", and select one.') -->
bar
==== RES: foo
0: <p1>
1: <param2>
2: <Click "START" button.>
3: <Click 'START' button.>
4: <Thu, 09 Jul 2015>
5: <Say "1, 2, 3".>
6: <Click "START", and select one.>
bar

@anseki
Copy link
Author

anseki commented Jul 9, 2015

Sorry, I did not write test.
I have no time now.

@anseki
Copy link
Author

anseki commented Jul 17, 2015

I found another problem that is caused by this bug.
For example:

To easy change a style of a specific text by a specific CSS class.

<!-- @exec showWithClass('Back to ABOUT, HOME, NEWS or EVENTS', 'warning') -->

Desired Result:

<span class="warning">Back to ABOUT, HOME, NEWS or EVENTS</span>
// shallow copy of ENV
var context = Object.keys(process.env).reduce(
  function(env, prop) { env[prop] = process.env[prop]; return env; }, {});

// add function
context.showWithClass = function(text, className) {
  return '<span class="' + className + '">' + text + '</span>';
  // In actuality, do something more. Otherwise, @echo is good enough.
};

console.log(require('preprocess').preprocess(html, context));

Result:

<span class="/home/USERNAME">undefined</span>

Because a quoted text was split by commas, HOME environment variable was shown. And a text was lost.
This PR solves this problem too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant