コード例 #1
0
        public void EnsureJsPageEvaluatorRespectsMode()
        {
            var obj = new JsBlockContentEvaluator();

            var data = new[]
            {
                "<html>",
                "<head>",
                "</head>",
                "<body>",
                "<script type='text/javascript'>",
                "   $(function(){",
                "       alert('I am a script without any at variables);",
                "   });",
                "</script>",
                "<!-- some text-->", //TODO: Consider how to treat "joined" blocks.
                "<script type='text/javascript'>",
                "   $(function(){",
                "       alert('I am a script with an @Viewmodel.Variable);",
                "   });",
                "</script>",
                "</body>",
                "</html>",
            };

            var result = obj.Evaluate(data, JsPageEvaluationMode.RazorOnly);

            Assert.AreEqual(1, result.Length);
        }
コード例 #2
0
        public void BreakUpMixedRazorBlocks()
        {
            var obj = new JsBlockContentEvaluator();

            var data = new[]
            {
                "<html>",
                "<head>",
                "</head>",
                "<body>",
                "<script type='text/javascript'>",
                "   $(function(){",
                "       alert('I am a script without any at variables);",
                "   });",
                "</script>",
                "<script type='text/javascript'>",
                "   $(function(){",
                "       alert('I am a script with an @Viewmodel.Variable);",
                "   });",
                "</script>",
                "</body>",
                "</html>",
            };
            var result = obj.Evaluate(data, JsPageEvaluationMode.RazorOnly);

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(5, result[0].Lines.Count);
        }
コード例 #3
0
        public void Page()
        {
            var raw = AssetRetriever.GetFileAndContent(AvailableTestingResources.TestingResource);

            var evaluator = new JsBlockContentEvaluator(new RemediatedBlockJsRemover());

            var result = evaluator.Evaluate(raw.Contents, PageEvaluationMode.Any, true);

            Assert.AreEqual(2, result.Length);
        }
コード例 #4
0
        public SeperatedJs Evaluate(string[] lines, string solutionRouteDirectory, string generatedResultDirectory, string fileName, bool mergeBlocks)
        {
            var          cleanedLines = new string[lines.Length];
            const string correct      = "<script type=\"text/javascript\">";

            for (var i = 0; i < lines.Length; i++)
            {
                cleanedLines[i] = lines[i]
                                  .Replace("<script>", correct)
                                  .Replace("<script language=\"javascript\" type=\"text/javascript\">", correct);
            }

            var inlineJs = JsBlockContentEvaluator.Evaluate(cleanedLines, PageEvaluationMode.NonRazorOnly, mergeBlocks);

            GeneratedJsViewModel[] extractedJsBlocks;
            List <string>          replacmentContent;


            if (!inlineJs.Any())
            {
                extractedJsBlocks = new GeneratedJsViewModel[0];
                replacmentContent = cleanedLines.ToList();
            }
            else
            {
                replacmentContent = new List <string>();
                extractedJsBlocks = new GeneratedJsViewModel[inlineJs.Count()];

                var jsFileDetails = new RefactoredFileNameViewModel[inlineJs.Count()];
                var blockIndex    = 0;
                var lineIndex     = 0;
                var done          = false;

                for (var i = 0; i < inlineJs.Count(); i++)
                {
                    extractedJsBlocks[i] = new GeneratedJsViewModel {
                        Lines = new List <string>()
                    };
                    jsFileDetails[i] = JsFileNameEvaluator.Evaluate(solutionRouteDirectory, generatedResultDirectory, fileName, i);
                }

                var openingTagWrittenFor = -1;

                foreach (var l in cleanedLines)
                {
                    if (!done)
                    {
                        var toReplace = inlineJs[blockIndex].Lines[lineIndex];

                        if (l.Contains(toReplace))
                        {
                            var hasStartTag = Regex.Matches(toReplace, RegexConstants.ScriptOpeningTag, RegexOptions.IgnoreCase).Count > 0;
                            var line        = l;

                            var cssReplacement = Regex.Replace(toReplace, RegexConstants.ScriptClosingTag, "", RegexOptions.IgnoreCase);

                            if (hasStartTag)
                            {
                                cssReplacement = Regex.Replace(cssReplacement, RegexConstants.ScriptOpeningTag, "", RegexOptions.IgnoreCase);
                                if (openingTagWrittenFor != blockIndex)
                                {
                                    line = Regex.Replace(line, toReplace, jsFileDetails[blockIndex].HtmlLink, RegexOptions.IgnoreCase);
                                    openingTagWrittenFor = blockIndex;
                                }
                                else
                                {
                                    line = Regex.Replace(line, toReplace, "", RegexOptions.IgnoreCase);
                                }
                            }
                            else
                            {
                                line = line.Remove(toReplace);
                            }

                            if (line.Trim().Length > 0)
                            {
                                replacmentContent.Add(line.Trim());
                            }

                            if (cssReplacement.Trim().Length > 0)
                            {
                                extractedJsBlocks[blockIndex].Lines.Add(cssReplacement);
                            }
                            if (lineIndex == inlineJs[blockIndex].Lines.Count - 1)
                            {
                                extractedJsBlocks[blockIndex].ProposedFileName = jsFileDetails[blockIndex].Filename;
                                lineIndex = -1;
                                blockIndex++;
                                if (blockIndex == inlineJs.Length)
                                {
                                    done = true;
                                }
                            }
                            lineIndex++;
                        }
                        else
                        {
                            replacmentContent.Add(l);
                        }
                    }
                    else
                    {
                        replacmentContent.Add(l);
                    }
                }
            }
            return(new SeperatedJs
            {
                JsRemoved = extractedJsBlocks,
                RefactoredLines = replacmentContent.ToArray()
            });
        }
コード例 #5
0
        public SeperatedJs Evaluate(string[] lines, string solutionRouteDirectory, string generatedResultDirectory, string fileName, bool mergeBlocks)
        {
            var          cleanedLines = new string[lines.Length];
            const string correct      = "<script type=\"text/javascript\">";

            for (var i = 0; i < lines.Length; i++)
            {
                cleanedLines[i] = lines[i]
                                  .Replace("<script>", correct)
                                  .Replace("<script language=\"javascript\" type=\"text/javascript\">", correct);
            }

            var jsBlockContents = JsBlockContentEvaluator.Evaluate(cleanedLines, PageEvaluationMode.RazorOnly, mergeBlocks).ToList();

            GeneratedJsViewModel[] jsRemoved;
            List <string>          refactoredLines;


            if (jsBlockContents.Any())
            {/*
              * find the first script reference
              *
              * Convert it with the js module
              *
              * Create new js files without razor and references to them
              *
              * put in references to new Js files where old files WERE
              *
              * Done?
              */
             // We may have several blocks of JS in the view. But we will have only one new AP2 module.
             // There may be duplicated razor fragments in the existing JS blocks. The duplication must be removed
             // before adding the fragments to the new ap2 module.

                jsRemoved = new GeneratedJsViewModel[jsBlockContents.Count];

                List <JsModuleViewModel> razorLines = new List <JsModuleViewModel>();
                for (var i = 0; i < jsBlockContents.Count; i++)
                {
                    var blockContent = jsBlockContents[i];
                    razorLines.AddRange(JsModuleBlockEvaluator.Evaluate(blockContent.Lines));

                    jsRemoved[i] = new GeneratedJsViewModel
                    {
                        Lines = JsInjectNewModuleVariables.Build(blockContent.Lines, razorLines).ToList()
                    };
                }


                //(LAST?) PROBLEM IS HERE!!
                var jsModule = JsModuleFactory.Build(razorLines.Distinct().ToList()); // generate the new ap2 module from the de-duplicated razor fragments

                refactoredLines = new List <string>();

                var jsFileDetails = new RefactoredFileNameViewModel[jsBlockContents.Count];
                var blockIndex    = 0;
                var lineIndex     = 0;
                var done          = false;

                for (var i = 0; i < jsBlockContents.Count; i++)
                {
                    jsFileDetails[i] = JsFileNameEvaluator.Evaluate(solutionRouteDirectory, generatedResultDirectory, fileName, i);
                }

                var moduleHasBeenIncluded = false;

                foreach (var l in cleanedLines)
                {
                    if (!done)
                    {
                        var toReplace = jsBlockContents[blockIndex].Lines[lineIndex];

                        if (l.Contains(toReplace))
                        {
                            var hasStartTag = Regex.Matches(toReplace, RegexConstants.ScriptOpeningTag, RegexOptions.IgnoreCase).Count > 0;
                            var line        = l;

                            if (hasStartTag)
                            {
                                if (!moduleHasBeenIncluded)
                                {
                                    refactoredLines.AddRange(jsModule);
                                    moduleHasBeenIncluded = true;
                                }

                                line = Regex.Replace(line, toReplace, jsFileDetails[blockIndex].HtmlLink, RegexOptions.IgnoreCase);
                            }
                            else
                            {
                                line = line.Remove(toReplace);
                            }

                            if (line.Trim().Length > 0)
                            {
                                refactoredLines.Add(line);
                            }

                            if (lineIndex == jsBlockContents[blockIndex].Lines.Count - 1)
                            {
                                jsRemoved[blockIndex].ProposedFileName = jsFileDetails[blockIndex].Filename;
                                lineIndex = -1;
                                blockIndex++;
                                if (blockIndex == jsBlockContents.Count)
                                {
                                    done = true;
                                }
                            }
                            lineIndex++;
                        }
                        else
                        {
                            refactoredLines.Add(l);
                        }
                    }
                    else
                    {
                        refactoredLines.Add(l);
                    }
                }
            }
            else
            {
                jsRemoved       = new GeneratedJsViewModel[0];
                refactoredLines = cleanedLines.ToList();
            }

            return(new SeperatedJs
            {
                JsRemoved = jsRemoved,
                RefactoredLines = refactoredLines.ToArray()
            });
        }