pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
391 stars 42 forks source link

Ternary conditional operator fails with linebreak #225

Closed radiocity closed 5 years ago

radiocity commented 5 years ago

Hello,

I encountered an issue with the following code:

+contact-form(i18n, true)(
  action = network.form_handler ?
    network.form_handler :
    "/api/forms/handler"
)

I expected to get:

<form action="/api/forms/handler">...</form>

But I actually get:

Failed to read: Failed to match pattern: The backtrack limit was exhausted (Increase pcre.backtrack_limit in php.ini) 
Near:  ?

File: /source/new/pug/pages/networks.pug
Line: X 
Offset: Y (at question mark)
Position: Z

Workaround

+contact-form(i18n, true)(
  action = network.form_handler ? network.form_handler : "/api/forms/handler"
)

Thanks!

kylekatarnls commented 5 years ago

Hi,

As the error says: The backtrack limit was exhausted (Increase pcre.backtrack_limit in php.ini) you need to increase the pcre.backtrack_limit in php.ini (the default value 1000000 should be enough, but you may have lower setting enforced). Else you need to double the value until the error disappears.

Pug-php is a parser, it uses complex regular expression that cannot work with a low pcre.backtrack_limit setting.

Note than using the cache, those RegExp are no longer executed in production. This is needed only for the compilation.

Thanks!

radiocity commented 5 years ago

That are bad news, actually. Source code isn`t so complex as could be.

alessandro-fazzi commented 5 years ago

Just saying for the sake of sharing: having a simple (what does it mean simple?) source doesn't mean the parser will use RegEx-es with less backtracking. Moreover:

Don't mind it so much 😃 ❤️ (my 2 joking cents)

radiocity commented 5 years ago

Well, parser fails at production environment... I thought developers should care about optimization more than cutting-corner solutions or jokes.

kylekatarnls commented 5 years ago

Source code isn`t so complex as could be.

I did not say it depends on the source complexity.

Well, parser fails at production environment

It depends how you deploy, it also depends on the framework (for example laravel-pug will rely on the template cache of Laravel) and if you pre-compile templates in production (you should).

Then you may cache templates on the production machine if you do it in your deployment script (so it does not impact your users). Else you can compile templates then deploy the whole thing on the production machine, this way the production machine will never render any templates.

Finally, I don't think you reached pcre.backtrack_limit because RegExp run into an excessive backtrack loop. I think you more likely have a smaller limit than the default value (1000000) and a normal application should have at least 1000000.

kylekatarnls commented 5 years ago

I thought developers should care about optimization more than cutting-corner solutions or jokes.

That's why you should care about our recommendations for production: https://phug-lang.com/#usage

By following it, you will really optimize the page display and resources consumption. Reducing pcre.backtrack_limit will just force a limit for RegExp calculation, while following caching method will not run those RegExps at all when users load pages.