2

I have to encode strings to remove parentheses for Ids for HTML elements.

Parentheses (these ones (,)) aren't valid in HTML Ids, are there standard strings (like those used in URLs) to use?

Is there an existing method that can be used in ASP.NET MVC?

N.B. System.Web.Mvc.HttpUtility.HtmlEncode(string), does not encode parentheses.

StuperUser
  • 10,555
  • 13
  • 78
  • 137

3 Answers3

3

As per the HTML specification (and this question about id's) parentheses aren't allowed in the HTML id attribute. If you need them, you could use string replace, e.g.:

//  ( = 'op--'  Opening Parenthesis
//  ) = 'cp--'  Closing Parenthesis

string id = "collectionName.get_Item(index)";

// encode
string encodedId = id.Replace("(", "op--").Replace(")", "cp--");

// decode
string decodedId = encodedId.Replace("op--", "(").Replace("cp--", ")");
Community
  • 1
  • 1
Andrew
  • 12,991
  • 15
  • 55
  • 85
1

I don't think I understand the question, cos it feels like the answer is to substitute [ and ]. Or even %28 and %29 from the Wikipedia link you gave.

Have I got hold of the wrong end of the stick?

EDIT: From what has been said in the comments, it seems that %28 and %29 are not okay as the % character is also invalid, in which case you could select a substitute that won't appear elsewhere.

EG Something like ( becomes ---28--- (or even ---openbracket---) or something else you can guarantee won't appear elsewhere in the ID (which should be possible).

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • You've got it right Dommer, I was wondering whether there was anything more standard for Html Id s than the URL codes. – StuperUser Jun 15 '11 at 14:41
  • I don't understand how it helps to encode them: neither paranethes ('(' and ')') **nor** the percent character '%' are [valid characters in an HTML Id attribute](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html). – Kirk Woll Jun 15 '11 at 14:43
  • In that case it looks like some kind of substitution is required, which is always a bit risky. Something like `(` becomes `---28---` (or even `---openbracket---`) or something else you can guarantee won't appear elsewhere in the ID. – Tom Chantler Jun 15 '11 at 14:47
  • Are there any standard tokens to use? – StuperUser Jun 15 '11 at 15:11
  • In C# `"get_Item(28)"` and `"get_Item(29)"` WILL encode to `"get_Item---28---28---29---"` and `"get_Item---28---29---29--"`. To prevent any issues decoding, `.Replace("---28---","(")` must be called BEFORE `.Replace("---29---","(")`. They will however still decode without any collisions caused by the end of the "(" token and start of the ")" token creating instances of "---28---" and "---29---" in the string. – StuperUser Jun 16 '11 at 14:04
  • 1
    In that case it would be safer to use something like `--openbracket--` or `--ob--` and `--closebracket--` or `--cb--`. You could obviously tailor this to your own scenario - as this would cause problems with getItem(openbracket) ;-p – Tom Chantler Jun 16 '11 at 15:10
0

If the elements are dynamically created then why not just do a .Replace on the id changing parentheses for, say, underscores?

If the elements are not dynamically created then why do they have parentheses in the ids?!

Phill
  • 1,302
  • 1
  • 9
  • 18
  • They are dynamically created. Using `_` is undesirable since it's not standard and may be used in other parts of the id, so make decoding accurately impossible. – StuperUser Jun 15 '11 at 14:38
  • Does the id follow a pattern that a regex could be used to match the inner part of the id e.g. a number surrounded in parentheses? Do the brackets occur in a predictable position in the string? If not, then with any of the solutions given you will need to ensure to check that the new magic character does not exist in the id and escape it if it does. – Phill Jun 15 '11 at 22:02