luyadev / luya

LUYA is a scalable web framework and content management system with the goal to please developers, clients and users alike.
https://luya.io
MIT License
811 stars 205 forks source link

File tag parentheses #1907

Closed TheMaaarc closed 5 years ago

TheMaaarc commented 5 years ago

If I use parentheses in the title part of the tag, the inner parentheses will close the link even if escaped.

Example:

file[1](Example file (PDF))

file[1](Example file \(PDF\))

Both examples result in:

<a href="...">Example file (PDF</a>)

rolandschaub commented 5 years ago

https://github.com/luyadev/luya/blob/707c6f7e8a725ae779abb7853009382185ddc90b/core/TagParser.php#L42

Mit dem negativen Lookbehind (?<!\) sollte sich das Escapen von Klammern mittels Backslash ermöglichen lassen:

REGEX = '/(?<function>[a-z]+)\[(?<value>.*?)\]((?<!\\)\((?<sub>.*?)(?<!\\)\))?/mi';

Hier getestet: https://regex101.com/r/aNMm3o/1

Natürlich müssten die Escape-Character vor der Ausgabe entfernt werden.

nadar commented 5 years ago

Perfect! Thanks @rolandschaub i will add this regex rule and make some unit tests.

rolandschaub commented 5 years ago

Yes, please test it first.

nadar commented 5 years ago

@rolandschaub i have made a new branch with the regex and a unit test which should not fail if everything works as expected.

If you like to test:

  1. git clone
  2. checkout issue-1907 branch
  3. run composer install
  4. run php unit test: ./vendor/bin/phpunit tests/core/TagParserTest.php
rolandschaub commented 5 years ago

/(?<function>[a-z]+)\[(?<value>.*?)\]((?<!\\\)\((?<sub>.*?)(?<!\\\)\))?/miis working!

rolandschaub commented 5 years ago

/(?<function>[a-z]+)\[(?<value>.*?)\]((?<!\\\\)\((?<sub>.*?)(?<!\\\\)\))?/mi

Four backslashes is the correct way to do it, see here:

https://stackoverflow.com/questions/30254174/preg-match-with-backslashes-doesnt-work-as-expected-throws-error

Step 1: PHP turns \\\\ into two literal \\ Step 2: Regex turns these \\ into one literal \