void MinimizeCss() { var cssProcessor = new CssProcessor(_tools); Assert.Equal(".c{width:100%}", cssProcessor.ConcatenateAndMinifyCss( new[] { new SourceFromPair(".c { width: 100% }", "/dir/file.css") }, (url, from) => url).Result); }
void ConcatenateAndMinimizeCss() { var cssProcessor = new CssProcessor(_tools); Assert.Equal(".c{background-image:url(/dir/logo.png)}.c2{background-image:url(pogo.png)}", cssProcessor.ConcatenateAndMinifyCss( new[] { new SourceFromPair(".c { background-image: url(\"logo.png\") }", "/dir/file.css"), new SourceFromPair(".c2 { background-image: url(\"pogo.png\") }", "./file.css") }, (url, from) => from + "/" + url).Result); }
void ProcessCssWithUrl() { var cssProcessor = new CssProcessor(_tools); Func <string, string, string> urlReplacerUrlFrom = (url, from) => { Assert.Equal("logo.png", url); Assert.Equal("./dir", from); return(from + "/" + url); }; Assert.Equal(".c { background-image: url(\"./dir/logo.png\") }", cssProcessor.ProcessCss(".c { background-image: url(\"logo.png\") }", "./dir/file.css", urlReplacerUrlFrom).Result); }
public void TestCss() { var crawler = new RuiJiCrawler(); var request = new Request("http://www.legaldaily.com.cn/index_article/content/2016-08/17/content_6765457.htm?node=5955"); var response = crawler.Request(request); var content = response.Data.ToString(); var p = new CssProcessor(); var s = new CssSelector(); s.Value = "div.f12:first"; s.Type = Core.Extractor.Enum.CssTypeEnum.Text; var pr = new ProcessResult(); pr.Matches.Add(content); pr = p.ProcessNeed(s, pr); Assert.IsTrue(pr.Content.IndexOf(">>") == -1); }
public void Build(bool compress, bool mangle, bool beautify, bool _, string?__) { var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in _buildResult.Path2FileInfo.Values.OrderBy(f => f.Owner.FullPath).ToArray()) { if (source.Type == FileCompilationType.Css || source.Type == FileCompilationType.ImportedCss) { cssToBundle.Add(new SourceFromPair(source.Owner.Utf8Content, source.Owner.FullPath)); } else if (source.Type == FileCompilationType.Resource) { _mainBuildResult.FilesContent.GetOrAddValueRef(_buildResult.ToOutputUrl(source)) = source.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = _mainBuildResult.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(_project.Tools); var cssContent = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; _buildResult.Path2FileInfo.TryGetValue(fullJustName, out var fileAdditionalInfo); _mainBuildResult.FilesContent.GetOrAddValueRef(_buildResult.ToOutputUrl(fileAdditionalInfo)) = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.GetFile(fileAdditionalInfo.OutputUrl) + full.Substring(fullJustName.Length)); }).Result; var cssImports = ""; foreach (var match in Regex.Matches(cssContent, "@import .*;")) { cssImports += match.ToString(); cssContent = cssContent.Replace(match.ToString(), ""); } _mainBuildResult.FilesContent.GetOrAddValueRef(cssPath) = cssImports + cssContent; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (_project.SpriteGeneration) { _bundlePng = _project.BundlePngUrl; var bundlePngContent = _project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { _mainBuildResult.FilesContent.GetOrAddValueRef(PathUtils.InjectQuality(_bundlePng, slice.Quality)) = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } var bundler = new BundlerImpl(_tools); bundler.Callbacks = this; if ((_project.ExampleSources?.Count ?? 0) > 0) { bundler.MainFiles = new[] { _project.ExampleSources[0] }; } else { bundler.MainFiles = new[] { _project.MainFile }; } _mainJsBundleUrl = _buildResult.BundleJsUrl; bundler.Compress = compress; bundler.Mangle = mangle; bundler.Beautify = beautify; bundler.Defines = _project.BuildDefines(_mainBuildResult); bundler.Bundle(); if (!_project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); _mainBuildResult.FilesContent.GetOrAddValueRef("index.html") = _indexHtml; } if (_project.SubProjects != null) { var newSubBundlers = new RefDictionary <string, BundleBundler>(); foreach (var(projPath, subProject) in _project.SubProjects.OrderBy(a => a.Value?.Variant == "serviceworker")) { if (subProject == null) { continue; } if (_subBundlers == null || !_subBundlers.TryGetValue(projPath, out var subBundler)) { subBundler = new BundleBundler(_tools, _mainBuildResult, subProject, _buildResult.SubBuildResults.GetOrFakeValueRef(projPath)); } newSubBundlers.GetOrAddValueRef(projPath) = subBundler; subBundler.Build(compress, mangle, beautify, false, null); } _subBundlers = newSubBundlers; } else { _subBundlers = null; } }
public void Build(bool compress, bool mangle, bool beautify) { var diskCache = Project.Owner.DiskCache; var root = Project.Owner.Owner.FullPath; _jsFilesContent = new Dictionary <string, string>(); var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in BuildResult.Path2FileInfo) { if (source.Value.Type == FileCompilationType.TypeScript || source.Value.Type == FileCompilationType.JavaScript || source.Value.Type == FileCompilationType.JavaScriptAsset) { if (source.Value.Output == null) { continue; // Skip d.ts } _jsFilesContent[PathUtils.ChangeExtension(source.Key, "js").ToLowerInvariant()] = source.Value.Output; } else if (source.Value.Type == FileCompilationType.Css) { cssToBundle.Add(new SourceFromPair(source.Value.Owner.Utf8Content, source.Value.Owner.FullPath)); } else if (source.Value.Type == FileCompilationType.Resource) { FilesContent[source.Value.OutputUrl] = source.Value.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = Project.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(Project.Tools); FilesContent[cssPath] = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; var fileAdditionalInfo = BuildModuleCtx.AutodetectAndAddDependencyCore(Project, fullJustName, diskCache.TryGetItem(from) as IFileCache); FilesContent[fileAdditionalInfo.OutputUrl] = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.SplitDirAndFile(fileAdditionalInfo.OutputUrl).Item2 + full.Substring(fullJustName.Length)); }).Result; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (Project.SpriteGeneration) { _bundlePng = Project.BundlePngUrl; var bundlePngContent = Project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { FilesContent[PathUtils.InjectQuality(_bundlePng, slice.Quality)] = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } var bundler = new BundlerImpl(_tools); bundler.Callbacks = this; if (Project.ExampleSources.Count > 0) { bundler.MainFiles = new[] { PathUtils.ChangeExtension(Project.ExampleSources[0], "js") }; } else { bundler.MainFiles = new[] { PathUtils.ChangeExtension(Project.MainFile, "js") }; } _mainJsBundleUrl = Project.BundleJsUrl; bundler.Compress = compress; bundler.Mangle = mangle; bundler.Beautify = beautify; var defines = new Dictionary <string, object>(); foreach (var p in Project.Defines) { defines.Add(p.Key, p.Value); } bundler.Defines = defines; bundler.Bundle(); if (!Project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); FilesContent["index.html"] = _indexHtml; } }
void ProcessSimpleCss() { var cssProcessor = new CssProcessor(_tools); Assert.Equal(".c { width: 100% }", cssProcessor.ProcessCss(".c { width: 100% }", "/dir/file.css", (url, from) => url).Result); }
public void Build(bool compress, bool mangle, bool beautify) { var diskCache = Project.Owner.DiskCache; var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in BuildResult.Path2FileInfo.Values.OrderBy(f => f.Owner.FullPath).ToArray()) { if (source.Type == FileCompilationType.Css || source.Type == FileCompilationType.ImportedCss) { cssToBundle.Add(new SourceFromPair(source.Owner.Utf8Content, source.Owner.FullPath)); } else if (source.Type == FileCompilationType.Resource) { FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(source)) = source.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = BuildResult.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(Project.Tools); var cssContent = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; BuildResult.Path2FileInfo.TryGetValue(fullJustName, out var fileAdditionalInfo); FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(fileAdditionalInfo)) = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.GetFile(fileAdditionalInfo.OutputUrl) + full.Substring(fullJustName.Length)); }).Result; var cssImports = ""; foreach (var match in Regex.Matches(cssContent, "@import .*;")) { cssImports += match.ToString(); cssContent = cssContent.Replace(match.ToString(), ""); } FilesContent.GetOrAddValueRef(cssPath) = cssImports + cssContent; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (Project.SpriteGeneration) { _bundlePng = Project.BundlePngUrl; var bundlePngContent = Project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { FilesContent.GetOrAddValueRef(PathUtils.InjectQuality(_bundlePng, slice.Quality)) = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } var bundler = new BundlerImpl(_tools); bundler.Callbacks = this; if (Project.ExampleSources.Count > 0) { bundler.MainFiles = new[] { Project.ExampleSources[0] }; } else { bundler.MainFiles = new[] { Project.MainFile }; } _mainJsBundleUrl = BuildResult.BundleJsUrl; bundler.Compress = compress; bundler.Mangle = mangle; bundler.Beautify = beautify; var defines = new Dictionary <string, object>(); foreach (var p in Project.Defines) { defines.Add(p.Key, p.Value); } bundler.Defines = defines; bundler.Bundle(); if (!Project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); FilesContent.GetOrAddValueRef("index.html") = _indexHtml; } }