Example #1
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Content))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            string attName = null;

            foreach (var item in element.attributes)
            {
                if (item.name == "tal-content" || item.name == "k-content" || item.name == "tal-replace" || item.name == "k-replace")
                {
                    attName = item.name;
                    break;
                }
            }
            if (!string.IsNullOrEmpty(attName))
            {
                var    response = new EvaluatorResponse();
                var    result   = new List <IRenderTask>();
                string value    = element.getAttribute(attName);
                element.removeAttribute(attName);
                result.Add(new ValueRenderTask(value));
                response.ContentTask = result;
                if (attName == "tal-replace" || attName == "k-replace")
                {
                    response.OmitTag = true;
                }

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var bindingTask = new BindingRenderTask(value);
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }

                options.HasContentTask = true;
                return(response);
            }
            return(null);
        }
Example #2
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Label))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            string LabelName = null;

            foreach (var item in element.attributes)
            {
                var lower = item.name.ToLower();
                if (lower == "tal-label" || lower == "k-label")
                {
                    LabelName = item.name;
                    break;
                }
            }
            if (!string.IsNullOrEmpty(LabelName))
            {
                var    response = new EvaluatorResponse();
                var    result   = new List <IRenderTask>();
                string value    = element.getAttribute(LabelName);
                element.removeAttribute(LabelName);

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var traceability = new ComponentTrace(value, "label");
                    var bindingTask  = new BindingRenderTask(traceability);
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }

                result.Add(new LabelRenderTask(value));
                response.ContentTask = result;
                return(response);
            }
            return(null);
        }
Example #3
0
        public RepeatRenderTask(string DataKey, string Alias, bool RepeatSelf, Element element, EvaluatorOption options)
        {
            this.datakey    = DataKey;
            this.alias      = Alias;
            this.repeatself = RepeatSelf;
            string boundary = null;

            if (options.RequireBindingInfo)
            {
                boundary = Kooboo.Lib.Helper.StringHelper.GetUniqueBoundary();
            }

            BindingEndRenderTask bindingEndRenderTask = null;

            if (options.RequireBindingInfo)
            {
                var bindingRenderTask = new BindingRenderTask(new RepeatItemTrace(Alias));
                bindingEndRenderTask = bindingRenderTask.BindingEndRenderTask;
                this.SubTasks.Add(bindingRenderTask);
            }

            if (repeatself)
            {
                string NewHtml = Service.DomService.ReSerializeElement(element, element.InnerHtml);
                this.SubTasks.AddRange(RenderEvaluator.Evaluate(NewHtml, options));
            }
            else
            {
                string opentag = string.Empty;

                this.ContainerEndTag = "</" + element.tagName + ">";

                var attributeEvaluator = new AttributeEvaluator();
                var response           = attributeEvaluator.Evaluate(element, options);
                if (response != null && response.AttributeTask != null && response.AttributeTask.Count() > 0)
                {
                    opentag = RenderHelper.GetHalfOpenTag(element);
                    this.ContainerTask.Add(new ContentRenderTask(opentag));
                    this.ContainerTask.AddRange(response.AttributeTask);
                    this.ContainerTask.Add(new ContentRenderTask(">"));
                }
                else
                {
                    this.ContainerTask.Add(new ContentRenderTask(Service.DomService.ReSerializeOpenTag(element)));
                }
                this.SubTasks.AddRange(RenderEvaluator.Evaluate(element.InnerHtml, options));
            }

            if (options.RequireBindingInfo)
            {
                this.SubTasks.Add(bindingEndRenderTask);
            }
        }
Example #4
0
        public static async Task <string> RenderPageAsync(FrontContext context)
        {
            if (context.Page.Parameters.Count > 0)
            {
                context.RenderContext.DataContext.Push(context.Page.Parameters);
            }

            string result = string.Empty;

            List <IRenderTask> RenderPlan = null;

            var option = RenderOptionHelper.GetPageOption(context);

            if (option.RequireBindingInfo)
            {
                string html = DomService.ApplyKoobooId(context.Page.Body);
                RenderPlan = RenderEvaluator.Evaluate(html, option);
                var traceability = new ComponentTrace(context.Page.Id.ToString(), "page");
                var bindingTask  = new BindingRenderTask(traceability, new Dictionary <string, string> {
                    { "scope", "true" }
                });
                RenderPlan.Insert(0, bindingTask);
                RenderPlan.Add(bindingTask.BindingEndRenderTask);

                result = RenderHelper.Render(RenderPlan, context.RenderContext);
                result = DomService.EnsureDocType(result);
            }
            else
            {
                RenderPlan = Cache.RenderPlan.GetOrAddRenderPlan(context.SiteDb, context.Page.Id, () => RenderEvaluator.Evaluate(context.Page.Body, option));

                result = RenderHelper.Render(RenderPlan, context.RenderContext);
            }


            if (context.Page.Type == Models.PageType.RichText)
            {
                //special for richtext editor. meta name = "viewport" content = "width=device-width, initial-scale=1"
                var header = new Models.HtmlHeader();
                Dictionary <string, string> content = new Dictionary <string, string>();
                content.Add("", "width=device-width, initial-scale=1");
                header.Metas.Add(new Models.HtmlMeta()
                {
                    name = "viewport", content = content
                });

                result = HtmlHeadService.SetHeaderToHtml(result, header);
            }

            return(result);
        }
Example #5
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Form))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            if (ShouldEvaluate(element))
            {
                var response = new EvaluatorResponse();
                List <IRenderTask> result = new List <IRenderTask>();

                Guid   OwnerObjectId = options.OwnerObjectId;
                string KoobooId      = Service.DomService.GetKoobooId(element);
                Guid   FormId        = Kooboo.Data.IDGenerator.GetFormId(OwnerObjectId, KoobooId);

                result.Add(new FormRenderTask(element, OwnerObjectId, FormId, options));

                response.ContentTask       = result;
                response.StopNextEvaluator = true;
                response.OmitTag           = true;

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var traceability = new ComponentTrace(FormId.ToString(), "innerform");
                    var bindingTask  = new BindingRenderTask(traceability);
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }
                return(response);
            }

            return(null);
        }
Example #6
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Component))
            {
                return(null);
            }
            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            if (Components.Manager.IsComponent(element))
            {
                var response = new EvaluatorResponse();
                var result   = new List <IRenderTask>();
                result.Add(new ComponentRenderTask(element));
                response.ContentTask       = result;
                response.OmitTag           = true;
                response.StopNextEvaluator = true;

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var traceability = new ComponentTrace(element.id, element.tagName);
                    var bindingTask  = new BindingRenderTask(traceability, new Dictionary <string, string> {
                        { "scope", "true" }
                    });
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }
                return(response);
            }

            return(null);
        }
Example #7
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.SiteLayout))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            if (element.tagName == "layout" && (element.hasAttribute("id") || element.hasAttribute("name")))
            {
                var response = new EvaluatorResponse();
                var result   = new List <IRenderTask>();
                result.Add(new SiteLayoutRenderTask(element, options));
                response.ContentTask = result;
                response.OmitTag     = true;

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var traceability = new ComponentTrace(element.id, element.tagName);
                    var bindingTask  = new BindingRenderTask(traceability, new Dictionary <string, string> {
                        { "scope", "true" }
                    });
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }
                return(response);
            }

            return(null);
        }
Example #8
0
        public static async Task <string> RenderPageAsync(FrontContext context)
        {
            if (context.Page.Parameters.Count > 0)
            {
                context.RenderContext.DataContext.Push(context.Page.Parameters);
            }

            string result = string.Empty;

            List <IRenderTask> RenderPlan = null;

            var option = RenderOptionHelper.GetPageOption(context);

            if (option.RequireBindingInfo)
            {
                string html = DomService.ApplyKoobooId(context.Page.Body);
                RenderPlan = RenderEvaluator.Evaluate(html, option);
                var traceability = new ComponentTrace(context.Page.Id.ToString(), "page");
                var bindingTask  = new BindingRenderTask(traceability, new Dictionary <string, string> {
                    { "scope", "true" }
                });
                RenderPlan.Insert(0, bindingTask);
                RenderPlan.Add(bindingTask.BindingEndRenderTask);

                result = RenderHelper.Render(RenderPlan, context.RenderContext);
                result = DomService.EnsureDocType(result);
            }
            else
            {
                RenderPlan = Cache.RenderPlan.GetOrAddRenderPlan(context.SiteDb, context.Page.Id, () => RenderEvaluator.Evaluate(context.Page.Body, option));

                // check the cache.
                if (context.Page.EnableCache)
                {
                    Dictionary <string, string> querystring = null;

                    if (!string.IsNullOrWhiteSpace(context.Page.CacheQueryKeys))
                    {
                        //querystring = RequestManager.GetQueryString(context.RenderContext.Request);
                        querystring = GetParaValues(context.RenderContext, context.Page.CacheKeys);
                    }

                    if (context.Page.CacheByVersion)
                    {
                        result = PageCache.PageCache.GetByVersion(context.SiteDb.Id, context.Page.Id, context.Page.Version, querystring);
                    }
                    else
                    {
                        result = PageCache.PageCache.GetByMinutes(context.SiteDb.Id, context.Page.Id, context.Page.CacheMinutes, querystring, context.Page.Version);
                    }

                    if (string.IsNullOrEmpty(result))
                    {
                        result = RenderHelper.Render(RenderPlan, context.RenderContext);

                        Kooboo.Sites.Render.PageCache.PageCache.Set(context.SiteDb.Id, context.Page.Id, result, context.Page.Version, querystring);
                    }


                    // cache result may have replacement.
                }
                else
                {
                    result = RenderHelper.Render(RenderPlan, context.RenderContext);
                }
            }


            if (context.Page.Type == Models.PageType.RichText)
            {
                //special for richtext editor. meta name = "viewport" content = "width=device-width, initial-scale=1"
                var header = new Models.HtmlHeader();
                Dictionary <string, string> content = new Dictionary <string, string>();
                content.Add("", "width=device-width, initial-scale=1");
                header.Metas.Add(new Models.HtmlMeta()
                {
                    name = "viewport", content = content
                });

                result = HtmlHeadService.SetHeaderToHtml(result, header);
            }

            return(result);
        }
Example #9
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Url))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element  = node as Element;
            var response = new EvaluatorResponse();

            string url     = string.Empty;
            string attName = null;

            foreach (var item in element.attributes)
            {
                var lower = item.name.ToLower();
                if (lower == "tal-href" || lower == "k-href")
                {
                    attName = item.name;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(attName))
            {
                url = element.getAttribute(attName);
                element.removeAttribute(attName);

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var bindingTask = new BindingRenderTask(url, new Dictionary <string, string> {
                        { "attribute", "href" }
                    });
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }
            }

            if (string.IsNullOrEmpty(url))
            {
                if (options.RenderUrl && element.tagName == "a")
                {
                    string href = element.getAttribute("href");
                    if (!string.IsNullOrEmpty(href))
                    {
                        url = href.Trim();
                    }
                    if (!options.RequireBindingInfo)
                    {
                        element.removeAttribute("href");
                    }
                }
            }

            if (!string.IsNullOrEmpty(url))
            {
                if (Kooboo.Sites.Service.DomUrlService.IsSpecialUrl(url))
                {
                    return(null);
                }

                var result = new List <IRenderTask>();
                result.Add(new ContentRenderTask(" href=\""));
                result.Add(new UrlRenderTask(url));
                result.Add(new ContentRenderTask("\""));
                response.AttributeTask = result;
                element.removeAttribute("href");

                if (!options.HasContentTask)
                {
                    response.ContentTask       = RenderEvaluator.Evaluate(element.InnerHtml, options);
                    response.StopNextEvaluator = true;
                }

                return(response);
            }


            return(null);
        }
Example #10
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Attribute))
            {
                return(null);
            }

            Dictionary <string, string> appendValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            string attName = null;

            foreach (var item in element.attributes)
            {
                if (item.name == "tal-attribute" || item.name == "k-attribute" || item.name == "tal-attributes" || item.name == "k-attributes")
                {
                    attName = item.name;
                    break;
                }
            }

            if (string.IsNullOrEmpty(attName))
            {
                return(null);
            }

            string attributeValues = element.getAttribute(attName);

            element.removeAttribute(attName);

            if (string.IsNullOrEmpty(attributeValues) || attributeValues.IndexOf(' ') < 0)
            {
                return(null);
            }

            EvaluatorResponse response = new EvaluatorResponse();

            string[] attributes = attributeValues.Split(';');

            foreach (var item in attributes)
            {
                var attkeyvalue = ParseAtt(item);
                if (attkeyvalue == null)
                {
                    continue;
                }

                string attributeName  = attkeyvalue.Key;
                string attributeValue = attkeyvalue.Value;

                if (AppendAttributes.ContainsKey(attributeName))
                {
                    string sep   = AppendAttributes[attributeName];
                    string value = element.getAttribute(attributeName);

                    if (!string.IsNullOrEmpty(value))
                    {
                        if (!value.Trim().EndsWith(sep))
                        {
                            value = value + sep;
                        }
                        if (appendValues.ContainsKey(attributeName))
                        {
                            var orgvalue = appendValues[attributeName];
                            value = orgvalue + value;
                        }
                        appendValues[attributeName] = value;
                    }
                }

                List <IRenderTask> tasks = new List <IRenderTask>();
                tasks.Add(new ContentRenderTask(" " + attributeName + "=\""));

                if (appendValues.ContainsKey(attributeName))
                {
                    tasks.Add(new ContentRenderTask(appendValues[attributeName]));
                }

                tasks.Add(new ValueRenderTask(attributeValue));
                tasks.Add(new ContentRenderTask("\""));

                if (response.AttributeTask == null)
                {
                    response.AttributeTask = tasks;
                }
                else
                {
                    response.AttributeTask.AddRange(tasks);
                }

                element.removeAttribute(attributeName);

                if (options.RequireBindingInfo)
                {
                    if (response.BindingTask == null)
                    {
                        response.BindingTask = new List <IRenderTask>();
                    }
                    var bindingTask = new BindingRenderTask(attributeValue, new Dictionary <string, string> {
                        { "attribute", attributeName }
                    });
                    response.BindingTask.Add(bindingTask);
                    if (response.EndBindingTask == null)
                    {
                        response.EndBindingTask = new List <IRenderTask>();
                    }
                    response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
                }
            }

            if (response.AttributeTask == null || response.AttributeTask.Count() == 0)
            {
                return(null);
            }
            else
            {
                return(response);
            }
        }
Example #11
0
        public EvaluatorResponse Evaluate(Node node, EvaluatorOption options)
        {
            if (options.IgnoreEvaluators.HasFlag(EnumEvaluator.Repeater))
            {
                return(null);
            }

            if (node.nodeType != enumNodeType.ELEMENT)
            {
                return(null);
            }
            var element = node as Element;

            string attName = null;

            foreach (var item in element.attributes)
            {
                var lower = item.name.ToLower();
                if (lower == "tal-repeat" || lower == "k-repeat" || lower == "k-foreach")
                {
                    attName = item.name;
                    break;
                }
            }

            if (string.IsNullOrEmpty(attName))
            {
                return(null);
            }
            string repeatitems = element.getAttribute(attName).Trim();

            if (string.IsNullOrEmpty(repeatitems))
            {
                return(null);
            }

            //if (!options.RequireBindingInfo)
            // {
            element.removeAttribute(attName);
            //}

            bool repeatself = false;

            if (element.hasAttribute("repeat-self"))
            {
                repeatself = true;
                element.removeAttribute("repeat-self");
            }
            else if (element.hasAttribute("tal-repeat-self"))
            {
                repeatself = true;
                element.removeAttribute("tal-repeat-self");
            }
            else if (element.hasAttribute("k-repeat-self"))
            {
                repeatself = true;
                element.removeAttribute("k-repeat-self");
            }

            string datakey;
            string alias = string.Empty;

            // the repeat item can be with or without an alias. it can be like articles or "item articles";
            string[] items = repeatitems.Split(' ');
            if (items.Length == 1)
            {
                datakey = items[0];
            }
            else if (items.Length == 2)
            {
                datakey = items[1];
                alias   = items[0];
            }
            else
            {
                return(null);
            }

            RepeatRenderTask task = new RepeatRenderTask(datakey, alias, repeatself, element, options);

            var response = new EvaluatorResponse();
            List <IRenderTask> result = new List <IRenderTask>();

            result.Add(task);
            response.ContentTask       = result;
            response.OmitTag           = true;
            response.StopNextEvaluator = true;

            if (options.RequireBindingInfo)
            {
                if (response.BindingTask == null)
                {
                    response.BindingTask = new List <IRenderTask>();
                }
                var traceability = new RepeatTrace();
                var bindingTask  = new BindingRenderTask(traceability);
                response.BindingTask.Add(bindingTask);
                if (response.EndBindingTask == null)
                {
                    response.EndBindingTask = new List <IRenderTask>();
                }
                response.EndBindingTask.Add(bindingTask.BindingEndRenderTask);
            }

            return(response);
        }