Example #1
0
        private static List <Element> GetSameMetaTags(Document dom, HtmlMeta meta)
        {
            List <Element> result = new List <Element>();

            if (!DomService.HasHeadTag(dom))
            {
                return(null);
            }

            var metas = dom.head.getElementsByTagName("meta").item;

            if (!string.IsNullOrEmpty(meta.name))
            {
                string lowermeta = meta.name.ToLower();
                foreach (var item in metas)
                {
                    var name = item.getAttribute("name");
                    if (!string.IsNullOrEmpty(name) && name.ToLower() == lowermeta)
                    {
                        result.Add(item);
                    }
                }
            }

            if (result.Any())
            {
                return(result);
            }

            if (!string.IsNullOrEmpty(meta.httpequiv))
            {
                string lowermeta = meta.httpequiv.ToLower();
                foreach (var item in metas)
                {
                    var name = item.getAttribute("http-equiv");
                    if (!string.IsNullOrEmpty(name) && name.ToLower() == lowermeta)
                    {
                        result.Add(item);
                    }
                }
            }

            if (!string.IsNullOrEmpty(meta.charset))
            {
                //  string lowermeta = meta.charset.ToLower();
                foreach (var item in metas)
                {
                    var name = item.getAttribute("charset");
                    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(meta.charset))
                    {
                        result.Add(item);
                    }
                }
            }

            return(result);
        }
Example #2
0
        internal static List <SourceUpdate> GetUpdates(HtmlHeader header, Document dom, string OnlyEnableCulture)
        {
            List <SourceUpdate> result;

            if (DomService.HasHeadTag(dom))
            {
                result = GetUpdateWithDomHeader(header, dom, OnlyEnableCulture);
            }
            else
            {
                result = GetUpdateWithOutDomHeader(header, dom, OnlyEnableCulture);
            }


            return(result);
        }
Example #3
0
        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));
        }