コード例 #1
0
        public override IHtmlElement Create(HtmlAttributesCollection attributes, string html, int startContentIndex, int endContentIndex, IHtmlParserManager htmlParserManager)
        {
            HtmlNodeElement element = (HtmlNodeElement)CreateInstance();

            // add children and texts
            if (endContentIndex - startContentIndex > 0)
            {
                element.Children = htmlParserManager.ParseElements(startContentIndex, endContentIndex);
                Dictionary <int, string> texts = htmlParserManager.ParseText(startContentIndex, endContentIndex);
                foreach (var text in texts)
                {
                    string decodedText = HttpUtility.HtmlDecode(text.Value);
                    string value       = text.Value;
                    if (Regex.IsMatch(decodedText, HtmlRegexParserManager.commentRegex))
                    {
                        value = decodedText;
                    }

                    if (text.Key >= 0)
                    {
                        element.Text(new HtmlString(value), element.Children[text.Key].UId);
                    }
                    else
                    {
                        element.Text(new HtmlString(value));
                    }
                }
            }

            element.Attributes = attributes;

            return(element);
        }
コード例 #2
0
        public void Create_ReturnTagAsHtmlString_WhenTagHaveTextBetweenChildren()
        {
            // Arrange
            htmlBuilder = new HtmlBuilder();

            HtmlNodeElement htmlTag = new HtmlNodeElement("div");
            HtmlNodeElement span    = new HtmlNodeElement("span");

            htmlTag.Children.Add(span);
            htmlTag.Text("Text", span.UId);
            HtmlNodeElement span1 = new HtmlNodeElement("span");

            htmlTag.Children.Add(span1);
            htmlTag.Text("Text1", span1.UId);
            HtmlNodeElement span2 = new HtmlNodeElement("span");

            htmlTag.Children.Add(span2);
            htmlTag.Text("Text2", span2.UId);

            // Act
            IHtmlContent result = htmlBuilder.CreateHtmlContent(htmlTag);

            // Assert
            Assert.Equal("<div><span></span>Text<span></span>Text1<span></span>Text2</div>", result.ToString());
        }
コード例 #3
0
        private HtmlNodeElement GeRadioButtonsGroup(string name, IEnumerable <SelectListItem> values, string value, bool isMultiple = false)
        {
            HtmlNodeElement wrapper = new HtmlNodeElement("div");

            if (_options.RadioButtonsWrapperAttributes != null)
            {
                wrapper.AddRangeAttributes(_options.RadioButtonsWrapperAttributes);
            }

            foreach (var item in values)
            {
                HtmlNodeElement label = new HtmlNodeElement("label");
                label.Text(item.Text);

                if (_options.RadioButtonsGroupLabelAttributes != null)
                {
                    label.AddRangeAttributes(_options.RadioButtonsGroupLabelAttributes);
                }

                var radioInput = new HtmlSelfClosingTagElement("input")
                {
                    Attributes = new IHtmlAttribute[]
                    {
                        new HtmlAttribute("name", name),
                        new HtmlAttribute("value", item.Value),
                        new HtmlAttribute("type", isMultiple ? "checkbox" : "radio")
                    }
                };

                if (_options.RadioButtonsGroupInputAttributes != null)
                {
                    radioInput.AddRangeAttributes(_options.RadioButtonsGroupInputAttributes);
                }

                if (item.Selected || (value != null && item.Value == value.ToString()))
                {
                    radioInput.AddAttribute(new HtmlAttribute("checked"));
                }

                label.Append(radioInput);

                var div = new HtmlNodeElement("div");
                div.Append(label);

                if (_options.RadioButtonsGroupWrapperAttributes != null)
                {
                    div.AddRangeAttributes(_options.RadioButtonsGroupWrapperAttributes);
                }

                wrapper.Append(div);
            }

            return(wrapper);
        }
コード例 #4
0
        public void Create_ReturnTagAsHtmlString_WhenTagHaveTextAfterChildren()
        {
            // Arrange
            htmlBuilder = new HtmlBuilder();

            HtmlNodeElement htmlTag = new HtmlNodeElement("div");

            htmlTag.Children.Add(new HtmlSpanElement());
            htmlTag.Text("Text", htmlTag.Children.LastOrDefault().UId);

            // Act
            IHtmlContent result = htmlBuilder.CreateHtmlContent(htmlTag);

            // Assert
            Assert.Equal("<div><span></span>Text</div>", result.ToString());
        }
コード例 #5
0
        /// <summary>
        /// Create html content.
        /// </summary>
        /// <param name="htmlElement">Object of type HtmlNodeElement, for convert to standart HTML document content.</param>
        /// <returns>Html content repsresent specified node element.</returns>
        public IHtmlContent CreateHtmlContent(HtmlNodeElement htmlElement)
        {
            if (htmlElement.Children.IsNullOrEmpty())
            {
                return(new HtmlString(
                           $"{CreateStartTag(htmlElement.TagName)}{htmlElement.Text()}{CreateEndTag(htmlElement.TagName)}"
                           .Insert((htmlElement.TagName.Length + 1), htmlElement.Attributes.IsNullOrEmpty() ? "" : $" {String.Join(" ", htmlElement.Attributes.Select(x => CreateAtribute(x)))}")));
            }

            List <IHtmlContent>        childContents         = new List <IHtmlContent>();
            List <HtmlNodeElementText> knownPossitionedTexts = new List <HtmlNodeElementText>();

            foreach (var text in htmlElement.Texts())
            {
                if (text.AfterElementUId == null || !htmlElement.Children.Any(x => x.UId == text.AfterElementUId))
                {
                    childContents.Add(text.HtmlString);
                }
                else
                {
                    knownPossitionedTexts.Add(text);
                }
            }

            foreach (var child in htmlElement.Children)
            {
                childContents.Add(CreateHtmlContent(child));
                if (knownPossitionedTexts.Any(x => x.AfterElementUId == child.UId))
                {
                    childContents.Add(knownPossitionedTexts.FirstOrDefault(x => x.AfterElementUId == child.UId).HtmlString);
                }
            }

            return(new HtmlString(
                       $"{CreateStartTag(htmlElement.TagName)}{String.Join("", childContents.Select(x => x.ToString()).ToArray())}{CreateEndTag(htmlElement.TagName)}"
                       .Insert((htmlElement.TagName.Length + 1), htmlElement.Attributes.IsNullOrEmpty() ? "" : $" {String.Join(" ", htmlElement.Attributes.Select(x => CreateAtribute(x)))}")));
        }