Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't parse this code, waiting for a long, long time #138

Open
colindcli opened this issue Aug 10, 2022 · 5 comments
Open

Can't parse this code, waiting for a long, long time #138

colindcli opened this issue Aug 10, 2022 · 5 comments

Comments

@colindcli
Copy link

                var str = @"
:root {
    --layout: {
    }
    --layout-horizontal: {
        @apply (--layout);
    }
}";
                var parser = new StylesheetParser();
                //Can't parse this code, waiting for a long, long time
                var parse = parser.Parse(str);
@colindcli
Copy link
Author

Another piece of code has the same problem

                var str = @"
@media (max-width: 991px) {
    body {
        background-color: #013668;
    }
    ;
}

@media (max-width: 991px) {
    body {
        background: #FFF;
    }
}";

@colindcli
Copy link
Author

Temporarily use a simple solution to solve this bug
fb89279#diff-7baec944edc72b3a373cea418d177eb296a1662f54a80b5de1e626441e5cd55b

@thabing
Copy link

thabing commented Mar 23, 2023

Here is another example (simplified from a much larger CSS) I just ran into:

    var str = @"      
    h3 {color: yellow; 
    @media print {
        h3 {color: black; }
        }        
    ";

Granted, this is malformed because of the missing } after the first selector, but I wouldn't expect an endless loop. It seems to manifest when the following structure has nested braces, like the @media selector. For example, this seems to work just fine even with the missing brace:

    var str = @"      
    h3 {color: yellow; 
    h2 {color: black; }
    ";

My use case is processing crappy HTML embedded in old emails, so I have no choice but to try and deal with this malformed junk. I'm going to try using the ParseAsync version, so I can bail after a few milliseconds if it seems to be stuck.


Well, this is disappointing, the ParseAsync gets hung up just like the Parse Function:

        var str = @"
          h3 {color: yellow; 
          @media print {
              h3 {color: black; }
              }
           ";
          
            var parser = new StylesheetParser();
            var stylesheetTask = parser.ParseAsync(str);
            stylesheetTask.Wait(700);
            var stylesheet = stylesheetTask.Result;

            Assert.IsNotNull(stylesheet);

For anyone else who just needs to get past this, log the error and keep going, here is a (possibly very bad) way:

        var parser = new StylesheetParser();

        var questionableCSS = @"
            h3 {color: yellow;
            @media print {
                h3 {color: black; }
                }
            ";

        var stylesheetTask = Task.Run<Stylesheet>(() => parser.Parse(questionableCSS ));
        var done = stylesheetTask.Wait(700);
        if (done)
            {
            var stylesheet = stylesheetTask.Result;
            //process the CSS like normal
            }
         else
             {
             // log the error, skip the bad data, and move onto the next thing
             }

My concern with the above is that after the timeout, the Parse task will continue to run in the background forever until your app stops, so use caution, especially if many instances of this task might be spun up and never finish .....

@TylerBrinks
Copy link
Owner

It shouldn't run forever given you only have so many stack frames before the process will overflow. Regardless, I'll look at whether the depth guard can be used in this scenario.

@inforithmics
Copy link
Contributor

Made a Pull Request that Fixes this Because I experienced it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants