//Called from the Razor-Views or master page public static Scripts KraftScripts(this Profile profile, string moduleDepStartFile = RESOURCEDEPENDENCY_FILE_NAME, string rootVirtualPath = "/modules") { if (!profile.HasScriptBundle(profile.Key + "-scripts")) { ScriptBundle scriptBundle = new ScriptBundle(profile.Key + "-scripts", new PhysicalFileProvider(_ModulesCollection.KraftGlobalConfigurationSettings.EnvironmentSettings.ContentRootPath), null, new List <IBundleTransform>(), false); StringBuilder contentTemplates = new StringBuilder(10000); bool appendDiv = false; //try to get the target module KraftModule profileTargetModule = _ModulesCollection.GetModule(profile.Key); if (profileTargetModule == null) { throw new Exception($"No CoreKraft module found for bundle target \"{profile.Key}\"!"); } HashSet <KraftModule> targetDeps = new HashSet <KraftModule>(); Dive(profileTargetModule, targetDeps); List <KraftModule> targetDepsSorted = targetDeps.OrderBy(x => x.DependencyOrderIndex).ToList <KraftModule>(); foreach (KraftModule kraftDepModule in targetDepsSorted) { kraftDepModule.ConstructResources(_CachingService, kraftDepModule.DirectoryName, moduleDepStartFile, true); if (kraftDepModule.ScriptKraftBundle != null) { using (KraftProfiler.Current.Step($"Time loading {kraftDepModule.Key}: ")) { scriptBundle.Include(new KraftRequireTransformation().Process(kraftDepModule.ScriptKraftBundle, kraftDepModule.ModulePath, kraftDepModule.Key, rootVirtualPath, _Logger)); scriptBundle.Transforms.Add(new JsCleanupTransformation()); } } if (kraftDepModule.TemplateKraftBundle != null && kraftDepModule.TemplateKraftBundle.TemplateFiles.Count > 0) { if (appendDiv) { contentTemplates.Append(","); } else { appendDiv = true; } HtmlTransformation htmlTransformation = new HtmlTransformation(); Func <StringBuilder, ILogger, StringBuilder> minifyHtml = htmlTransformation.Process; contentTemplates.Append(new KraftHtml2JsAssocArrayTransformation().Process(kraftDepModule.TemplateKraftBundle, minifyHtml, _Logger)); } } scriptBundle.IncludeContent("Registers.addRegister(new TemplateRegister(\"module_templates\")); Registers.getRegister(\"module_templates\").$collection= {" + contentTemplates.Append("}")); profile.Add(scriptBundle); return(profile.Scripts); } else { return(profile.Scripts); } }
public void CheckHtmlTransformation_OnInvalidHtmlInput_ShouldReturnTransformationError(string input) { var ht = new HtmlTransformation(); var sb = new StringBuilder(); sb.Append(input); ILoggerFactory loggerFac = new LoggerFactory(); var logger = loggerFac.CreateLogger("TestLogger"); ht.Process(sb, logger, _Response); var isEmpty = string.IsNullOrEmpty(_Response.TransformationErrors.ToString()); Assert.False(isEmpty); }
public void CheckHtmlTransformation_OnValidHtmlInput_ShouldReturnNoError() { var ht = new HtmlTransformation(); var sb = new StringBuilder(); sb.Append("<html><head><title>BindKraft</title></head><body></body></html>"); var result = new StringBuilder(); result = ht.Process(sb, null, _Response); var isEmpty = string.IsNullOrEmpty(_Response.TransformationErrors.ToString()); Assert.True(isEmpty); Assert.Equal("<title>BindKraft</title>", result.ToString()); }
//Called from the Razor-Views or master page public static Scripts KraftScripts(this Profile profile) { if (!profile.HasScriptBundle(profile.Key + "-scripts")) { KraftModuleCollection modulesCollection = _Builder.ApplicationServices.GetService <KraftModuleCollection>(); ScriptBundle scriptBundle = new ScriptBundle(profile.Key + "-scripts", new PhysicalFileProvider(modulesCollection.KraftGlobalConfigurationSettings.EnvironmentSettings.ContentRootPath)); StringBuilder contentTemplates = new StringBuilder(10000); bool appendDiv = false; //try to get the target module KraftModule profileTargetModule = modulesCollection.GetModule(profile.Key); if (profileTargetModule == null) { throw new Exception($"No CoreKraft module found for bundle target \"{profile.Key}\"!"); } void Dive(KraftModule kmodule, HashSet <KraftModule> deps) { foreach (var dep in kmodule.Dependencies) { Dive(dep.Value as KraftModule, deps); } deps.Add(kmodule); } HashSet <KraftModule> targetDeps = new HashSet <KraftModule>(); Dive(profileTargetModule, targetDeps); List <KraftModule> targetDepsSorted = targetDeps.OrderBy(x => x.DependencyOrderIndex).ToList <KraftModule>(); foreach (KraftModule kraftDepModule in targetDepsSorted) { if (kraftDepModule.ScriptKraftBundle != null) { using (KraftProfiler.Current.Step($"Time loading {kraftDepModule.Key}: ")) { scriptBundle.Include(new KraftRequireTransformation().Process(kraftDepModule.ScriptKraftBundle, _Logger)); } } if (kraftDepModule.TemplateKraftBundle != null && kraftDepModule.TemplateKraftBundle.TemplateFiles.Count > 0) { if (appendDiv) { contentTemplates.Append(","); } else { appendDiv = true; } HtmlTransformation htmlTransformation = new HtmlTransformation(); Func <StringBuilder, ILogger, StringBuilder> minifyHtml = htmlTransformation.Process; contentTemplates.Append(new KraftHtml2JsAssocArrayTransformation().Process(kraftDepModule.TemplateKraftBundle, minifyHtml, _Logger)); } } scriptBundle.IncludeContent("Registers.addRegister(new TemplateRegister(\"module_templates\")); Registers.getRegister(\"module_templates\").$collection= {" + contentTemplates.Append("}")); profile.Add(scriptBundle); return(profile.Scripts); } else { return(profile.Scripts); } }