Ejemplo n.º 1
0
        public void GetStringContent_WithInlineEditingAttribute_TextElementProperlyCreated()
        {
            // Arrange
            var htmlProcessor    = new HtmlProcessor();
            var dummyWidgetModel = new DummyWidgetModel {
                EditableContent = this.dummyContent, NonEditableContent = this.dummyContent
            };

            var fieldName = "DummyWidget";
            var type      = "LongText";

            // Act
            string inlineeditingAwareContent = htmlProcessor.GetStringContent(dummyWidgetModel, "EditableContent");

            // Assert
            using (var parser = new HtmlParser(inlineeditingAwareContent))
            {
                HtmlChunk chunk = parser.ParseNext();
                Assert.IsNotNull(chunk);

                // checks if the HTML tag is of type div and if it has the required attributes
                Assert.IsTrue(chunk.TagName.Equals(this.htmlWrapperTag, StringComparison.Ordinal), "There is no wrapper div appended to the property representation.");
                Assert.IsTrue(chunk.HasAttribute(this.fieldAttribute), "The field attribute is not appended correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.fieldTypeAttribute), "The field type attribute is not appended correctly.");

                // checks if the required attributes has proper values assigned to them
                Assert.AreEqual(fieldName, chunk.GetParamValue(this.fieldAttribute), "The value of the field attribute is not correct.");
                Assert.AreEqual(type, chunk.GetParamValue(this.fieldTypeAttribute), "The value of the fieldType attribute is not correct.");

                this.AssertContentAndCloseTag(parser);
            }
        }
Ejemplo n.º 2
0
        public void CreateInlineEditingRegion_DummyContent_IsWrappedIntoInlineEditingRegion()
        {
            // Arrange: create dummy data which will be set to the related attributes inside the region div tag
            TextWriter writer       = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
            var        providerName = "dummyProvider";
            var        type         = "dummyType";
            Guid       id           = Guid.NewGuid();

            // Act: create the CreateInlineEditingRegion
            var htmlProcessor = new HtmlProcessor();

            using (htmlProcessor.CreateInlineEditingRegion(writer, providerName, type, id))
            {
                writer.WriteLine(this.dummyContent);
            }

            string outPut = writer.ToString();

            // Assert: Parses the generated by the htmlTransformationProxy HTML checks if the HTML content is properly wrapped into a div tag
            // which has the required by the InlineEditing attributes
            // and these attributes has a proper data assigned
            using (var parser = new HtmlParser(outPut))
            {
                HtmlChunk chunk = parser.ParseNext();
                Assert.IsNotNull(chunk);

                // checks if the HTML tag is of type div and if it has the required attributes
                Assert.IsTrue(chunk.TagName.Equals(this.htmlWrapperTag, StringComparison.Ordinal));
                Assert.IsTrue(chunk.HasAttribute(this.idAttribute), "The id of the item is not appended as attribute correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.providerAttribute), "The provider is not appended as attribute correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.typeAttribute), "The id type the item is not appended as attribute correctly.");

                // checks if the required attributes has proper values assigned to them
                Assert.AreEqual(providerName, chunk.GetParamValue(this.providerAttribute), "The value of the provider attribute is not correct.");
                Assert.AreEqual(type, chunk.GetParamValue(this.typeAttribute), "The value of the provider attribute is not correct.");
                Assert.AreEqual(id.ToString(), chunk.GetParamValue(this.idAttribute), "The value of the id attribute is not correct.");

                this.AssertContentAndCloseTag(parser);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes the layout string adding the required attributes to the head tag
        /// and also adding the required form tag.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <returns></returns>
        public virtual string ProcessLayoutString(string template)
        {
            var           includeFormTag = this.IsFormTagRequired();
            StringBuilder outPut         = new StringBuilder();
            HtmlChunk     chunk          = null;

            using (HtmlParser parser = new HtmlParser(template))
            {
                parser.SetChunkHashMode(false);
                parser.AutoExtractBetweenTagsOnly  = false;
                parser.CompressWhiteSpaceBeforeTag = false;
                parser.KeepRawHTML = true;
                bool setTitle = true;
                bool modified;
                bool isOpenBodyTag;
                bool isCloseBodyTag;
                bool isClosedHeadTag;

                while ((chunk = parser.ParseNext()) != null)
                {
                    modified        = false;
                    isOpenBodyTag   = false;
                    isCloseBodyTag  = false;
                    isClosedHeadTag = false;

                    if (chunk.Type == HtmlChunkType.OpenTag)
                    {
                        if (chunk.TagName.Equals("head", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!chunk.HasAttribute("runat"))
                            {
                                chunk.SetAttribute("runat", "server");
                                modified = true;
                            }
                        }
                        else if (chunk.TagName.Equals("body", StringComparison.OrdinalIgnoreCase))
                        {
                            isOpenBodyTag = true;
                        }
                        else if (chunk.TagName.Equals("title", StringComparison.OrdinalIgnoreCase))
                        {
                            setTitle = false;
                        }
                    }
                    else if (chunk.Type == HtmlChunkType.CloseTag)
                    {
                        if (chunk.TagName.Equals("body", StringComparison.OrdinalIgnoreCase))
                        {
                            isCloseBodyTag = true;
                        }

                        if (chunk.TagName.Equals("head", StringComparison.OrdinalIgnoreCase))
                        {
                            isClosedHeadTag = true;
                        }
                    }

                    if (includeFormTag && isCloseBodyTag)
                    {
                        outPut.Append("</form>");
                    }
                    else if (!includeFormTag && isClosedHeadTag)
                    {
                        this.AppendRequiredHeaderContent(outPut, setTitle);
                    }

                    if (modified)
                    {
                        outPut.Append(chunk.GenerateHtml());
                    }
                    else
                    {
                        outPut.Append(chunk.Html);
                    }

                    if (includeFormTag && isOpenBodyTag)
                    {
                        outPut.Append("<form runat='server'>");
                    }
                }
            }

            return(outPut.ToString());
        }
Ejemplo n.º 4
0
        private void HandleOpenTag(Stack <Control> container, HtmlChunk chunk, StringBuilder currentLiteralText)
        {
            if (chunk.TagName.Equals("title", StringComparison.OrdinalIgnoreCase) && isCurrentlyInHeadTag)
            {
                // if we are not currently parsing the <head> tag, then this title must be in the body and we should skip it
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                var title = new HtmlTitle();
                container.Peek().Controls.Add(title);
                container.Push(title);
            }
            else if (chunk.TagName.Equals("head", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();
                var head = new HtmlHead();
                container.Peek().Controls.Add(head);
                container.Push(head);
                isCurrentlyInHeadTag = true;
            }
            else if (chunk.TagName.Equals("asp:ContentPlaceHolder", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                if (chunk.HasAttribute("ID"))
                {
                    var id          = chunk.AttributesMap["ID"] as string;
                    var placeHolder = new ContentPlaceHolder()
                    {
                        ID = id
                    };
                    container.Peek().Controls.Add(placeHolder);
                    this.AddContentPlaceHolder(id);
                    this.InstantiateControls(placeHolder);
                }
            }
            else if (chunk.TagName.Equals("form", StringComparison.OrdinalIgnoreCase) && chunk.HasAttribute("runat"))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                var form = new HtmlForm();
                if (chunk.HasAttribute("id"))
                {
                    form.ID = chunk.AttributesMap["id"] as string;
                }
                else
                {
                    form.ID = "aspnetForm";
                }

                container.Peek().Controls.Add(form);
                container.Push(form);
            }
            else if (chunk.TagName.Equals(LayoutsHelpers.SectionTag, StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                var sectionRenderer = new SectionRenderer();
                if (chunk.HasAttribute("name"))
                {
                    sectionRenderer.Name = chunk.AttributesMap["name"].ToString();
                }

                container.Peek().Controls.Add(sectionRenderer);
            }
            else if (chunk.TagName.Equals("meta", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                this.AddIfNotEmpty(chunk.Html, container.Peek());
            }
            else if (chunk.TagName == "%@")
            {
                //// Ignore
            }
            else
            {
                currentLiteralText.Append(chunk.Html);
            }
        }
Ejemplo n.º 5
0
        private void HandleOpenTag(Stack<Control> container, HtmlChunk chunk, StringBuilder currentLiteralText)
        {
            if (chunk.TagName.Equals("title", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                var title = new HtmlTitle();
                container.Peek().Controls.Add(title);
                container.Push(title);
            }
            else if (chunk.TagName.Equals("head", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();
                var head = new HtmlHead();
                container.Peek().Controls.Add(head);
                container.Push(head);
            }
            else if (chunk.TagName.Equals("asp:ContentPlaceHolder", StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                if (chunk.HasAttribute("ID"))
                {
                    var id = chunk.AttributesMap["ID"] as string;
                    var placeHolder = new ContentPlaceHolder() { ID = id };
                    container.Peek().Controls.Add(placeHolder);
                    this.AddContentPlaceHolder(id);
                    this.InstantiateControls(placeHolder);
                }
            }
            else if (chunk.TagName.Equals("form", StringComparison.OrdinalIgnoreCase) && chunk.HasAttribute("runat"))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                var form = new HtmlForm();
                if (chunk.HasAttribute("id"))
                    form.ID = chunk.AttributesMap["id"] as string;
                else
                    form.ID = "aspnetForm";

                container.Peek().Controls.Add(form);
                container.Push(form);
            }
            else if (chunk.TagName.Equals(LayoutsHelpers.SectionTag, StringComparison.OrdinalIgnoreCase))
            {
                this.AddIfNotEmpty(currentLiteralText.ToString(), container.Peek());
                currentLiteralText.Clear();

                var sectionRenderer = new SectionRenderer();
                if (chunk.HasAttribute("name"))
                {
                    sectionRenderer.Name = chunk.AttributesMap["name"].ToString();
                }

                container.Peek().Controls.Add(sectionRenderer);
            }
            else if (chunk.TagName == "%@")
            {
                //// Ignore
            }
            else
            {
                currentLiteralText.Append(chunk.Html);
            }
        }