public void HtmlTest()
        {
            HtmlAttribute attribute = new HtmlAttribute("attribute");
            StringAssert.Equals(attribute.Name, attribute.Html);
            StringAssert.Equals(attribute.Html, attribute.ToString());

            attribute.Value = "'value'";
            StringAssert.Equals(attribute.Html, "attribute=value");

            attribute.Value = "value";
            StringAssert.Equals(attribute.Html, attribute.ToString());
        }
        public void HtmlAttributeNameValueConstructorTest()
        {
            string name = "attribute";
            string value = "value";

            HtmlAttribute attribute = new HtmlAttribute(name, value);
            StringAssert.Equals(value, attribute.Value);

            attribute = new HtmlAttribute(name, "\"" + value);
            StringAssert.Equals(value, attribute.Value);

            attribute = new HtmlAttribute(name, "\"" + value + "\'");
            StringAssert.Equals(value, attribute.Value);

            attribute = new HtmlAttribute(name, "\'" + value + "\'");
            StringAssert.Equals(value, attribute.Value);
        }
Example #3
0
        private static HtmlNodeCollection BuildNodeCollection(Queue<string> tokens)
        {
            HtmlNodeCollection nodes = new HtmlNodeCollection(null);
            HtmlElement element = null;
            string current;

            while (tokens.Count > 0)
            {
                current = tokens.Dequeue();
                switch (current)
                {
                    case ("<"):
                        // Read open tag

                        if (tokens.Count == 0)
                            break;

                        current = tokens.Dequeue();
                        element = new HtmlElement(current);

                        // read the attributes and values
                        while (tokens.Count > 0 && (current = tokens.Dequeue()) != ">" && current != "/>")
                        {
                            string attribute_name = current;
                            if (tokens.Count > 0 && tokens.Peek() == "=")
                            {
                                tokens.Dequeue();
                                current = (tokens.Count > 0) ? tokens.Dequeue() : null;
                                HtmlAttribute attribute = new HtmlAttribute(attribute_name, HttpUtility.HtmlDecode(current));
                                element.Attributes.Add(attribute);
                            }
                            else //if (tokens.Count == 0)
                            {
                                // Null-attributeValue attribute
                                HtmlAttribute attribute = new HtmlAttribute(attribute_name);
                                element.Attributes.Add(attribute);
                            }
                        }
                        nodes.Add(element);

                        if (current == "/>")
                        {
                            element.IsTerminated = true;
                            element = null; //could not have any sub elements
                        }
                        else if (current == ">")
                        {
                            continue;
                        }
                        break;
                    case (">"):
                        continue;
                    case ("</"):
                        // Read close tag

                        if (tokens.Count == 0)
                            break;

                        current = tokens.Dequeue();

                        int open_index = FindTagOpenNodeIndex(nodes, current);
                        if (open_index != -1)
                        {
                            MoveNodesDown(ref nodes, open_index + 1, (HtmlElement)nodes[open_index]);
                        }

                        // Skip to the end of this tag
                        while (tokens.Count > 0 && (current = tokens.Dequeue()) != ">")
                        {
                            //shouldn't happen
                        }
                        element = null;
                        break;
                    default:
                        HtmlText node = new HtmlText(current);
                        nodes.Add(node);
                        break;
                }
            }
            return nodes;
        }
        public void NameTest()
        {
            string name = "attribute";
            HtmlAttribute target = new HtmlAttribute(name);

            StringAssert.Equals(target.Name, name);
        }
        public void HtmlAttributeNameConstructorTest()
        {
            HtmlAttribute attribute = new HtmlAttribute(null);

            //positive test case covered by other tests
        }
        public void ValueTest()
        {
            HtmlAttribute target = new HtmlAttribute("attribute", "value");
            target.Value = string.Empty;

            Assert.IsNull(target.Value);
        }
        public void parseStartTag(string tag, string tagName, string rest, string unaryStr)
        {
            if (HtmlTags.Block.Contains(tagName))
            {
                while (false == string.IsNullOrWhiteSpace(stack.SafePeek()) && HtmlTags.Inline.Contains(stack.SafePeek()))
                {
                    parseEndTag("", stack.Peek());
                }
            }

            if (HtmlTags.SelfClosing.Contains(tagName) && stack.SafePeek() == tagName)
            {
                parseEndTag("", tagName);
            }

            bool parsedUnary;
            bool.TryParse(unaryStr, out parsedUnary);
            var unary = HtmlTags.Empty.Contains(tagName) || parsedUnary;

            if (!unary)
            {
                stack.Push(tagName);
            }

            var attrs = new Dictionary<string, HtmlAttribute>();

            var matches = RegularExpressions.Attribute.Matches(rest);

            for (int i = 0; i < matches.Count; i++)
            {
                var match = matches[i];

                if (match.Success)
                {
                    var attributeName = match.Groups[1].Value;
                    string value;

                    if (false == string.IsNullOrWhiteSpace(match.Groups[2].Value))
                    {
                        value = match.Groups[2].Value;
                    }
                    else if (false == string.IsNullOrWhiteSpace(match.Groups[3].Value))
                    {
                        value = match.Groups[3].Value;
                    }
                    else if (false == string.IsNullOrWhiteSpace(match.Groups[4].Value))
                    {
                        value = match.Groups[4].Value;
                    }
                    else if (HtmlTags.FillAttrs.Contains(tagName))
                    {
                        value = tagName;
                    }
                    else
                    {
                        value = string.Empty;
                    }

                    var htmlAttribute = new HtmlAttribute
                    {
                        Name = attributeName,
                        Value = value,
                    };

                    attrs.Add(attributeName, htmlAttribute);
                }
            }

            start(tagName, attrs, unary);
        }
        internal HtmlAttributeCollection CreateTestCollection()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlAttributeCollection target = new HtmlAttributeCollection(root);

            HtmlAttribute attribute = new HtmlAttribute("first");
            target.Add(attribute);
            attribute = new HtmlAttribute("second", "value");
            target.Add(attribute);
            attribute = new HtmlAttribute("third", "\"another value\"");
            target.Add(attribute);

            return target;
        }