public static string RemoveBaseHrel(string htmlsource) { var dom = Kooboo.Dom.DomParser.CreateDom(htmlsource); List <SourceUpdate> updates = new List <SourceUpdate>(); var basetags = dom.head.getElementsByTagName("base"); if (basetags != null && basetags.length > 0) { foreach (var item in basetags.item) { SourceUpdate update = new SourceUpdate(); update.StartIndex = item.location.openTokenStartIndex; update.EndIndex = item.location.openTokenEndIndex; update.NewValue = ""; updates.Add(update); } } if (updates.Count() > 0) { return(Service.DomService.UpdateSource(htmlsource, updates)); } else { return(htmlsource); } }
private static List <SourceUpdate> GetElementUpdate(Document doc, Element element, List <InlineSourceUpdate> KoobooIdUpdates) { List <SourceUpdate> sourceupdates = new List <SourceUpdate>(); bool HasAttChange = false; foreach (var item in KoobooIdUpdates) { if (item.IsDelete) { int start = element.location.openTokenStartIndex; int end = element.location.endTokenEndIndex; if (end < start) { end = -1; } sourceupdates.Add(new SourceUpdate() { StartIndex = start, EndIndex = end, NewValue = "" }); } else { if (string.IsNullOrEmpty(item.AttributeName)) { int start = element.location.openTokenEndIndex + 1; int end = element.location.endTokenStartIndex - 1; if (end < start) { end = -1; } sourceupdates.Add(new SourceUpdate() { StartIndex = start, EndIndex = end, NewValue = item.Value }); } else { HasAttChange = true; element.setAttribute(item.AttributeName, item.Value); } } } if (HasAttChange) { SourceUpdate update = new SourceUpdate(); update.StartIndex = element.location.openTokenStartIndex; update.EndIndex = element.location.openTokenEndIndex; update.NewValue = Kooboo.Sites.Service.DomService.ReSerializeOpenTag(element); sourceupdates.Add(update); } return(sourceupdates); }
private void RaiseUISourceUpdate() { var task = new Task(() => { SourceUpdate?.Invoke(this, EventArgs.Empty); _previous_js_code = JSCode; }); task.RunSynchronously(_ui_context); }
internal virtual void DeleteDom(IRepository repo, RenderContext context, IDomObject domObject, Element element) { var sourceUpdate = new SourceUpdate { StartIndex = element.location.openTokenStartIndex, EndIndex = element.location.endTokenEndIndex, NewValue = string.Empty }; UpdateDomObject(repo, context, domObject, sourceUpdate); }
public static string SetBaseHref(string htmlsource, string baseurl) { string strbasetag = "<base href=\"" + baseurl + "\">"; var dom = Kooboo.Dom.DomParser.CreateDom(htmlsource); SourceUpdate update = new SourceUpdate(); update.NewValue = strbasetag; update.EndIndex = -1; if (DomService.HasHeadTag(dom)) { var TagBase = dom.head.getElementsByTagName("base"); if (TagBase != null && TagBase.item.Count() > 0) { var tagElement = TagBase.item[0]; string currentbase = tagElement.getAttribute("href"); if (!string.IsNullOrWhiteSpace(currentbase) && currentbase == strbasetag) { return(htmlsource); } update.StartIndex = tagElement.location.openTokenStartIndex; update.EndIndex = tagElement.location.endTokenEndIndex; } else { var afterheaderlocation = dom.head.location.openTokenEndIndex + 1; update.StartIndex = afterheaderlocation; } } else { var afterHtmlLocation = dom.documentElement.location.openTokenEndIndex + 1; update.StartIndex = afterHtmlLocation; update.NewValue = "<head>" + update.NewValue + "</head>"; } List <SourceUpdate> updates = new List <SourceUpdate>(); updates.Add(update); return(Service.DomService.UpdateSource(htmlsource, updates)); }
public static string UpdatePageChange(string PageHtmlSource, List <ConvertSourceUpdate> updates) { List <SourceUpdate> sourceupdates = new List <SourceUpdate>(); foreach (var one in updates) { SourceUpdate newupdate = new SourceUpdate(); newupdate.StartIndex = one.StartIndex; newupdate.EndIndex = one.EndIndex; newupdate.NewValue = one.NewValue; if (newupdate.StartIndex > 0 && newupdate.EndIndex > 0) { sourceupdates.Add(newupdate); } } if (!string.IsNullOrEmpty(PageHtmlSource)) { return(Service.DomService.UpdateSource(PageHtmlSource, sourceupdates)); } return(null); }
public static string UpdateInlineStyle(string HtmlSource, List <Kooboo.Sites.Models.InlineStyleChange> changes) { int currentindex = 0; int totallen = HtmlSource.Length; StringBuilder sb = new StringBuilder(); Document doc = Kooboo.Dom.DomParser.CreateDom(HtmlSource); List <SourceUpdate> sourceupdates = new List <SourceUpdate>(); foreach (var item in changes) { if (string.IsNullOrEmpty(item.KoobooId)) { continue; } var node = GetElementByKoobooId(doc, item.KoobooId); if (node == null) { continue; } var element = node as Element; if (element == null) { continue; } var declarationtext = element.getAttribute("style"); Kooboo.Dom.CSS.CSSDeclarationBlock declaration; if (string.IsNullOrEmpty(declarationtext)) { declaration = new Dom.CSS.CSSDeclarationBlock(); } else { declaration = Kooboo.Dom.CSS.CSSSerializer.deserializeDeclarationBlock(declarationtext); } foreach (var propertychange in item.PropertyValues) { if (string.IsNullOrEmpty(propertychange.Value)) { declaration.removeProperty(propertychange.Key); } else { declaration.setPropertyValue(propertychange.Key, propertychange.Value); } } var newtext = declaration.GenerateCssText(); if (string.IsNullOrEmpty(newtext)) { element.removeAttribute("style"); } else { element.setAttribute("style", newtext); } string elementstring = ReSerializeOpenTag(element); SourceUpdate update = new SourceUpdate(); update.StartIndex = element.location.openTokenStartIndex; update.EndIndex = element.location.openTokenEndIndex + 1; update.NewValue = Kooboo.Sites.Service.DomService.ReSerializeOpenTag(element); sourceupdates.Add(update); } // update to real html source. foreach (var item in sourceupdates.OrderBy(o => o.StartIndex)) { string currentString = doc.HtmlSource.Substring(currentindex, item.StartIndex - currentindex); if (!string.IsNullOrEmpty(currentString)) { sb.Append(currentString); } sb.Append(item.NewValue); currentindex = item.EndIndex; } if (currentindex < totallen - 1) { sb.Append(doc.HtmlSource.Substring(currentindex, totallen - currentindex)); } return(sb.ToString()); }
public static string UpdateSource(Document doc, Dictionary <string, string> Updates) { List <SourceUpdate> sourceupdates = new List <SourceUpdate>(); foreach (var item in Updates) { string koobooid = CleanKoobooId(item.Key); if (!string.IsNullOrEmpty(koobooid)) { var element = GetElementByKoobooId(doc, koobooid); if (element != null) { if (item.Value.StartsWith(SiteConstants.UpdateDomAttributePrefix)) { /// this is to change the element attribute. Element currentitem = element as Element; string AttributeJson = item.Value.Substring(SiteConstants.UpdateDomAttributePrefix.Length); Dictionary <string, string> attrs = Lib.Helper.JsonHelper.Deserialize <Dictionary <string, string> >(AttributeJson); foreach (var att in attrs) { // for safety reason. convert all duouble quote to single quote within the attribute value. string value = att.Value; if (!string.IsNullOrEmpty(value)) { value = value.Replace("\"\"", "\""); value = value.Replace("\"", "'"); } currentitem.setAttribute(att.Key, value); } SourceUpdate update = new SourceUpdate(); update.StartIndex = currentitem.location.openTokenStartIndex; update.EndIndex = currentitem.location.openTokenEndIndex; update.NewValue = Kooboo.Sites.Service.DomService.ReSerializeOpenTag(currentitem); sourceupdates.Add(update); } else { int start = element.location.openTokenEndIndex + 1; int end = element.location.endTokenStartIndex - 1; if (end < start) { end = -1; } sourceupdates.Add(new SourceUpdate() { StartIndex = start, EndIndex = end, NewValue = item.Value }); } } } } return(UpdateSource(doc.HtmlSource, sourceupdates)); }
public static string ConvertLayout(SiteDb SiteDb, Guid PageId, List <LayoutResult> Results) { var page = SiteDb.Pages.Get(PageId); if (page == null) { return(null); } var dom = Kooboo.Dom.DomParser.CreateDom(page.Body); var updates = new List <SourceUpdate>(); foreach (var item in Results) { if (item.KoobooIds == null || item.KoobooIds.Count == 0) { continue; } if (item.IsContainer) { var koobooid = item.KoobooIds[0]; if (!string.IsNullOrEmpty(koobooid)) { var node = Service.DomService.GetElementByKoobooId(dom, koobooid); if (node != null && node.nodeType == enumNodeType.ELEMENT) { var element = node as Element; element.setAttribute(ConstTALAttributes.placeholder, item.Name); string newtag = Service.DomService.ReSerializeElement(element, ""); updates.Add(new SourceUpdate { StartIndex = element.location.openTokenStartIndex, EndIndex = element.location.endTokenEndIndex, NewValue = newtag }); } } } else { var koobooids = item.KoobooIds; int startindex = int.MaxValue; int endindex = int.MinValue; foreach (var id in koobooids) { var node = Service.DomService.GetElementByKoobooId(dom, id); if (node != null && node.nodeType == enumNodeType.ELEMENT) { var element = node as Element; if (element.location.openTokenStartIndex < startindex) { startindex = element.location.openTokenStartIndex; } if (element.location.endTokenEndIndex > endindex) { endindex = element.location.endTokenEndIndex; } } } if (startindex < int.MaxValue && endindex > int.MinValue) { var layouttag = "<div " + ConstTALAttributes.placeholder + "='" + item.Name + "' " + ConstTALAttributes.omitTag + "></div>"; var update = new SourceUpdate(); update.StartIndex = startindex; update.EndIndex = endindex; update.NewValue = layouttag; updates.Add(update); } } } if (updates.Any()) { string layoutbody = ConvertManager.UpdateDomSource(dom, updates); var name = GetLayoutName(SiteDb); Layout layout = new Layout(); layout.Name = name; layout.Body = layoutbody; SiteDb.Layouts.AddOrUpdate(layout); } return(null); }
public string UpdateViewTemplate(string template, DataAddResponse dataResponse) { if (dataResponse == null || dataResponse.DateList == null || !dataResponse.DateList.Any()) { return(template); } List <SourceUpdate> sourceUpdate = new List <SourceUpdate>(); var doc = Kooboo.Dom.DomParser.CreateDom(template); var els = GetPossibleEls(doc); foreach (var el in els) { Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var att in el.attributes) { dict.Add(att.name, att.value); } Dictionary <string, string> updates = new Dictionary <string, string>(); foreach (var at in dict) { if (at.Key.ToLower() == "k-content" || at.Key.ToLower() == "k-replace") { var value = at.Value; foreach (var date in dataResponse.DateList) { if (value == date.Name || value.EndsWith("." + date.Name)) { var newvalue = "DateFormat(" + at.Value + ", '" + date.Format + "')"; updates.Add(at.Key, newvalue); } } } } if (updates.Count() > 0) { foreach (var item in updates) { dict[item.Key] = item.Value; } SourceUpdate supdate = new SourceUpdate(); supdate.StartIndex = el.location.openTokenStartIndex; supdate.EndIndex = el.location.openTokenEndIndex; string newopentag = Kooboo.Sites.Service.DomService.GenerateOpenTag(dict, el.tagName); supdate.NewValue = newopentag; sourceUpdate.Add(supdate); } } if (sourceUpdate.Any()) { return(Kooboo.Sites.Service.DomService.UpdateSource(template, sourceUpdate)); } else { return(template); } }
internal override void UpdateDomObject(IRepository repo, RenderContext context, IDomObject domObject, SourceUpdate sourceUpdate) { var newDom = Service.DomService.UpdateSource(_currentCultureDom, new List <SourceUpdate> { sourceUpdate }); ((HtmlBlock)domObject).SetValue(_culture, newDom); repo.AddOrUpdate(domObject, context.User.Id); }
internal virtual void UpdateDom(IRepository repo, RenderContext context, IDomObject domObject, Element element) { SourceUpdate sourceUpdate; if (!string.IsNullOrWhiteSpace(Attribute)) { element.setAttribute(Attribute, Value); sourceUpdate = new SourceUpdate { StartIndex = element.location.openTokenStartIndex, EndIndex = element.location.openTokenEndIndex, NewValue = Service.DomService.ReSerializeOpenTag(element) }; } else if (!string.IsNullOrWhiteSpace(Property)) { var style = element.getAttribute("style"); var cssDeclar = CSSSerializer.deserializeDeclarationBlock(style); var propertise = cssDeclar.item; var exist = propertise.Find(o => o.propertyname.ToLower() == Property.ToLower()); if (exist == null) { propertise.Add(new CSSDeclaration { important = Important, propertyname = Property, value = Value }); } else { exist.value = Value; exist.important = Important; propertise.Remove(exist); propertise.Add(exist); } cssDeclar.item = cssDeclar.item.Where(o => !string.IsNullOrWhiteSpace(o.propertyname) && !string.IsNullOrWhiteSpace(o.value)).ToList(); var newStyle = CSSSerializer.serializeDeclarationBlock(cssDeclar); element.setAttribute("style", newStyle); sourceUpdate = new SourceUpdate { StartIndex = element.location.openTokenStartIndex, EndIndex = element.location.openTokenEndIndex, NewValue = Service.DomService.ReSerializeOpenTag(element) }; } else { sourceUpdate = new SourceUpdate { StartIndex = element.location.openTokenEndIndex + 1, EndIndex = element.location.endTokenStartIndex - 1, NewValue = Value }; } UpdateDomObject(repo, context, domObject, sourceUpdate); }
internal virtual void UpdateDomObject(IRepository repo, RenderContext context, IDomObject domObject, SourceUpdate sourceUpdate) { var newDom = Service.DomService.UpdateSource(domObject.Body, new List <SourceUpdate> { sourceUpdate }); domObject.Body = newDom; repo.AddOrUpdate(domObject, context.User.Id); }