Attribute with a space not working


I'm using BBCode for a forum, and need to be able to do quotes using the name of the original author.
This works just fine with:
new BBTag("quote", "<blockquote><cite>${user} wrote:</cite>", "</blockquote>", new BBAttribute("user", ""), new BBAttribute("user", "user"))
unless the username contains a space.
Is there any reason why a space is included in line 317 in BBCodeParser ?
var endIndex = input.IndexOfAny(" []".ToCharArray(), end);
What will break if I remove the space from that string?


saphua wrote May 20, 2011 at 12:29 PM

I have the same issue. Did you find out a fix for this?

steentottrup wrote May 20, 2011 at 2:32 PM

The only fix I've "found" is the one I describe. I've removed the space from the string and haven't had any problems so far, so:
        var endIndex = input.IndexOfAny("[]".ToCharArray(), end);

codekicker wrote Jun 21, 2011 at 10:56 AM

The problem is that the space enables you to have multiple attribute values. A better solution would be to be able to enclose attribute values in quotes, but this would be a little too much work for me. I would include a patch of yours however.

saphua wrote Jun 24, 2011 at 1:10 PM

So what are the consquences of using the fix suggested by steentottrup?

jwendl wrote Jul 9, 2011 at 4:38 AM

A VERY hackish way to approach this would be to use a comma instead of whitespace to separate attributes.
    static string ParseAttributeValue(string input, ref int pos)
        var end = pos;

        if (end >= input.Length || input[end] != '=') return null;

        var endIndex = input.IndexOfAny(",[]".ToCharArray(), end);
        if (endIndex == -1) endIndex = input.Length;

        var valStart = pos + 1;
        var result = input.Substring(valStart, endIndex - valStart);
        pos = endIndex;
        return result;
    static bool ParseWhitespace(string input, ref int pos)
        int end = pos;
        while (end < input.Length && (char.IsWhiteSpace(input[end]) || input[end] == ','))

        var found = pos != end;
        pos = end;
        return found;
The test could be modified to look like:
    public void AttributeValueTransformer()
        var parser = new BBCodeParser(ErrorMode.Strict, null, new[]
                new BBTag("font", "<span style=\"${color}${font}\">", "</span>", true, true,
                    new BBAttribute("color", "color", attributeRenderingContext => string.IsNullOrEmpty(attributeRenderingContext.AttributeValue) ? "" : "color:" + attributeRenderingContext.AttributeValue + ";"),
                    new BBAttribute("font", "font", attributeRenderingContext => string.IsNullOrEmpty(attributeRenderingContext.AttributeValue) ? "" : "font-family:" + attributeRenderingContext.AttributeValue + ";")),

        Assert.AreEqual("<span style=\"color:red;font-family:Arial;\">abc</span>", parser.ToHtml("[font color=red,font=Arial]abc[/font]"));
        Assert.AreEqual("<span style=\"color:red;font-family:Arial Foobar;\">abc</span>", parser.ToHtml("[font color=red,font=Arial Foobar]abc[/font]"));
        Assert.AreEqual("<span style=\"color:red;\">abc</span>", parser.ToHtml("[font color=red]abc[/font]"));
All of the other tests (744 of them which is impressive) pass with these modifications. Not sure that means anything.

jwendl wrote Jul 9, 2011 at 4:42 AM

After looking at this a little closer. Turns out you can trick things the good ol html way. [tag id=foo bar]

Works in my situation.

codekicker wrote Jul 12, 2011 at 10:34 AM

"So what are the consquences of using the fix suggested by steentottrup?": You cannot have multiple attributes.

"After looking at this a little closer. Turns out you can trick things the good ol html way. [tag id=foo bar] ": this should not work because the attribute value should be html encoded. It would end up double encoded. Are you sure this works?

wrote Aug 16, 2011 at 5:12 PM

wrote Sep 29, 2011 at 8:09 PM

orwellophile wrote Dec 27, 2011 at 1:37 PM

I'd really love to see how this fix would work with the following:

(note: I'm using the extremely non-standard BBCode employed by jwendl here)

[font font=courier new,courier,monospace color=red]Red Courier[/font]

wrote Feb 14, 2013 at 2:41 AM

wrote May 16, 2013 at 7:20 AM

wrote May 16, 2013 at 7:20 AM

wrote Jun 14, 2013 at 6:52 AM