Ejemplo n.º 1
0
        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 = HtmlData.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 = HtmlData.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 = HtmlData.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 = HtmlData.AttributeEncode(neither, true, out quoteChar);
            Assert.AreEqual(neither, result, "With neither, nothing changeed");
            Assert.AreEqual("\"", quoteChar, "Quote char was a quote with both");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Render an attribute.
        /// </summary>
        ///
        /// <param name="writer">
        /// The writer to which output is written.
        /// </param>
        /// <param name="name">
        /// The name of the attribute.
        /// </param>
        /// <param name="value">
        /// The attribute value.
        /// </param>
        /// <param name="quoteAll">
        /// true to require quotes around the attribute value, false to use quotes only if needed.
        /// </param>

        protected void RenderAttribute(TextWriter writer, string name, string value, bool quoteAll)
        {
            // validator.nu: as it turns out "" and missing are synonymous
            // don't ever render attr=""

            if (value != null && value != "")
            {
                string quoteChar;
                string attrText = HtmlData.AttributeEncode(value,
                                                           quoteAll,
                                                           out quoteChar);
                writer.Write(name.ToLower());
                writer.Write("=");
                writer.Write(quoteChar);
                writer.Write(attrText);
                writer.Write(quoteChar);
            }
            else
            {
                writer.Write(name);
            }
        }