Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same tag issue #4

Open
gonpress opened this issue Mar 9, 2024 · 1 comment
Open

Same tag issue #4

gonpress opened this issue Mar 9, 2024 · 1 comment

Comments

@gonpress
Copy link

gonpress commented Mar 9, 2024

Hello

I'm having a problem using bbcode parser

[b][size=5][bookblock] test [/bookblock][/size][/b]
[b][size=5][bookblock] test2 [/bookblock][/size][/b]

The problem is that [b] and [bookblock] in the above case are recognized as the same tag.

@gonpress
Copy link
Author

gonpress commented Mar 9, 2024

       void GetNestedEndTag(ref BBCodeNodePosition nodePos, string assumedCloseTag, in string bbCode) {
            //nodePos.CloseTagStart = bbCode.IndexOf(assumedCloseTag, nodePos.OpenTagEnd, StringComparison.InvariantCultureIgnoreCase);
            // Keep open to match any arguments
            string lowerOpenTag = $"[{nodePos.Node.TagName.ToLower()}";
            string lowerAssumedCloseTag = assumedCloseTag.ToLower();
            int endTagSearchStartPos = nodePos.OpenTagEnd;
            bool openCloseTagsMatch = false;
            while (!openCloseTagsMatch) {
                // By ignoring cases, bbcodes like [QUOTE]abc[/quote] got matched correctly, Search only for end tags based on open tags. This is enough since bbcode would be invalid 
                // if tags inside doesn't match. And we can't search for all non closing tags since some tags were not closed like [hr] and we got false positive matches.
                int closeTagStart = bbCode.IndexOf(assumedCloseTag, endTagSearchStartPos, StringComparison.InvariantCultureIgnoreCase);
                if (closeTagStart == -1) {
                    // Happens if no closing tag could be found. Mostly false positive matches like "[Help] XYZ not working", smileys and so on
                    break;
                }

                nodePos.CloseTagStart = closeTagStart;
                nodePos.CloseTagEnd = closeTagStart + assumedCloseTag.Length;

                string contentLower = bbCode.Substring(nodePos.OpenTagStart, nodePos.CloseTagEnd - nodePos.OpenTagStart);
                // .NET Standard doesn't support the simple Split(string delimiter) overload, but it seems we can use the following overload without any issues
                int openTagsCount = contentLower.Split(new string[] { lowerOpenTag }, StringSplitOptions.None).Length - 1;
                int closeTagsCount = contentLower.Split(new string[] { lowerAssumedCloseTag }, StringSplitOptions.None).Length - 1;

                openCloseTagsMatch = openTagsCount == closeTagsCount;
                endTagSearchStartPos = nodePos.CloseTagStart + 1;
            }
        }

How about changing the contentLower.Split part to a regular expression pattern?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant