greasyfork-org / greasyfork

An online repository of user scripts.
https://greasyfork.org
GNU General Public License v3.0
1.41k stars 422 forks source link

Broken style to script conversion when there is comment before document block after global part #1245

Closed myfonj closed 4 months ago

myfonj commented 4 months ago

The global part seems to be completely omitted from JS version of userstyle when there is a comment before @-moz-document block at the end of the "global" part.

Test: https://greasyfork.org/en/scripts/486968-test-style-for-debugging-conversion-to-userscript/code?version=1325339

https://update.greasyfork.org/scripts/486968/1325339/Test%20style%20for%20debugging%20conversion%20to%20userscript.user.css :

...

/**/
el { color: green; }

/**/
@-moz-document domain("facebook.com") { el { color: red; } }

produces https://update.greasyfork.org/scripts/486968/1325339/Test%20style%20for%20debugging%20conversion%20to%20userscript.user.js :

...
// @include http://facebook.com/*
// @include https://facebook.com/*
// @include http://*.facebook.com/*
// @include https://*.facebook.com/*
// @downloadURL none
// ==/UserScript==

(function() {
let css = ` el { color: red; } `;
...

NB there is no el { color: green; } whatsoever.

While https://greasyfork.org/en/scripts/486968-test-style-for-debugging-conversion-to-userscript/code?version=1325324

https://update.greasyfork.org/scripts/486968/1325324/Test%20style%20for%20debugging%20conversion%20to%20userscript.user.css

...

/**/
el { color: green; }

@-moz-document domain("facebook.com") { /**/ el { color: red; } }

produces

...
// @include *
// @downloadURL none
// ==/UserScript==

(function() {
let css = "";
css += `

/**/

el { color: green; }

`;
if ((location.hostname === "facebook.com" || location.hostname.endsWith(".facebook.com"))) {
  css += ` /**/ el { color: red; } `;
}
...

What is OK.

Got couple of bugreports for https://greasyfork.org/en/scripts/408378-unround-everything-everywhere/feedback when hit this problem.

JasonBarnabe commented 4 months ago

Thanks for the minimal reproduction case. It appears as though it thinks the the portion outside of the @-moz-document consists entirely of comments. This is because it starts with /* and ends with */ (ignoring whitespace). The CSS parser is not very sophisticated in this way, but I think I can fix this case.

JasonBarnabe commented 4 months ago

I've put in a fix; let me know if this is still an issue.

Note the contents of version-specific code URLs are cached for 24 hours, so https://update.greasyfork.org/scripts/486968/1325339/Test%20style%20for%20debugging%20conversion%20to%20userscript.user.js may not show the fix until tomorrow.