예제 #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()));
        }
예제 #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);
        }
예제 #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);
        }
예제 #4
0
 public static IHtml ControlGroup(IHtml label, IHtml input, IHtml help)
 {
     return
     "div.control-group".Add(
         label,
         "div.controls".Add(
             input,
             help));
 }
예제 #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());
     }
 }
예제 #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));
    }
예제 #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));
        }
예제 #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));
        }
예제 #10
0
 private void Init(IHtml textBox)
 {
     AddChild(textBox);
     LabelHtmlAttribute = new HtmlAttribute();
     LabelHtmlAttribute.Append("class", GetLabelSize());
 }
예제 #11
0
 private void Init(IHtml textBox)
 {
     AddChild(textBox);
 }
예제 #12
0
 public void AddControl(IHtml element)
 {
     controls.Add(element);
 }
예제 #13
0
 public void AddTag(IHtml ihtml)
 {
     _childern.Add(ihtml);
 }
예제 #14
0
 public static IHtml WithWarning(IHtml e)
 {
     return e.AddClass("warning");
 }
예제 #15
0
 public static IHtml WithSuccess(IHtml e)
 {
     return e.AddClass("success");
 }
예제 #16
0
 public static IHtml WithInfo(IHtml e)
 {
     return e.AddClass("info");
 }
예제 #17
0
 public static IHtml WithError(IHtml e)
 {
     return e.AddClass("error");
 }
예제 #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));
 }
예제 #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();
        }
예제 #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));
        }
예제 #21
0
 public void AddControl(IHtml html)
 {
     controls.Add(html);
 }