Example #1
0
        private void CheckAndAddChange(List <SourceUpdate> updates, Dom.Element item, string itemsrc, string ObjectRelativeUrl)
        {
            if (string.IsNullOrEmpty(itemsrc))
            {
                return;
            }

            if (DomUrlService.IsExternalLink(itemsrc))
            {
                return;
            }

            string RelativeUrl = Kooboo.Lib.Helper.UrlHelper.Combine(ObjectRelativeUrl, itemsrc);

            if (RelativeUrl != null)
            {
                RelativeUrl = System.Net.WebUtility.UrlDecode(RelativeUrl);
            }

            if (itemsrc != RelativeUrl)
            {
                string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item);
                string newstring = oldstring.Replace(itemsrc, RelativeUrl);

                updates.Add(new SourceUpdate()
                {
                    StartIndex = item.location.openTokenStartIndex,
                    EndIndex   = item.location.openTokenEndIndex,
                    NewValue   = newstring
                });
            }
        }
Example #2
0
        public static List <string> ComputeReferenceScript(Document doc, SiteDb sitedb, Guid objectId, byte constType, string baseUrl = "", HtmlHeader header = null)
        {
            List <string> UrlList = Service.DomUrlService.GetReferenceScripts(doc);

            if (header != null)
            {
                UrlList.AddRange(header.Scripts);
            }
            DomUrlService.MakeAllUrlRelative(UrlList, baseUrl);

            List <string> scripturls = new List <string>();
            List <string> groupurls  = new List <string>();

            foreach (var item in UrlList)
            {
                if (Kooboo.Sites.Service.GroupService.IsGroupUrl(item))
                {
                    groupurls.Add(item);
                }
                else
                {
                    scripturls.Add(item);
                }
            }

            ComputeUrlRelation(sitedb, objectId, constType, scripturls, ConstObjectType.Script);

            return(groupurls);
        }
Example #3
0
        public static void ComputeLinks(Document doc, SiteDb sitedb, Guid objectId, byte constType, string baseUrl = "")
        {
            List <string> UrlList = DomUrlService.GetLinks(doc);

            DomUrlService.MakeAllUrlRelative(UrlList, baseUrl);

            ComputeUrlRelation(sitedb, objectId, constType, UrlList, ConstObjectType.Link);
        }
Example #4
0
        /// <summary>
        /// Compute the image references, includes internal and external image references.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="sitedb"></param>
        public static void ComputeImage(Document doc, SiteDb sitedb, Guid objectId, byte constType, string baseUrl = "")
        {
            var imagelinks = Service.DomUrlService.GetImages(doc);

            DomUrlService.MakeAllUrlRelative(imagelinks, baseUrl);

            ComputeUrlRelation(sitedb, objectId, constType, imagelinks, ConstObjectType.Image);
        }
Example #5
0
        public static void ComputeEmbed(Document doc, SiteDb sitedb, Guid objectId, byte constType)
        {
            HTMLCollection embedElement = doc.getElementsByTagName("embed");

            List <string> UrlList = new List <string>();

            foreach (var item in embedElement.item)
            {
                string fileurl = DomUrlService.GetLinkOrSrc(item);

                if (string.IsNullOrEmpty(fileurl))
                {
                    continue;
                }
                UrlList.Add(fileurl);
            }
            ComputeUrlRelation(sitedb, objectId, constType, UrlList, ConstObjectType.File);
        }
Example #6
0
        private string CorrectHtmlLink(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(null);
            }

            var dom = Kooboo.Dom.DomParser.CreateDom(input);

            List <SourceUpdate> updates = new List <SourceUpdate>();

            var imgurls = Kooboo.Sites.Service.DomUrlService.GetImageSrcs(dom);

            foreach (var item in imgurls)
            {
                if (!string.IsNullOrEmpty(item.Value) && !Kooboo.Lib.Utilities.DataUriService.isDataUri(item.Value))
                {
                    if (DomUrlService.IsExternalLink(item.Value))
                    {
                        continue;
                    }

                    if (string.IsNullOrWhiteSpace(item.Value))
                    {
                        continue;
                    }

                    string RelativeUrl = AppendPath(item.Value);

                    if (item.Value != RelativeUrl)
                    {
                        string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item.Key);
                        string newstring = oldstring.Replace(item.Value, RelativeUrl);

                        updates.Add(new SourceUpdate()
                        {
                            StartIndex = item.Key.location.openTokenStartIndex,
                            EndIndex   = item.Key.location.openTokenEndIndex,
                            NewValue   = newstring
                        });
                    }
                }
            }


            // for external css link.
            var list = dom.getElementsByTagName("link");

            foreach (var item in list.item)
            {
                //  < link rel = "stylesheet" href = " " >
                var rel = item.getAttribute("rel");
                if (rel != null && rel.ToLower() == "stylesheet")
                {
                    var href = item.getAttribute("href");
                    if (string.IsNullOrWhiteSpace(href))
                    {
                        continue;
                    }
                    else
                    {
                        string RelativeUrl = AppendPath(href);
                        if (href != RelativeUrl)
                        {
                            string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item);
                            string newstring = oldstring.Replace(href, RelativeUrl);

                            updates.Add(new SourceUpdate()
                            {
                                StartIndex = item.location.openTokenStartIndex,
                                EndIndex   = item.location.openTokenEndIndex,
                                NewValue   = newstring
                            });
                        }
                    }
                }
            }


            var embeds = dom.getElementsByTagName("embed");

            //<embed src = "UpcE1I9h3kCZ1P2oBSKRpw_files/img_01.svg" type = "image/svg+xml" class="stl_03" />

            foreach (var item in embeds.item)
            {
                var type = item.getAttribute("type");
                if (type != null & type.ToLower().Contains("image"))
                {
                    var href = item.getAttribute("src");
                    if (string.IsNullOrWhiteSpace(href))
                    {
                        continue;
                    }
                    else
                    {
                        string RelativeUrl = AppendPath(href);
                        if (href != RelativeUrl)
                        {
                            string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item);
                            string newstring = oldstring.Replace(href, RelativeUrl);

                            updates.Add(new SourceUpdate()
                            {
                                StartIndex = item.location.openTokenStartIndex,
                                EndIndex   = item.location.openTokenEndIndex,
                                NewValue   = newstring
                            });
                        }
                    }
                }
            }


            if (updates.Any())
            {
                return(Kooboo.Sites.Service.DomService.UpdateSource(input, updates));
            }
            return(input);
        }
Example #7
0
        public string CorrectHtml(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(null);
            }

            var dom = Kooboo.Dom.DomParser.CreateDom(input);

            List <SourceUpdate> updates = new List <SourceUpdate>();

            var imgurls = Kooboo.Sites.Service.DomUrlService.GetImageSrcs(dom);

            foreach (var item in imgurls)
            {
                if (!string.IsNullOrEmpty(item.Value) && !Kooboo.Lib.Utilities.DataUriService.isDataUri(item.Value))
                {
                    if (DomUrlService.IsExternalLink(item.Value))
                    {
                        continue;
                    }

                    string RelativeUrl = Kooboo.Lib.Helper.UrlHelper.RelativePath(item.Value);


                    if (string.IsNullOrWhiteSpace(RelativeUrl))
                    {
                        continue;
                    }

                    // add converter folder.
                    /// RelativeUrl = Lib.Helper.UrlHelper.Combine("officeconverter", RelativeUrl);

                    if (RelativeUrl.StartsWith("/") || RelativeUrl.Contains("/"))
                    {
                        RelativeUrl = "/officeconverter" + RelativeUrl;
                    }
                    else if (RelativeUrl.StartsWith("\\") || RelativeUrl.Contains("\\"))
                    {
                        RelativeUrl = "\\officeconverter" + RelativeUrl;
                    }
                    else
                    {
                        RelativeUrl = "/officeconverter/" + RelativeUrl;
                    }


                    if (item.Value != RelativeUrl)
                    {
                        string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item.Key);
                        string newstring = oldstring.Replace(item.Value, RelativeUrl);

                        updates.Add(new SourceUpdate()
                        {
                            StartIndex = item.Key.location.openTokenStartIndex,
                            EndIndex   = item.Key.location.openTokenEndIndex,
                            NewValue   = newstring
                        });
                    }
                }
            }

            if (updates.Any())
            {
                return(Kooboo.Sites.Service.DomService.UpdateSource(input, updates));
            }
            return(input);
        }
        public void RemoveExternalStyleScript(T value)
        {
            if (value != null && !value.IsEmbedded)
            {
                var    route     = this.SiteDb.Routes.GetByObjectId(value.Id);
                string objecturl = route != null ? route.Name : null;

                if (objecturl == null)
                {
                    return; // not url, should not be possible.
                }

                var relations = this.SiteDb.Relations.GetReferredBy(value as SiteObject);

                foreach (var item in relations)
                {
                    var repo = this.SiteDb.GetRepository(item.ConstTypeX);

                    var parentobject = repo?.Get(item.objectXId);

                    if (parentobject != null && parentobject is IDomObject)
                    {
                        var domobject = parentobject as IDomObject;

                        if (domobject is Page)
                        {
                            var page = domobject as Page;
                            page.Headers.Styles.Remove(objecturl);
                            page.Headers.Scripts.Remove(objecturl);
                        }

                        List <SourceUpdate> updates = new List <SourceUpdate>();

                        if (value.ConstType == ConstObjectType.Style)
                        {
                            HTMLCollection styletags = domobject.Dom.getElementsByTagName("link");

                            foreach (var cssitem in styletags.item)
                            {
                                if (cssitem.hasAttribute("rel") && cssitem.getAttribute("rel").ToLower().Contains("stylesheet"))
                                {
                                    string itemurl = DomUrlService.GetLinkOrSrc(cssitem);

                                    if (Lib.Helper.StringHelper.IsSameValue(itemurl, objecturl))
                                    {
                                        updates.Add(new SourceUpdate()
                                        {
                                            StartIndex = cssitem.location.openTokenStartIndex, EndIndex = cssitem.location.endTokenEndIndex
                                        });
                                    }
                                }
                            }
                        }
                        else if (value.ConstType == ConstObjectType.Script)
                        {
                            HTMLCollection scripttags = domobject.Dom.getElementsByTagName("script");

                            foreach (var jsitem in scripttags.item)
                            {
                                if (jsitem.hasAttribute("src"))
                                {
                                    string itemurl = jsitem.getAttribute("src");

                                    if (Lib.Helper.StringHelper.IsSameValue(itemurl, objecturl))
                                    {
                                        updates.Add(new SourceUpdate()
                                        {
                                            StartIndex = jsitem.location.openTokenStartIndex, EndIndex = jsitem.location.endTokenEndIndex
                                        });
                                    }
                                }
                            }
                        }

                        // Form is handlbed by component...

                        domobject.Body = Kooboo.Sites.Service.DomService.UpdateSource(domobject.Body, updates);

                        repo.AddOrUpdate(domobject);
                    }
                }
            }
        }
Example #9
0
        private List <SourceUpdate> GetChanges(SiteDb SiteDb, DomObject DomObject, string Language = null)
        {
            List <SourceUpdate> updates = new List <SourceUpdate>();

            if (string.IsNullOrEmpty(DomObject.Body))
            {
                return(updates);
            }

            string ObjectRelativeUrl = GetObjectUrl(SiteDb, DomObject);


            ObjectRelativeUrl = ObjectRelativeUrl.Replace("\\", "/");

            if (!ObjectRelativeUrl.StartsWith("/"))
            {
                ObjectRelativeUrl = "/" + ObjectRelativeUrl;
            }

            var dom = DomObject.Dom;

            foreach (var item in dom.Links.item)
            {
                string itemsrc = Service.DomUrlService.GetLinkOrSrc(item);

                if (!string.IsNullOrEmpty(itemsrc))
                {
                    CheckAndAddChange(updates, item, itemsrc, ObjectRelativeUrl);
                }
            }

            foreach (var item in dom.images.item)
            {
                string itemsrc = DomUrlService.GetLinkOrSrc(item);

                if (!string.IsNullOrEmpty(itemsrc) && !Kooboo.Lib.Utilities.DataUriService.isDataUri(itemsrc))
                {
                    CheckAndAddChange(updates, item, itemsrc, ObjectRelativeUrl);
                }
            }

            HTMLCollection scripts = dom.getElementsByTagName("script");

            foreach (var item in scripts.item)
            {
                if (item.hasAttribute("src"))
                {
                    string srcurl = Service.DomUrlService.GetLinkOrSrc(item);

                    if (!string.IsNullOrEmpty(srcurl))
                    {
                        CheckAndAddChange(updates, item, srcurl, ObjectRelativeUrl);
                    }
                }
            }

            HTMLCollection embedElement = dom.getElementsByTagName("embed");

            foreach (var item in embedElement.item)
            {
                string fileurl = Kooboo.Sites.Service.DomUrlService.GetLinkOrSrc(item);

                if (!string.IsNullOrEmpty(fileurl))
                {
                    CheckAndAddChange(updates, item, fileurl, ObjectRelativeUrl);
                }
            }

            HTMLCollection styletags = dom.getElementsByTagName("link");

            foreach (var item in styletags.item)
            {
                if (item.hasAttribute("rel") && item.getAttribute("rel").ToLower().Contains("stylesheet"))
                {
                    string itemurl = Kooboo.Sites.Service.DomUrlService.GetLinkOrSrc(item);

                    if (!string.IsNullOrEmpty(itemurl))
                    {
                        CheckAndAddChange(updates, item, itemurl, ObjectRelativeUrl);
                    }
                }
            }

            return(updates);
        }