Example #1
0
        public MvcHtmlString Render(IHtml element)
        {
            var sb = new StringBuilder();

            sb.Append($"<{element.Tag}");

            foreach (var attribute in element.Attributes)
            {
                sb.Append($" {attribute.Key}=\"{attribute.Value.ToAttribute()}\"");
            }

            if (element.TagOmission != TagOmission.SelfClosing)
            {
                sb.Append(">");

                foreach (var elem in element.ChildElements)
                {
                    sb.Append(elem.Render());
                }

                sb.Append($"</{element.Tag}>");
            }
            else
            {
                sb.Append("/>");
            }

            return(new MvcHtmlString(sb.ToString()));
        }
Example #2
0
        /// <summary>
        /// 检查Html标签闭合
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool CheckTags(this IHtml c)
        {
            var list = RegexHelper.Matches(c.GetValue(), "(<[^>]*[^/]>)");

            if (list.Count % 2 != 0)
            {
                return(false);
            }
            var un = new List <string>();

            foreach (var i in list)
            {
                var tag = i.As <IRegex>().Match("<([a-z0-9A-Z]+)[^>]*>", 1);
                if (tag.IsNotNullOrEmpty())
                {
                    un.Add(tag);
                }
                else
                {
                    tag = i.As <IRegex>().Match("</([a-z0-9A-Z]+)>", 1);
                    if (tag.IsNotNullOrEmpty())
                    {
                        if (un[un.Count - 1] != tag)
                        {
                            return(false);
                        }
                        un.RemoveAt(un.Count - 1);
                    }
                }
            }
            return(un.Count == 0);
        }
Example #3
0
        private IHtml InjectAttributes(IHtml html)
        {
            var fieldsOfHtmlPage = html.GetType()
                                   .GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            var fieldsOfHtmlManager = this.GetType()
                                      .GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var fieldOfHtmlPage in fieldsOfHtmlPage)
            {
                Attribute attribute = fieldOfHtmlPage.GetCustomAttribute(typeof(InjectAttribute));

                if (attribute == null)
                {
                    continue;
                }

                var fieldHtmlManager = fieldsOfHtmlManager
                                       .FirstOrDefault(f => f.FieldType == fieldOfHtmlPage.FieldType);
                if (fieldHtmlManager != null)
                {
                    fieldOfHtmlPage.SetValue(html, fieldHtmlManager.GetValue(this));
                }
            }

            return(html);
        }
Example #4
0
 public static IHtml ControlGroup(IHtml label, IHtml input, IHtml help)
 {
     return
     "div.control-group".Add(
         label,
         "div.controls".Add(
             input,
             help));
 }
Example #5
0
 public static string GetHtml(this IHtml c, Encoding encoding, string method = null, string param = null)
 {
     if (method.IsNullOrEmpty())
     {
         method = "Get";
     }
     using (var http = new HttpHelper(c.GetValue(), method, encoding, param))
     {
         return(http.GetHtml());
     }
 }
Example #6
0
        /// <summary>
        /// 清空Html标签
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string ClearHtml(this IHtml c)
        {
            var html = c.GetValue();

            if (html.IsNullOrEmpty())
            {
                return(string.Empty);
            }
            html = HttpUtility.HtmlDecode(html);
            return(html.IsNullOrEmpty() ? string.Empty : RegexHelper.ClearHtml(html));
        }
    public static IElement Create(IHtml dom)
    {
        switch (dom.ElementType)
        {
        case "table":
            return(new TableElement(dom));

        case "div":
            return(new DivElement(dom));

        case "span":
            return(new SpanElement(dom));
        }
        return(new PassthroughElement(dom));
    }
Example #8
0
        /// <summary>
        /// 获取Attr
        /// </summary>
        /// <param name="c"></param>
        /// <param name="attr"></param>
        /// <returns></returns>
        public static IEnumerable <string> GetHtmlByAttr(this IHtml c, string attr)
        {
            var html = c.GetValue();

            return(RegexHelper.FindByAttr(html, attr));
        }
Example #9
0
        /// <summary>
        /// 获取CSS
        /// </summary>
        /// <param name="c"></param>
        /// <param name="css"></param>
        /// <returns></returns>
        public static IEnumerable <string> GetHtmlByCss(this IHtml c, string css)
        {
            var html = c.GetValue();

            return(RegexHelper.FindByCss(html, css));
        }
Example #10
0
 private void Init(IHtml textBox)
 {
     AddChild(textBox);
     LabelHtmlAttribute = new HtmlAttribute();
     LabelHtmlAttribute.Append("class", GetLabelSize());
 }
Example #11
0
 private void Init(IHtml textBox)
 {
     AddChild(textBox);
 }
Example #12
0
 public void AddControl(IHtml element)
 {
     controls.Add(element);
 }
Example #13
0
 public void AddTag(IHtml ihtml)
 {
     _childern.Add(ihtml);
 }
Example #14
0
 public static IHtml WithWarning(IHtml e)
 {
     return e.AddClass("warning");
 }
Example #15
0
 public static IHtml WithSuccess(IHtml e)
 {
     return e.AddClass("success");
 }
Example #16
0
 public static IHtml WithInfo(IHtml e)
 {
     return e.AddClass("info");
 }
Example #17
0
 public static IHtml WithError(IHtml e)
 {
     return e.AddClass("error");
 }
Example #18
0
 /// <summary>
 /// 获取网页源码
 /// </summary>
 /// <param name="c"></param>
 /// <param name="method"></param>
 /// <param name="param"></param>
 /// <returns></returns>
 public static string GetHtml(this IHtml c, string method = null, string param = null)
 {
     return(c.GetHtml(Encoding.UTF8, method, param));
 }
Example #19
0
        public static void Main()
        {
            ISharpStoreContext     context         = new SharpStoreContext();
            IHtmlProvider          htmlProvider    = new HtmlProvider(context);
            ICustomServiceProvider serviceProvider = new ServiceProvider(context);

            var routes = new List <Route>()
            {
                new Route()
                {
                    Name     = "Html pages",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^\\/.+\\.html.*",
                    Callable = (request) =>
                    {
                        string pageName = GetPageNameByUrl(request);
                        IHtml  html     = htmlProvider.GetHtmlPage(pageName);

                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = html?.Print(request.Url)
                        };
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Html pages Post",
                    Method   = RequestMethod.POST,
                    UrlRegex = "^\\/.+\\.html$",
                    Callable = (request) =>
                    {
                        string pageName = GetPageNameByUrl(request);
                        IHtml  html     = htmlProvider.GetHtmlPage(pageName);

                        serviceProvider.ExecuteRequest(request);

                        var response = new HttpResponse()
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = html.Print(request.Url)
                        };
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Carousel CSS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/content/css/carousel.css$",
                    Callable = (request) =>
                    {
                        var response = new HttpResponse
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = File.ReadAllText("../../content/css/carousel.css"),
                            Header        = { ContentType = "text/css" }
                        };
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Products CSS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/content/productsTest.css$",
                    Callable = (request) =>
                    {
                        var response = new HttpResponse
                        {
                            StatusCode    = ResponseStatusCode.Ok,
                            ContentAsUtf8 = File.ReadAllText("../../content/products.css"),
                            Header        = { ContentType = "text/css" }
                        };
                        return(response);
                    }
                },
                new Route()
                {
                    Name     = "Bootstrap JS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/bootstrap/js/bootstrap.min.js$",
                    Callable = (request) => new HttpResponse
                    {
                        StatusCode    = ResponseStatusCode.Ok,
                        ContentAsUtf8 = File.ReadAllText("../../content/bootstrap/js/bootstrap.min.js"),
                        Header        = { ContentType = "application/x-javascript" }
                    }
                },
                new Route()
                {
                    Name     = "Bootstrap CSS",
                    Method   = RequestMethod.GET,
                    UrlRegex = "^/bootstrap/css/bootstrap.min.css$",
                    Callable = request => new HttpResponse()
                    {
                        StatusCode    = ResponseStatusCode.Ok,
                        ContentAsUtf8 = File.ReadAllText("../../content/bootstrap/css/bootstrap.min.css"),
                        Header        = { ContentType = "text/css" }
                    }
                }
            };

            HttpServer server = new HttpServer(8081, routes);

            server.Listen();
        }
Example #20
0
        /// <summary>
        /// 获取ID
        /// </summary>
        /// <param name="c"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetHtmlById(this IHtml c, string id)
        {
            var html = c.GetValue();

            return(RegexHelper.FindById(html, id));
        }
Example #21
0
 public void AddControl(IHtml html)
 {
     controls.Add(html);
 }