Example #1
0
        public void TestSpecialCharacter()
        {
            const string html  = "<>A";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == '<');
            Assert.IsTrue(parse.Read() == '>');
            Assert.IsTrue(parse.Read() == 'A');
        }
Example #2
0
        public void TestComment()
        {
            const string html  = "a<!-- Hello -->b";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 'a');
            Assert.IsTrue(parse.Read() == 0);
            Assert.IsTrue(parse.Read() == 'b');
        }
Example #3
0
        public void TestTagToString()
        {
            const string html  = "<br/>";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 0);
        }
Example #4
0
        public void TestBothWithAttributes()
        {
            const string html  = "<img src=\"picture.gif\" alt=\"A Picture\"/>";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 0);
        }
Example #5
0
        public void TestSimpleAttribute()
        {
            const string html  = "<!DOCTYPE \"test\">";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 0);
            Tag tag = parse.LastTag;

            Assert.AreEqual(tag.ToString(), html);
        }
Example #6
0
        public void TestScript2()
        {
            const string html  = "a<script>1<2</script>b<br>";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 'a');
            Assert.IsTrue(parse.Read() == 0);
            Assert.IsTrue(parse.Read() == '1');
            Assert.IsTrue(parse.Read() == '<');
            Assert.IsTrue(parse.Read() == '2');
            Assert.IsTrue(parse.Read() == 0);
            Assert.IsTrue(parse.Read() == 'b');
            Assert.IsTrue(parse.Read() == 0);
        }
Example #7
0
        public void TestAttributesNoDelim()
        {
            const string html  = "<img src=picture.gif alt=APicture>";
            var          enc   = new ASCIIEncoding();
            var          bis   = new MemoryStream(enc.GetBytes(html));
            var          parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 0);
            Tag tag = parse.LastTag;

            Assert.IsNotNull(tag);
            Assert.IsTrue(tag.Name.Equals("img"));
            Assert.IsTrue(tag.GetAttributeValue("src").Equals("picture.gif"));
            Assert.IsTrue(tag.GetAttributeValue("alt").Equals("APicture"));
        }
        /// <summary>
        /// Load the data units.  Once the lower level data units have been
        /// loaded, the contents can be loaded.
        /// </summary>
        /// <param name="istream">The input stream that the data units are loaded from.</param>
        protected void LoadDataUnits(Stream istream)
        {
            var  text = new StringBuilder();
            int  ch;
            var  parse  = new ReadHTML(istream);
            bool style  = false;
            bool script = false;

            while ((ch = parse.Read()) != -1)
            {
                if (ch == 0)
                {
                    if (style)
                    {
                        CreateCodeDataUnit(text.ToString());
                    }
                    else if (script)
                    {
                        CreateCodeDataUnit(text.ToString());
                    }
                    else
                    {
                        CreateTextDataUnit(text.ToString());
                    }
                    style  = false;
                    script = false;

                    text.Length = 0;
                    CreateTagDataUnit(parse.LastTag);
                    if (String.Compare(parse.LastTag.Name, "style", true) == 0)
                    {
                        style = true;
                    }
                    else if (string.Compare(parse.LastTag.Name,
                                            "script", true) == 0)
                    {
                        script = true;
                    }
                }
                else
                {
                    text.Append((char)ch);
                }
            }

            CreateTextDataUnit(text.ToString());
        }
Example #9
0
        /// <summary>
        /// Perform a Yahoo search.
        /// </summary>
        /// <param name="url">The REST URL.</param>
        /// <returns>The search results.</returns>
        private ICollection <Uri> DoSearch(Uri url)
        {
            ICollection <Uri> result = new List <Uri>();
            // submit the search
            WebRequest http     = WebRequest.Create(url);
            var        response = (HttpWebResponse)http.GetResponse();

            using (Stream istream = response.GetResponseStream())
            {
                var  parse   = new ReadHTML(istream);
                var  buffer  = new StringBuilder();
                bool capture = false;

                // parse the results
                int ch;
                while ((ch = parse.Read()) != -1)
                {
                    if (ch == 0)
                    {
                        Tag tag = parse.LastTag;
                        if (tag.Name.Equals("Url", StringComparison.CurrentCultureIgnoreCase))
                        {
                            buffer.Length = 0;
                            capture       = true;
                        }
                        else if (tag.Name.Equals("/Url", StringComparison.CurrentCultureIgnoreCase))
                        {
                            result.Add(new Uri(buffer.ToString()));
                            buffer.Length = 0;
                            capture       = false;
                        }
                    }
                    else
                    {
                        if (capture)
                        {
                            buffer.Append((char)ch);
                        }
                    }
                }
            }

            response.Close();

            return(result);
        }
Example #10
0
        public void TestBoth()
        {
            const string html     = "<br/>";
            const string htmlName = "br";
            var          enc      = new ASCIIEncoding();
            var          bis      = new MemoryStream(enc.GetBytes(html));
            var          parse    = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == 0);
            Tag tag = parse.LastTag;

            Assert.IsNotNull(tag);
            Assert.IsTrue(tag.TagType == Tag.Type.Begin);
            Assert.IsTrue(tag.Name.Equals(htmlName));
            parse.ReadToTag();
            tag = parse.LastTag;
            Assert.IsNotNull(tag);
            Assert.IsTrue(tag.TagType == Tag.Type.End);
            Assert.IsTrue(tag.Name.Equals(htmlName));
        }
Example #11
0
        public void TestAttributeLess()
        {
            const string html = "12<b>12</b>1";
            var          enc  = new ASCIIEncoding();
            var          bis  = new MemoryStream(enc.GetBytes(html));

            var parse = new ReadHTML(bis);

            Assert.IsTrue(parse.Read() == '1');
            Assert.IsTrue(parse.Read() == '2');
            Assert.IsTrue(parse.Read() == 0);
            Assert.IsTrue(parse.LastTag.Name.Equals("b"));
            Assert.IsTrue(parse.LastTag.TagType == Tag.Type.Begin);
            Assert.IsTrue(parse.Read() == '1');
            Assert.IsTrue(parse.Read() == '2');
            Assert.IsTrue(parse.Read() == 0);
            Tag tag = parse.LastTag;

            Assert.IsTrue(tag.Name.Equals("b"));
            Assert.IsTrue(tag.TagType == Tag.Type.End);
            Assert.AreEqual(tag.ToString(), "</b>");
            Assert.IsTrue(parse.Read() == '1');
        }