GetDataUnit() public method

Get the DataUnit unit at the specified index.
public GetDataUnit ( int i ) : DataUnit
i int The index to use.
return DataUnit
Ejemplo n.º 1
0
        public void TestSimple()
        {
            var     load = new LoadWebPage(null);
            WebPage page = load.Load("a<b>b</b>c");

            Assert.AreEqual(5, page.Data.Count);

            // Index 0 (text)
            var textDu = (TextDataUnit)page.GetDataUnit(0);

            Assert.AreEqual("a", textDu.ToString());
            // Index 1 (tag)
            var tagDu = (TagDataUnit)page.GetDataUnit(1);

            Assert.AreEqual("b", tagDu.Tag.Name);
            Assert.AreEqual("<b>", tagDu.Tag.ToString());
            Assert.AreEqual(Tag.Type.Begin, tagDu.Tag.TagType);
            // Index 2 (text)
            textDu = (TextDataUnit)page.GetDataUnit(2);
            Assert.AreEqual("b", textDu.ToString());
            // Index 3 (tag)
            tagDu = (TagDataUnit)page.GetDataUnit(3);
            Assert.AreEqual("b", tagDu.Tag.Name);
            Assert.AreEqual(Tag.Type.End, tagDu.Tag.TagType);
            // Index 4 (text)
            textDu = (TextDataUnit)page.GetDataUnit(4);
            Assert.AreEqual("c", textDu.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  Find the end tag that lines up to the beginning tag.
        /// </summary>
        /// <param name="index">The index to start the search on. This specifies
        /// the starting data unit.</param>
        /// <param name="tag">The beginning tag that we are seeking the end tag
        /// for.</param>
        /// <returns>The index that the ending tag was found at. Returns -1
        /// if not found.</returns>
        protected int FindEndTag(int index, Tag tag)
        {
            int depth = 0;
            int count = index;

            while (count < _page.getDataSize())
            {
                DataUnit du = _page.GetDataUnit(count);

                if (du is TagDataUnit)
                {
                    Tag nextTag = ((TagDataUnit)du).Tag;
                    if (String.Compare(tag.Name, nextTag.Name, true) == 0)
                    {
                        if (nextTag.TagType == Tag.Type.End)
                        {
                            if (depth == 0)
                            {
                                return(count);
                            }
                            depth--;
                        }
                        else if (nextTag.TagType == Tag.Type.Begin)
                        {
                            depth++;
                        }
                    }
                }
                count++;
            }
            return(-1);
        }