jsoverson / preprocess

Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration
Other
366 stars 80 forks source link

@exec parameters are not parsed correctly #78

Open anseki opened 9 years ago

anseki commented 9 years ago

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 commented 9 years ago

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

anseki commented 9 years ago

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.