Ejemplo n.º 1
0
        public void ParseSheet_Simple_TextAlign()
        {
            var loader = new CssLoader();
            var css    = loader.ParseSheet("h1 { text-align: right; }", SheetUri, BaseUri);

            var h1textalign = css.StyleRules.Single().Declaration.Data.Single();

            Assert.AreEqual(CssProperty.TextAlign, h1textalign.Property);
            Assert.AreEqual(CssStyle.TextAlignRight, h1textalign.Value.Int);
        }
Ejemplo n.º 2
0
        public void ParseSheet_Simple_ColorName()
        {
            var loader = new CssLoader();
            var css    = loader.ParseSheet("h1 { color: red; }", SheetUri, BaseUri);

            var h1color = css.StyleRules.Single().Declaration.Data.Single();

            Assert.AreEqual(CssProperty.Color, h1color.Property);
            Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), CssColor.ColorNameToRGB(h1color.Value.String));
            Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), h1color.Value.ComputedColor);
        }
Ejemplo n.º 3
0
        public void TestBadCss()
        {
            CssLoader Parser = new CssLoader();

            foreach (string Filename in CssBadDocs.Keys)
            {
                string        CssData    = CssBadDocs[Filename];
                CssStyleSheet Stylesheet = Parser.ParseSheet(CssData, new Uri("https://nazuke.github.io/dummy.css"), new Uri("https://nazuke.github.io/dummy.css"));

                Assert.IsNotNull(Stylesheet, string.Format("FAIL: {0}", Filename));
            }
        }
Ejemplo n.º 4
0
        public void Run(String source)
        {
            var parser = new CssLoader {
                Compatibility = Alba.CsCss.BrowserCompatibility.FullStandards
            };

            try
            {
                parser.ParseSheet(source, new Uri("http://localhost/foo.css"), new Uri("http://localhost"));
            }
            catch
            {
                // May crash otherwise if invalid input detected ...
            }
        }
Ejemplo n.º 5
0
        public void ParseSheet_GetUris_StackOverflow18262390()
        {
            var           loader = new CssLoader();
            var           css    = loader.ParseSheet(CssStackOverflow18262390, SheetUri, BaseUri);
            List <string> uris   = css.AllStyleRules
                                   .SelectMany(styleRule => styleRule.Declaration.AllData)
                                   .SelectMany(prop => prop.Value.Unit == CssUnit.List ? prop.Value.List : new[] { prop.Value })
                                   .Where(value => value.Unit == CssUnit.Url)
                                   .Select(value => value.OriginalUri)
                                   .ToList();

            CollectionAssert.AreEqual(
                new[] { "img0", "img1", "img2", "img3", "img4", "img5", "img6", "img7" },
                uris);
        }
Ejemplo n.º 6
0
        public void ParseSheet_ParseErrorEvent()
        {
            int nErrors = 0;
            var loader  = new CssLoader();

            loader.ParseError += (o, a) => {
                Assert.AreEqual(SheetUri, a.Uri);
                Assert.AreEqual("b {*}", a.Line);
                Assert.AreEqual(1, a.LineNumber);
                Assert.AreEqual(3, a.ColumnNumber);
                Assert.IsTrue(a.Message.Contains("found '*'"));
                Assert.IsTrue(a.Message.Contains("Skipped to next"));
                nErrors++;
            };
            loader.ParseSheet("a {}\nb {*}\nc {}", SheetUri, BaseUri);
            Assert.AreEqual(1, nErrors);
        }
Ejemplo n.º 7
0
        public void ParseSheet_Simple_ColorHex()
        {
            var loader = new CssLoader();
            var css    = loader.ParseSheet("h1 { color: #123; }", SheetUri, BaseUri);

            Assert.AreEqual(SheetUri, css.SheetUri);
            Assert.AreEqual(BaseUri, css.BaseUri);
            Assert.AreEqual(1, css.Rules.Count());
            Assert.AreEqual(1, css.AllRules.Count());
            Assert.AreEqual(1, css.StyleRules.Count());
            Assert.AreEqual(1, css.AllStyleRules.Count());
            var h1    = css.StyleRules.Single();
            var h1sel = h1.SelectorGroups.Single().Selectors.Single();

            Assert.AreEqual("h1", h1sel.Tag);
            var h1color = h1.Declaration.Data.Single();

            Assert.AreEqual(CssProperty.Color, h1color.Property);
            Assert.AreEqual(CssColor.RGB(0x11, 0x22, 0x33), h1color.Value.Color);
            Assert.AreEqual(CssColor.RGB(0x11, 0x22, 0x33), css.StyleRules.Single().Declaration.Color.Color);
        }
Ejemplo n.º 8
0
 public void ParseSheet_TwitterBootstrap()
 {
     var loader = new CssLoader();
     var css    = loader.ParseSheet(CssTwitterBootstrap, SheetUri, SheetUri);
     //var q = css.AllStyleRules.Where(r => r.Declaration.BackgroundImage.GetUnit() != CssUnit.Null).Select(r => r.Declaration.BackgroundImage);
 }
        /** -------------------------------------------------------------------- **/

        private async Task _ProcessCssPage()
        {
            MacroscopeHttpTwoClient         Client   = this.DocCollection.GetJobMaster().GetHttpClient();
            MacroscopeHttpTwoClientResponse Response = null;
            string ResponseErrorCondition            = null;

            DebugMsg(string.Format("ProcessCssPage: {0}", ""));

            try
            {
                Response = await Client.Get(
                    this.GetUri(),
                    this.ConfigureCssPageRequestHeadersCallback,
                    this.PostProcessRequestHttpHeadersCallback
                    );
            }
            catch (MacroscopeDocumentException ex)
            {
                this.DebugMsg(string.Format("_ProcessCssPage :: MacroscopeDocumentException: {0}", ex.Message));
                ResponseErrorCondition = ex.Message;
                this.SetStatusCode(HttpStatusCode.BadRequest);
                this.AddRemark("_ProcessCssPage", ex.Message);
            }
            catch (Exception ex)
            {
                this.DebugMsg(string.Format("_ProcessCssPage :: Exception: {0}", ex.Message));
                ResponseErrorCondition = ex.Message;
                this.SetStatusCode(HttpStatusCode.BadRequest);
                this.AddRemark("_ProcessCssPage", ex.Message);
            }

            /** Set Base URL ----------------------------------------------------- **/
            if (Response != null)
            {
                Uri    CssUri       = this.GetUri();
                string CssLocalPath = CssUri.LocalPath;
                string CssPath      = System.IO.Path.GetDirectoryName(CssLocalPath);
                Uri    NewCssUri    = new Uri(new UriBuilder(scheme: CssUri.Scheme, host: CssUri.Host, port: CssUri.Port, pathValue: CssPath).ToString());
                this.SetBaseHref(NewCssUri.ToString());
            }

            /** Process The Response Body ---------------------------------------- **/

            if (Response != null)
            {
                string RawData = "";

                this.ProcessResponseHttpHeaders(Response: Response);

                /** Get Response Body ---------------------------------------------- **/

                try
                {
                    DebugMsg(string.Format("MIME TYPE: {0}", this.MimeType));

                    RawData = Response.GetContentAsString();

                    this.SetContentLength(Length: RawData.Length); // May need to find bytes length

                    this.SetWasDownloaded(true);
                }
                catch (Exception ex)
                {
                    DebugMsg(string.Format("Exception: {0}", ex.Message));
                    this.SetStatusCode(HttpStatusCode.Ambiguous);
                    this.SetContentLength(Length: 0);
                }

                if (!string.IsNullOrEmpty(RawData))
                {
                    try
                    {
                        CssLoader     Parser     = new CssLoader();
                        CssStyleSheet Stylesheet = Parser.ParseSheet(RawData, this.GetUri(), this.GetUri());
                        this.ProcessCssOutlinks(Stylesheet: Stylesheet);
                    }
                    catch (Exception ex)
                    {
                        this.DebugMsg(string.Format("ProcessHtmlAttributeCssLinks: {0}", ex.Message));
                        this.AddRemark("ProcessHtmlAttributeCssLinks", ex.Message);
                    }
                }
                else
                {
                    DebugMsg(string.Format("ProcessCssPage: ERROR: {0}", this.GetUrl()));
                }

                /** Custom Filters ------------------------------------------------- **/

                if (!string.IsNullOrEmpty(RawData))
                {
                    if (
                        MacroscopePreferencesManager.GetCustomFiltersEnable() &&
                        MacroscopePreferencesManager.GetCustomFiltersApplyToCss())
                    {
                        MacroscopeCustomFilters CustomFilter = this.DocCollection.GetJobMaster().GetCustomFilter();

                        if ((CustomFilter != null) && (CustomFilter.IsEnabled()))
                        {
                            this.ProcessGenericCustomFiltered(
                                CustomFilter: CustomFilter,
                                GenericText: RawData
                                );
                        }
                    }
                }

                /** Data Extractors ------------------------------------------------ **/

                if (!string.IsNullOrEmpty(RawData))
                {
                    if (
                        MacroscopePreferencesManager.GetDataExtractorsEnable() &&
                        MacroscopePreferencesManager.GetDataExtractorsApplyToCss())
                    {
                        this.ProcessGenericDataExtractors(GenericText: RawData);
                    }
                }

                /** Title ---------------------------------------------------------- **/

                {
                    MatchCollection reMatches     = Regex.Matches(this.DocUrl, "/([^/]+)$");
                    string          DocumentTitle = null;
                    foreach (Match match in reMatches)
                    {
                        if (match.Groups[1].Value.Length > 0)
                        {
                            DocumentTitle = match.Groups[1].Value.ToString();
                            break;
                        }
                    }
                    if (DocumentTitle != null)
                    {
                        this.SetTitle(DocumentTitle, MacroscopeConstants.TextProcessingMode.NO_PROCESSING);
                        DebugMsg(string.Format("TITLE: {0}", this.GetTitle()));
                    }
                    else
                    {
                        DebugMsg(string.Format("TITLE: {0}", "MISSING"));
                    }
                }
            }

            if (ResponseErrorCondition != null)
            {
                this.ProcessErrorCondition(ResponseErrorCondition);
            }
        }