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())); }
/// <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); }
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); }
public static IHtml ControlGroup(IHtml label, IHtml input, IHtml help) { return "div.control-group".Add( label, "div.controls".Add( input, help)); }
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()); } }
/// <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)); }
/// <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)); }
/// <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)); }
private void Init(IHtml textBox) { AddChild(textBox); LabelHtmlAttribute = new HtmlAttribute(); LabelHtmlAttribute.Append("class", GetLabelSize()); }
private void Init(IHtml textBox) { AddChild(textBox); }
public void AddControl(IHtml element) { controls.Add(element); }
public void AddTag(IHtml ihtml) { _childern.Add(ihtml); }
public static IHtml WithWarning(IHtml e) { return e.AddClass("warning"); }
public static IHtml WithSuccess(IHtml e) { return e.AddClass("success"); }
public static IHtml WithInfo(IHtml e) { return e.AddClass("info"); }
public static IHtml WithError(IHtml e) { return e.AddClass("error"); }
/// <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)); }
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(); }
/// <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)); }
public void AddControl(IHtml html) { controls.Add(html); }