internal static CssStyleDeclaration ParseDeclarations(String declarations)
 {
     var parser = new CssParser();
     var style = new CssStyleDeclaration(parser);
     style.Update(declarations);
     return style;
 }
Example #2
0
 /// <summary>
 /// Sets a new default stylesheet defined by the provided string.
 /// </summary>
 /// <param name="sourceCode">The source for a new base stylesheet.</param>
 /// <returns>The CSSOM of the parsed source.</returns>
 public ICssStyleSheet SetDefault(String sourceCode)
 {
     var parser = new CssParser(_options, Configuration.Default);
     var sheet = parser.ParseStylesheet(sourceCode);
     _default = sheet;
     return sheet;
 }
Example #3
0
 static CssImportRule NewImportRule(String cssText)
 {
     var parser = new CssParser();
     var rule = new CssImportRule(parser);
     rule.CssText = cssText;
     return rule;
 }
Example #4
0
 /// <summary>
 /// Creates a new unknown rule.
 /// </summary>
 public CssUnknownRule(String name, CssParser parser)
     : base(CssRuleType.Unknown, parser)
 {
     _name = name;
     _prelude = String.Empty;
     _content = String.Empty;
 }
Example #5
0
 internal CssStyleSheet(CssParser parser, String url, IElement owner)
 {
     _media = new MediaList(parser);
     _owner = owner;
     _url = url;
     _rules = new CssRuleList(this);
     _parser = parser;
 }
Example #6
0
 /// <summary>
 /// Sets a new default stylesheet defined by the provided string.
 /// </summary>
 /// <param name="sourceCode">The source for a new base stylesheet.</param>
 /// <returns>The CSSOM of the parsed source.</returns>
 public ICssStyleSheet SetDefault(String sourceCode)
 {
     var parser = new CssParser(_options);
     var source = new TextSource(sourceCode);
     var sheet = new CssStyleSheet(parser, default(String), default(ICssStyleSheet));
     _default = Parse(parser, sheet, source).Result;
     return _default;
 }
Example #7
0
 static ICssStyleSheet TestCss(String source, String title = "CSS document")
 {
     var parser = new CssParser();
     var sw = Stopwatch.StartNew();
     var sheet = parser.ParseStylesheet(source);
     sw.Stop();
     Console.WriteLine("Parsing " + title + " took " + sw.ElapsedMilliseconds + "ms");
     return sheet;
 }
Example #8
0
        static void CreateCssSelectorTest(IBrowsingContext context, String url, List<String> methods)
        {
            Console.Write("Loading " + url + " ... ");
            var document = context.OpenAsync(url).Result;
            var title = Sanatize(document.GetElementsByTagName("title")[0].TextContent);
            var content = document.GetElementsByTagName("content")[0].InnerHtml.Trim().Replace("\"", "\"\"");
            var styling = document.GetElementsByTagName("css")[0].TextContent;
            var parser = new CssParser();
            var sheet = parser.ParseStylesheet(styling);
            var selectors = new StringBuilder();
            var i = 1;

            if (methods.Contains(title))
            {
                var ltr = 'A';

                while (methods.Contains(title + ltr))
                    ltr = (Char)(ltr + 1);

                title += ltr.ToString();
            }

            foreach (var rule in sheet.Rules)
            {
                if (rule is ICssStyleRule)
                {
                    selectors.Append(@"
	        var selectorINDEX = doc.QuerySelectorAll(""SELECTOR"");
	        Assert.AreEqual(0, selectorINDEX.Length);"
                .Replace("SELECTOR", ((ICssStyleRule)rule).SelectorText)
                .Replace("INDEX", i.ToString()));
                    i++;
                }
            }

            File.AppendAllText("test.cs", @"
        /// <summary>
        /// Test taken from URL
        /// </summary>
        public void TITLE()
        {
	        var source = @""HTML"";
	        var doc = DocumentBuilder.Html(source);
	        SELECTORS
        }
"
            .Replace("URL", url)
            .Replace("TITLE", title)
            .Replace("HTML", content)
            .Replace("SELECTORS", selectors.ToString())
            );
            Console.WriteLine("success.");
            methods.Add(title);
        }
Example #9
0
		/// <summary>
		/// Applies a CSS stylesheet to the current App
		/// <para xml:lang="es">
		/// Aplica una hoja de estilo css para la aplicacion actual.
		/// </para>
		/// </summary>
		/// <param name="styleSheet">A list of css rules to be applied to the current running App
		/// <para xml:lang="es">Una lista de reglas css que se aplicaran a la aplicacion actual en ejecucion.</para>
		/// </param>
		public static void ParseStyleRules(string styleSheet)
		{
			CssParser parser = new CssParser();
			ICssStyleSheet cssStylesSheet = parser.ParseStylesheet(styleSheet);

			//get only the rules that are actually styles
			foreach (ICssStyleRule rule in cssStylesSheet.Rules.Where(rule => rule.Type == CssRuleType.Style))
			{
				ParsedStyleRules.Add(rule);
			}
		}
Example #10
0
 public void TestAsyncCssParsing()
 {
     var source = "h1 { color: red; } h2 { color: blue; } p { font-family: Arial; } div { margin: 10 }";
     var parser = new CssParser(source, Configuration.Default);
     var task = parser.ParseAsync();
     Assert.IsFalse(task.IsCompleted);
     Assert.IsNotNull(parser.Result);
     Assert.IsFalse(task.IsCompleted);
     task.Wait();
     Assert.IsTrue(task.IsCompleted);
     Assert.IsNotNull(parser.Result);
     Assert.AreEqual(4, parser.Result.Rules.Length);
 }
Example #11
0
        public async Task TestAsyncCssParsingFromString()
        {
            var source = "h1 { color: red; } h2 { color: blue; } p { font-family: Arial; } div { margin: 10 }";
            var parser = new CssParser(Configuration.Default);

            using (var task = parser.ParseStylesheetAsync(source))
            {
                Assert.IsTrue(task.IsCompleted);
                var result = await task;

                Assert.AreEqual(result, result);
                Assert.AreEqual(4, result.Rules.Length);
            }
        }
Example #12
0
 public void CssStyleSheetComplexRoundtrip()
 {
     var parser = new CssParser(new CssParserOptions
     {
         IsStoringTrivia = true,
         IsToleratingInvalidValues = true
     });
     var source = CssStyleEngine.DefaultSource.Replace(Environment.NewLine, "\n").Replace("\\A", "\\a").Replace("'", "\"");
     var sheet = parser.ParseStylesheet(source);
     var roundtrip = sheet.ParseTree.GetSource();
     Assert.AreEqual(source, roundtrip);
 }
Example #13
0
 public void CssStyleSheetSimpleRoundtrip()
 {
     var parser = new CssParser(new CssParserOptions
     {
         IsStoringTrivia = true,
         IsToleratingInvalidValues = true
     });
     var source = ".foo { color: red; } @media all /*test*/ and /*test*/ (min-width: 701px) /*test*/ { #myid { color: green; } }";
     var sheet = parser.ParseStylesheet(source);
     var roundtrip = sheet.ParseTree.GetSource();
     Assert.AreEqual(source, roundtrip);
 }
Example #14
0
        public void CssStyleSheetWithCommentInMedia()
        {
            var parser = new CssParser(new CssParserOptions
            {
                IsStoringTrivia = true
            });
            var source = ".foo { color: red; } @media all /*test*/ and /*test*/ (min-width: 701px) /*test*/ { #myid { color: green; } }";
            var sheet = parser.ParseStylesheet(source);
            var comments = sheet.ParseTree.GetComments();
            Assert.AreEqual(3, comments.Count());

            foreach (var comment in comments)
                Assert.AreEqual("test", comment.Text);
        }
Example #15
0
 /// <summary>
 /// Creates a new CSS style rule.
 /// </summary>
 internal CssStyleRule(CssParser parser)
     : base(CssRuleType.Style, parser)
 {
     _style = new CssStyleDeclaration(this);
     _selector = SimpleSelector.All;
 }
Example #16
0
 public CssUnknownRule(String name, CssParser parser)
     : base(CssRuleType.Unknown, parser)
 {
     _name = name;
 }
Example #17
0
 /// <summary>
 /// Creates a style sheet for the given response from a request.
 /// </summary>
 /// <param name="response">
 /// The response with the stream representing the source of the
 /// stylesheet.
 /// </param>
 /// <param name="options">
 /// The options with the parameters for evaluating the style.
 /// </param>
 /// <returns>The created style sheet.</returns>
 public IStyleSheet ParseStylesheet(IResponse response, StyleOptions options)
 {
     var parser = new CssParser(_options, options.Configuration);
     var sheet = new CssStyleSheet(parser, response.Address.Href, options.Element) 
     { 
         IsDisabled = options.IsDisabled
     };
     var source = new TextSource(response.Content);
     return Parse(parser, sheet, source).Result;
 }
Example #18
0
 internal CssDeclarationRule(CssRuleType type, String name, CssParser parser)
     : base(type, parser)
 {
     _name = name;
 }
Example #19
0
 public void CssStyleSheetSelectorsGetAll()
 {
     var parser = new CssParser(new CssParserOptions
     {
         IsStoringTrivia = true
     });
     var source = ".foo { } #bar { } @media all { div { } a > b { } @media print { script[type] { } } }";
     var sheet = parser.ParseStylesheet(source);
     var roundtrip = sheet.ParseTree.GetSource();
     Assert.AreEqual(source, roundtrip);
     var selectors = sheet.ParseTree.GetAll<ISelector>();
     Assert.AreEqual(5, selectors.Count());
     var mediaRules = sheet.ParseTree.GetAll<ICssMediaRule>();
     Assert.AreEqual(2, mediaRules.Count());
     var descendentSelector = selectors.Skip(3).First();
     Assert.AreEqual("a>b", descendentSelector.Text);
     var selectorNode = sheet.ParseTree.GetAssociatedNode(descendentSelector);
     Assert.AreEqual("a > b ", selectorNode.GetSource());
 }
Example #20
0
 public void CssStyleSheetWithoutCommentsButStoringTrivia()
 {
     var parser = new CssParser(new CssParserOptions
     {
         IsStoringTrivia = true
     });
     var source = ".foo { color: red; } @media print { #myid { color: green; } }";
     var sheet = parser.ParseStylesheet(source);
     var comments = sheet.ParseTree.GetComments();
     Assert.AreEqual(0, comments.Count());
 }
Example #21
0
        public void CssStyleSheetShouldIgnoreHtmlCommentTokens()
        {
            var parser = new CssParser();
            var source = "<!-- body { font-family: Verdana } div.hidden { display: none } -->";
            var sheet = parser.ParseStylesheet(source);
            Assert.AreEqual(2, sheet.Rules.Length);

            Assert.AreEqual(CssRuleType.Style, sheet.Rules[0].Type);
            var body = sheet.Rules[0] as ICssStyleRule;
            Assert.AreEqual("body", body.SelectorText);
            Assert.AreEqual(1, body.Style.Length);
            Assert.AreEqual("Verdana", body.Style.FontFamily);

            Assert.AreEqual(CssRuleType.Style, sheet.Rules[1].Type);
            var div = sheet.Rules[1] as ICssStyleRule;
            Assert.AreEqual("div.hidden", div.SelectorText);
            Assert.AreEqual(1, div.Style.Length);
            Assert.AreEqual("none", div.Style.Display);
        }
Example #22
0
        async Task<CssStyleSheet> Parse(CssParser parser, CssStyleSheet sheet, TextSource source)
        {
            var evt = new CssParseStartEvent(sheet);
            var events = parser.Config.Events;

            if (events != null)
                events.Publish(evt);

            await parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false);
            evt.FireEnd();
            return sheet;
        }
Example #23
0
 /// <summary>
 /// Creates a media list for the given source.
 /// </summary>
 /// <param name="source">The media source.</param>
 /// <param name="options">
 /// The options with the parameters for evaluating the style.
 /// </param>
 /// <returns>The created media list.</returns>
 public IMediaList ParseMedia(String source, StyleOptions options)
 {
     var parser = new CssParser(_options, options.Configuration);
     var media = new MediaList(parser);
     media.MediaText = source;
     return media;
 }
Example #24
0
 /// <summary>
 /// Creates a style declaration for the given source.
 /// </summary>
 /// <param name="source">
 /// The source code for the inline style declaration.
 /// </param>
 /// <param name="options">
 /// The options with the parameters for evaluating the style.
 /// </param>
 /// <returns>The created style declaration.</returns>
 public ICssStyleDeclaration ParseInline(String source, StyleOptions options)
 {
     var parser = new CssParser(_options, options.Configuration);
     var style = new CssStyleDeclaration(parser);
     style.Update(source);
     return style;
 }
Example #25
0
        public void CssStyleSheetInsertAndDeleteShouldWork()
        {
            var parser = new CssParser();
		    var s = new CssStyleSheet(parser);
            Assert.AreEqual(0, s.Rules.Length);
            
            s.Insert("a {color: blue}", 0);
            Assert.AreEqual(1, s.Rules.Length);
            
            s.Insert("a *:first-child, a img {border: none}", 1);
            Assert.AreEqual(2, s.Rules.Length);

            s.RemoveAt(1);
            Assert.AreEqual(1, s.Rules.Length);

            s.RemoveAt(0);
            Assert.AreEqual(0, s.Rules.Length);
        }
Example #26
0
 /// <summary>
 /// Creates a new @keyframes rule.
 /// </summary>
 internal CssKeyframesRule(CssParser parser)
     : base(CssRuleType.Keyframes, parser)
 {
     _rules = new CssRuleList();
 }
Example #27
0
 public void CssStyleSheetInsertShouldSetParentStyleSheetCorrectly()
 {
     var parser = new CssParser();
     var s = new CssStyleSheet(parser);
     s.Insert("a {color: blue}", 0);
     Assert.AreEqual(s, s.Rules[0].Owner);
 }
Example #28
0
 /// <summary>
 /// Creates a style sheet for the given source.
 /// </summary>
 /// <param name="sourceCode">
 /// The source code describing the style sheet.
 /// </param>
 /// <param name="options">
 /// The options with the parameters for evaluating the style.
 /// </param>
 /// <returns>The created style sheet.</returns>
 public IStyleSheet ParseStylesheet(String sourceCode, StyleOptions options)
 {
     var parser = new CssParser(_options, options.Configuration);
     var sheet = new CssStyleSheet(parser, default(String), options.Element)
     {
         IsDisabled = options.IsDisabled
     };
     var source = new TextSource(sourceCode);
     return Parse(parser, sheet, source).Result;
 }
Example #29
0
        public void CssStyleSheetWithCommentInRule()
        {
            var parser = new CssParser(new CssParserOptions
            {
                IsStoringTrivia = true
            });
            var source = ".foo { color: red; } @media print { /*test*/ #myid { color: green; } /*test*/ }";
            var sheet = parser.ParseStylesheet(source);
            var comments = sheet.ParseTree.GetComments();
            Assert.AreEqual(2, comments.Count());

            foreach (var comment in comments)
                Assert.AreEqual("test", comment.Text);
        }
Example #30
0
 /// <summary>
 /// Creates a new CSS import rule
 /// </summary>
 internal CssImportRule(CssParser parser)
     : base(CssRuleType.Import, parser)
 {
     _media = new MediaList(parser);
 }