nodecraft / ya-bbcode

Yet another BBCode Parser
MIT License
17 stars 5 forks source link

Tags regex breaks when one of the attributes contains a whitespace #162

Closed spuxx1701 closed 1 year ago

spuxx1701 commented 1 year ago

Hi there!

It appears that the tags regex fails to recognize a tag when that tag has whitespaces within its attributes.

Example:

[quote=123,456,\"Some User\"][b] ... [/b][/quote]

Results in a Array(3) [ "[b]", "[/b]", "[/quote]" ] tags array. Replacing the whitespace with e.g. an _ fixes the behavior. I have a usecase where I'm trying to parse BBCodes quotes where the author names can contain whitespaces. I'm not the best with regexes, but maybe you would know how to fix this @Cherry? :)

spuxx1701 commented 1 year ago

It appears that removing the whitespace matcher \s rom the tags regex seems to fix the issue for me without introducing any side effects. What's the purpose of that matcher? The tags regex would then look like this: /(\[[^\]^]+])/g

spuxx1701 commented 1 year ago

I've provided a small PR that could offer a potential fix, although i'm not entirely sure about side effects. https://github.com/nodecraft/ya-bbcode/pull/163

Cherry commented 1 year ago

Thanks for the issue! What HTML would you expect that to produce?

<blockquote author="123,456,"Some User""><strong> ... </strong></blockquote>

Something like that doesn't feel like valid HTML to me. Should the quotes inside the attribute be escaped? Is that valid? What do you think? Can you add a test in the test.js file for the expected behaviour with your PR?

spuxx1701 commented 1 year ago

In my case I needed to write a custom parser, but since ya-bbcode didn't recognize the tag in the first place, I needed to solve that first. As you can see here, I needed to parse quotes into some more complex HTML consisting of several elements (including an tag and the actual blockquote).

spuxx1701 commented 1 year ago

I think the default quote parser is fine as is, only the \s matcher in the regex prevented me from doing what I wanted to do with a custom parser.

Cherry commented 1 year ago

Ah I see, thank you. If you could add a test to the PR so it reflects the behaviour you're expecting, that would be great.

Cherry commented 1 year ago

Fixed in #163. New version will be released later today 🎉 Thank you!