posthtml / posthtml-expressions

Use variables, JS-like expressions, and even markup-powered logic in your HTML.
Other
123 stars 20 forks source link

Ignore expressions in quotes #101

Closed cossssmin closed 2 years ago

cossssmin commented 4 years ago

Imagine we run PostHTML with { locals: { foo: 'bar' } } on this HTML:

<p>{{ 1 === 1 ? '{{ foo }}' : 'false' }}</p>

Normally, you'd expect this to be output:

<p>{{ foo }}</p>

... however, I think the plugin tries to parse '{{ foo }}' and it fails with a SyntaxError:

SyntaxError {
  message: 'Invalid or unexpected token',
}

I think posthtml-expressions should not try to parse 'expressions' inside quotes, these should be simply treated as strings and parsing should skip them.

Scrum commented 4 years ago

@cossssmin Hi The point is finding part of the expression and in your case it turns out

1 === 1? '{{foo

If you write a regex that will search for an expression with quotes taken into account, then the question remains unresolved with an expression in attributes like

<p class="{{ dynamicClass }}">