Exemple #1
0
        private static void InjectPartialView(CompileResourceRequest request, KEntity kentity, GetProjectDetailsResponseModel projectDetails, GetPartialPagesDetailsResponseModel partialPages, string[] lines, int i)
        {
            var partialregex  = Constants.PartialPageRegularExpression;
            var partialresult = partialregex.Matches(lines[i]);

            if (partialPages != null && partialPages.Resources != null && partialPages.Resources.Any())
            {
                for (int mc = 0; mc < partialresult.Count; mc++)
                {
                    if (partialresult[mc].Groups.Count > 1)
                    {
                        var groupparam = partialresult[mc].Groups[1].Value?.Trim('(', ')').Trim('\'').ToLower();
                        if (!string.IsNullOrEmpty(groupparam))
                        {
                            var partialViewParams = groupparam.Split(',');
                            var viewName          = partialViewParams[0]?.Trim('\'');

                            if (projectDetails.Resources.Any(x => x.PageType == Models.Project.KitsunePageType.PARTIAL && x.SourcePath.ToLower() == viewName.ToLower()))
                            {
                                var htmlData = new HtmlDocument();
                                htmlData.LoadHtml(partialPages.Resources.FirstOrDefault(x => string.Compare(x.SourcePath, viewName, true) == 0).HtmlSourceString);
                                //Support partial page with Html body and without body tag
                                string partialbody       = htmlData.DocumentNode.SelectSingleNode("/html/body")?.InnerHtml ?? htmlData.DocumentNode.OuterHtml;
                                var    partialCompiledOb = KitsuneCompiler.CompileHTML(new CompileResourceRequest
                                {
                                    FileContent = partialbody,
                                    IsPublish   = true,
                                    SourcePath  = viewName,
                                    PageType    = Models.Project.KitsunePageType.PARTIAL,
                                    ProjectId   = request.ProjectId,
                                    UserEmail   = request.UserEmail
                                }, out htmlData, projectDetails, kentity, partialPages);
                                if (partialCompiledOb != null && partialCompiledOb.Success)
                                {
                                    lines[i] = lines[i].Replace(partialresult[mc].Value, partialCompiledOb.CompiledString);
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Validate objects used in expressions are valid.
        /// </summary>
        /// <param name="compileErrors"></param>
        /// <param name="kentity"></param>
        /// <param name="objectNamesValidated"></param>
        /// <param name="classNameAlias"></param>
        /// <param name="baseClassList"></param>
        /// <param name="node"></param>
        private void ValidateExpressions(List <CompilerError> compileErrors, Dictionary <string, KEntity> entities, Dictionary <string, bool> objectNamesValidated, Dictionary <string, string> classNameAlias, IEnumerable <KClass> baseClassList, HtmlNode node, List <MatchNode> objectNamesToValidate, Dictionary <string, int> customVariables, GetProjectDetailsResponseModel projectDetails)
        {
            if (node.NodeType != HtmlNodeType.Element)
            {
                return;
            }
            int           lineNo           = 0;
            CompilerError viewExist        = null;
            CompilerError partialViewExist = null;
            string        viewExpression   = string.Empty;

            foreach (string line in node.OuterHtml.Split("\n"))
            {
                List <MatchNode> expressionNodes = HtmlHelper.GetExpressionFromElement(line, node.Line + lineNo);
                foreach (var expression in expressionNodes)
                {
                    try
                    {
                        viewExpression = expression.Value;
                        //View() function validation

                        viewExist = KitsuneCompiler.ValidateView(ref viewExpression, entities.First().Value);
                        if (viewExist != null)
                        {
                            viewExist.LineNumber   = expression.Line;
                            viewExist.LinePosition = expression.Column;
                            compileErrors.Add(viewExist);
                        }
                        //Partial() view function validation
                        partialViewExist = KitsuneCompiler.ValidatePartialView(ref viewExpression, projectDetails.Resources);
                        if (partialViewExist != null)
                        {
                            partialViewExist.LineNumber   = expression.Line;
                            partialViewExist.LinePosition = expression.Column;
                            compileErrors.Add(partialViewExist);
                        }

                        foreach (string raw_obj in Parser.GetObjects(viewExpression))
                        {
                            string obj = raw_obj.Replace("[[", "").Replace("]]", "").ToLower();
                            if (customVariables.ContainsKey(obj))
                            {
                                continue;
                            }
                            else
                            {
                                objectNamesToValidate.Add(new MatchNode {
                                    Value = obj, Line = node.Line + lineNo, Column = line.IndexOf(obj)
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(ex.Message, node.Line, node.LinePosition));
                        continue;
                    }
                }
                lineNo++;
            }
            foreach (MatchNode obj in objectNamesToValidate)
            {
                Boolean isAlias    = false;
                string  objectName = obj.Value.ToLower();
                if ((classNameAlias.ContainsKey(objectName) && classNameAlias[objectName] == "") || customVariables.ContainsKey(objectName))
                {
                    continue;
                }
                string baseClassName = objectName.Split('.')[0];
                while (baseClassName != "kresult" && baseClassName != "search" && classNameAlias.ContainsKey(baseClassName))
                {
                    int index = objectName.IndexOf('.');
                    objectName    = classNameAlias[baseClassName] + (index >= 0 ? objectName.Substring(index) : "");
                    isAlias       = true;
                    baseClassName = objectName.Split('.')[0];
                }
                if (objectNamesValidated.ContainsKey(objectName))
                {
                    if (!objectNamesValidated[objectName] && !isAlias)
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(String.Format(ErrorCodeConstants.UnrecognizedType, objectName), obj.Line, obj.Column));
                    }
                }
                else
                {
                    string[] objectPathArray  = objectName.ToLower().Split('.');
                    string   basePropertyName = objectPathArray[0];
                    var      baseClass        = baseClassList.FirstOrDefault(x => x.Name?.ToLower() == basePropertyName);

                    if (baseClass != null)
                    {
                        var baseEntity = entities.Keys.Contains(basePropertyName) ? entities[basePropertyName] : null;

                        if (baseEntity == null)
                        {
                            baseEntity = entities.First().Value;
                        }

                        IEnumerable <CompilerError> errorList = ValidateExpression(objectPathArray, 1, basePropertyName, PropertyType.obj, obj.Line, obj.Column, baseEntity);
                        if (errorList.Count() > 0)
                        {
                            compileErrors.AddRange(errorList);
                            objectNamesValidated.Add(objectName, false);
                        }
                        else
                        {
                            objectNamesValidated.Add(objectName, true);
                        }
                    }
                    else if (!ValidObjects.Contains(basePropertyName.Trim('[').Trim(']')) && ((basePropertyName.StartsWith("kresult") && !classNameAlias.ContainsKey("kresult")) || !basePropertyName.StartsWith("kresult")))
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(String.Format(ErrorCodeConstants.UnrecognizedClass, basePropertyName), obj.Line, obj.Column));
                        //objectNamesValidated.Add(objectName, false);
                    }
                }
            }
            //InnerText returns text from inner html tags, need to clear so we don't run parser again. DOM is unaffected by this.
            node.InnerHtml = "";
            objectNamesToValidate.Clear();
        }