/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The attribute AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { // styleSheet // : [ CHARSET_SYM STRING ';' ]? // [S|CDO|CDC]* [ import [ CDO S* | CDC S* ]* ]* // [ [ ruleset | media | page | keyframes ] [ CDO S* | CDC S* ]* ]* //// ; if (styleSheet == null) { return(null); } // [ CHARSET_SYM STRING ';' ]? if (!string.IsNullOrWhiteSpace(styleSheet.CharSetString)) { _printerFormatter.Append(CssConstants.Charset); _printerFormatter.Append(styleSheet.CharSetString); _printerFormatter.AppendLine(CssConstants.Semicolon); } // [S|CDO|CDC]* [ import [ CDO S* | CDC S* ]* ]* // Invoke the import visitor styleSheet.Imports.ForEach(importNode => importNode.Accept(this)); // [S|CDO|CDC]* [ namespace [ CDO S* | CDC S* ]* ]* // Invoke the import visitor styleSheet.Namespaces.ForEach(namespaceNode => namespaceNode.Accept(this)); // [ [ ruleset | media | page | keyframes | document ] [S|CDO|CDC]* ]* // Invoke the styleSheetRuleNode visitor styleSheet.StyleSheetRules.ForEach(styleSheetRuleNode => styleSheetRuleNode.Accept(this)); return(styleSheet); }
/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { var updatedStyleSheetRuleNodes = new List <StyleSheetRuleNode>(); styleSheet.StyleSheetRules.ForEach(styleSheetRuleNode => updatedStyleSheetRuleNodes.Add((StyleSheetRuleNode)styleSheetRuleNode.Accept(this))); return(new StyleSheetNode(styleSheet.CharSetString, styleSheet.Imports, styleSheet.Namespaces, updatedStyleSheetRuleNodes.AsReadOnly())); }
private static RulesetNode GetFirstRuleSet(StyleSheetNode stylesheet) { var styleSheetRuleNode = stylesheet.StyleSheetRules.FirstOrDefault() as RulesetNode; Assert.IsNotNull(styleSheetRuleNode); return(styleSheetRuleNode); }
private static DeclarationNode GetFirstDeclaration(StyleSheetNode stylesheet) { var styleSheetRuleNode = GetFirstRuleSet(stylesheet); var declarationNode = styleSheetRuleNode.Declarations.FirstOrDefault(); Assert.IsNotNull(declarationNode); return(declarationNode); }
private static DeclarationNode GetNthDeclaration(StyleSheetNode stylesheet, int index) { var styleSheetRuleNode = GetFirstRuleSet(stylesheet); var declarationNode = styleSheetRuleNode.Declarations[index]; Assert.IsNotNull(declarationNode); return(declarationNode); }
/// <summary>The <see cref="Ast.StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { if (styleSheet == null) { throw new ArgumentNullException("styleSheet"); } return(new StyleSheetNode( styleSheet.CharSetString, styleSheet.Imports, styleSheet.Namespaces, styleSheet.StyleSheetRules.Select( styleSheetRule => (StyleSheetRuleNode)styleSheetRule.Accept(this)).ToSafeReadOnlyCollection())); }
/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { if (styleSheet == null) { return(null); } var ruleSetMediaPageDictionary = this.GetMergedNodeDictionary(styleSheet.StyleSheetRules); // Extract the updated list from dictionary var styleSheetRuleNodes = ruleSetMediaPageDictionary.Values.Cast <StyleSheetRuleNode>().ToList(); // Create a new object with updated rulesets list. return(new StyleSheetNode( styleSheet.CharSetString, styleSheet.Imports, styleSheet.Namespaces, styleSheetRuleNodes.AsSafeReadOnly())); }
/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { if (styleSheet == null) { throw new ArgumentNullException("styleSheet"); } // Note - "@charset" string cannot be verified here as it is analyzed during the parsing. // No need to wrap the exception context here since it would be the beginning of Css. ValidateForLowerCase(styleSheet.CharSetString); // Note - "@import" string cannot be verified here as it is analyzed during the parsing. // No need to wrap the exception context here since it would be the beginning of Css. styleSheet.Imports.ForEach(importNode => ValidateForLowerCase(importNode.MinifyPrint())); // Visit the ruleset, media or page nodes styleSheet.StyleSheetRules.ForEach(styleSheetRule => styleSheetRule.Accept(this)); return(styleSheet); }
public void CloningPerf1() { var cssFile = Path.Combine(TestDeploymentPaths.TestDirectory, @"WebGrease.Tests\CloningTests\landingPage.tmx.pc.ms.css"); var css = File.ReadAllText(cssFile); StyleSheetNode stylesheetNode = null; StyleSheetNode stylesheetNode2 = null; var loopCount = 100; var timer = DateTimeOffset.Now; var webGreaseContext = new WebGreaseContext(new WebGreaseConfiguration()); Parallel.For(0, loopCount, i => { var ss = CssParser.Parse(webGreaseContext, css, false); if (loopCount - 1 == i) { stylesheetNode = ss; } }); var timeSpent1 = DateTimeOffset.Now - timer; timer = DateTimeOffset.Now; Parallel.For(0, loopCount, i => { var ss = stylesheetNode.Accept(new NodeTransformVisitor()) as StyleSheetNode; if (loopCount - 1 == i) { Assert.AreNotEqual(ss, stylesheetNode2); Assert.AreNotEqual(ss, stylesheetNode); stylesheetNode2 = ss; } }); var timeSpent2 = DateTimeOffset.Now - timer; Assert.AreEqual(stylesheetNode2.MinifyPrint(), stylesheetNode.MinifyPrint()); Assert.IsTrue(timeSpent2.TotalMilliseconds < timeSpent1.TotalMilliseconds / 5); Trace.WriteLine(string.Format("For {0} Runs, Parsed: {1}ms, Cloned: {2}ms", loopCount, timeSpent1.TotalMilliseconds, timeSpent2.TotalMilliseconds)); }
/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { if (styleSheet == null) { throw new ArgumentNullException("styleSheet"); } var updatedStyleSheetRules = new List <StyleSheetRuleNode>(); styleSheet.StyleSheetRules.ForEach( ruleSetMediaPageNode => { var updatedRuleSetMediaPageNode = (StyleSheetRuleNode)ruleSetMediaPageNode.Accept(this); // If there is a request for optimization, the ruleset will be null. // Only add the non-null rulesets here. if (updatedRuleSetMediaPageNode != null) { updatedStyleSheetRules.Add(updatedRuleSetMediaPageNode); } }); return(new StyleSheetNode(styleSheet.CharSetString, styleSheet.Imports, styleSheet.Namespaces, updatedStyleSheetRules.AsReadOnly())); }
/// <summary>The <see cref="StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public override AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { _imagesCriteriaFailedReferences.Clear(); styleSheet.StyleSheetRules.ForEach(styleSheetRuleNode => styleSheetRuleNode.Accept(this)); return(styleSheet); }
/// <summary>The <see cref="Ast.StyleSheetNode"/> visit implementation</summary> /// <param name="styleSheet">The styleSheet AST node</param> /// <returns>The modified AST node if modified otherwise the original node</returns> public virtual AstNode VisitStyleSheetNode(StyleSheetNode styleSheet) { return(styleSheet); }
private static void EnsureDeclarationNode(DeclarationNode declarationNode, RulesetNode ruleSetNode, MediaNode mediaQueryNode, StyleSheetNode actualCss) { var rules = actualCss.StyleSheetRules.ToArray(); var expectedMediaQuery = string.Empty; if (mediaQueryNode != null) { expectedMediaQuery = mediaQueryNode.PrintSelector(); rules = rules.OfType <MediaNode>().Where(r => r.PrintSelector().Equals(expectedMediaQuery)).SelectMany(mq => mq.Rulesets).ToArray(); } var expectedRule = ruleSetNode.PrintSelector(); var declarations = rules .OfType <RulesetNode>() .Where(rsn => rsn.PrintSelector().Equals(expectedRule)) .SelectMany(r => r.Declarations).ToArray(); var expectedproperty = declarationNode.Property; var declarationValues = declarations.Where(d => d.Property.Equals(expectedproperty)).ToArray(); var expectedValue = declarationNode.ExprNode.TermNode.MinifyPrint(); if (!declarationValues.Any(d => d.ExprNode.TermNode.MinifyPrint().Equals(expectedValue))) { Assert.Fail("Could not find [{0}] --> [{1}] --> {2}: {3}; ".InvariantFormat(expectedMediaQuery, expectedRule, expectedproperty, expectedValue)); } }
private static void EnsureExpectedCssResult(IEnumerable <StyleSheetRuleNode> styleSheetRuleNodes, StyleSheetNode actualCss, MediaNode mediaQueryNode = null) { foreach (var styleSheetRuleNode in styleSheetRuleNodes) { if (styleSheetRuleNode is MediaNode) { var mediaNode = styleSheetRuleNode as MediaNode; EnsureExpectedCssResult(mediaNode.Rulesets, actualCss, mediaNode); } else if (styleSheetRuleNode is RulesetNode) { var ruleSetNode = styleSheetRuleNode as RulesetNode; foreach (var declarationNode in ruleSetNode.Declarations) { EnsureDeclarationNode(declarationNode, ruleSetNode, mediaQueryNode, actualCss); } } } }