示例#1
0
        private static string ReadAllAttributes(string input)
        {
            StringBuilder  output = new StringBuilder();
            StringReader   sr     = new StringReader(input);
            HtmlTextReader reader = new HtmlTextReader(sr);

            while (sr.Peek() >= 0)
            {
                string name  = reader.ReadAttributeName();
                string value = reader.ReadAttributeValue();
                if (!string.IsNullOrEmpty(name))
                {
                    output.Append(output.Length > 0 ? " " : "");
                    output.Append(name + "='" + value + "'");
                }
            }
            return(output.ToString());
        }
示例#2
0
        public void ReadQuotedAttribute()
        {
            string         input;
            HtmlTextReader reader;
            string         output;

            // Ensure we parse to end of single quotes...
            input  = " ='abc'  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Ensure we parse to end of double quotes...
            input  = " =\"abc\"  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Ignore double quotes in the middle
            input  = " ='abc\"def'  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc\"def", output);

            // Ignore end tag in the middle of quotes
            input  = " ='abc>def'  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc>def", output);

            // Parse until end of string with open quote
            input  = "= 'abcdef ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abcdef ", output);

            // Parse until end of string with open quote
            input  = "= \"abcdef ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abcdef ", output);

            // Not checking whitespace inside quotes
            input  = " =\"abcdef ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abcdef ", output);

            // Ignore single quotes in the middle of double quotes
            input  = " =\"abc'def\" ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc'def", output);

            // Ignore end tag in the middle of quotes
            input  = " =\"abc>def\" ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc>def", output);

            // Ensure we don't consume the >.
            input = "  ='abcdef'>";
            StringReader sr = new StringReader(input);

            reader = new HtmlTextReader(sr);
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abcdef", output);
            Assert.AreEqual('>', (char)reader.Peek());
        }
示例#3
0
        public void ReadAttrNameAndVal()
        {
            string         input;
            StringReader   sr;
            HtmlTextReader reader;

            input  = "class='red' ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("red", reader.ReadAttributeValue());

            input  = "class = \"red\" ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("red", reader.ReadAttributeValue());

            input  = "   class =red ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("red", reader.ReadAttributeValue());

            input  = "   class =  red ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("red", reader.ReadAttributeValue());

            input  = "   class =  'red' ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("red", reader.ReadAttributeValue());

            // Make sure forward slashes are skipped in the right places...
            input  = " /class = /red ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("/red", reader.ReadAttributeValue());

            // Make sure forward slashes are skipped in the right places...
            input  = " class/ = red ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual("", reader.ReadAttributeValue());
            Assert.AreEqual("=", reader.ReadAttributeName());
            Assert.AreEqual("", reader.ReadAttributeValue());
            Assert.AreEqual("red", reader.ReadAttributeName());
            Assert.AreEqual("", reader.ReadAttributeValue());

            // Make sure forward slashes are skipped in the right places...
            input  = " /class /= /red ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            Assert.AreEqual("class", reader.ReadAttributeName());
            Assert.AreEqual(String.Empty, reader.ReadAttributeValue());
            Assert.AreEqual("=", reader.ReadAttributeName());
            Assert.AreEqual(String.Empty, reader.ReadAttributeValue());
            Assert.AreEqual("red", reader.ReadAttributeName());
            Assert.AreEqual(String.Empty, reader.ReadAttributeValue());
        }
示例#4
0
        public void ReadUnquotedAttribute()
        {
            string         input;
            HtmlTextReader reader;
            string         output;

            // no whitespace
            input  = "=abc";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Starting whitespace
            input  = "  =abc";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Starting whitespace
            input  = "  \t\n\r=abc";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Ending whitespace
            input  = "=abc  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Whitespace start and end
            input  = "  = abc  ";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Whitespace start and end
            input  = "  =\t\n\rabc \n\r";
            reader = new HtmlTextReader(new StringReader(input));
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);

            // Whitespace end tag in the middle. Ensure
            // we don't consume the char.
            input = "=  abc>def  ";
            StringReader sr = new StringReader(input);

            reader = new HtmlTextReader(sr);
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abc", output);
            Assert.AreEqual('d', (char)reader.Peek());
            Assert.AreEqual(ParseState.Text, reader.ParseState);

            // Ensure we don't consume the last space.
            input  = "  =abcdef ";
            sr     = new StringReader(input);
            reader = new HtmlTextReader(sr);
            output = reader.ReadAttributeValue();
            Assert.AreEqual("abcdef", output);
            Assert.AreEqual(' ', (char)reader.Peek());
        }