public void AttributeEncode() { string onlyQuotes = "{\"someprop\": 1, \"someprop2\", \"someval\"}"; string onlyApos = "{'someprop': 1, 'someprop2', 'someval'}"; string both = "{\"someprop\": 1, \"someprop2\", \"o'brien\"}"; string neither = "plain old text"; string quoteChar; string result = Objects.AttributeEncode(onlyQuotes, true, out quoteChar); Assert.AreEqual(onlyQuotes, result, "With only quotes, nothing changed"); Assert.AreEqual("'", quoteChar, "Quote char was an apostrophe with only quotes"); result = Objects.AttributeEncode(onlyApos, true, out quoteChar); Assert.AreEqual(onlyApos, result, "With only apostrophes, nothing changed"); Assert.AreEqual("\"", quoteChar, "Quote char was a quote with only apos"); result = Objects.AttributeEncode(both, true, out quoteChar); string expected = "{\"someprop\": 1, \"someprop2\", \"o'brien\"}"; Assert.AreEqual(expected, result, "With both, only apostrophes changed"); Assert.AreEqual("'", quoteChar, "Quote char was an apos with both"); result = Objects.AttributeEncode(neither, true, out quoteChar); Assert.AreEqual(neither, result, "With neither, nothing changeed"); Assert.AreEqual("\"", quoteChar, "Quote char was a quote with both"); }
/// <summary> /// TODO this really should be in Attributes /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> protected void RenderAttribute(StringBuilder sb, string name, string value, bool quoteAll) { if (value != null) { string quoteChar; string attrText = Objects.AttributeEncode(value, quoteAll, out quoteChar); sb.Append(name); sb.Append("="); sb.Append(quoteChar); sb.Append(attrText); sb.Append(quoteChar); } else { sb.Append(name); } }