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

Function toCSS generates broken CSS when semicolon in attribute value - e.g. @font-face with src attribute with url( ; ) #42

Open
igolovic opened this issue Apr 1, 2022 · 1 comment

Comments

@igolovic
Copy link

igolovic commented Apr 1, 2022

In case when semicolon is a part of CSS code, and CSS will be converted to JSON object, and JSON object converted back to CSS, the resulting CSS will be cut off after the semicolon.

Error reproduction example, if CSS is:

src: url("data:application/octet-stream;base64,d09GRgABAAAAAAnwAA4AAA [SHORTENED] zAju/YGBgXX2kwJFwDEASgaAAAA") format("truetype");

after conversion to JSON and back to CSS, resulting CSS string will be:

src: url("data:application/octet-stream;

Example for Visual Studio Code with added cssjson npm package:

<html>

<head>
</head>

<body>
    <script src="node_modules/cssjson/cssjson.js"></script>

    <script type="module">
        //import cssjson from cssjson
        // import { cssjson } from './node_modules/cssjson/cssjson.js';

        var cssString = `@media (max-width: 800px) {
            @font-face {
                font-family: "FabricMDL2Icons";
                src: url("data:application/octet-stream;base64,d09GRgABAAAAAAnwAA4AAAAAEmQAAwo9AAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAABRAAAAEgAAABgMvF9DmNtYXAAAAGMAAAATgAAAWrTEbXwY3Z0IAAAAdwAAAAgAAAAKgnZCa9mcGdtAAAB/AAAAPAAAAFZ/J7mjmdhc3AAAALsAAAADAAAAAwACAAbZ2x5ZgAAAvgAAAHOAAACiGs9SAVoZWFkAAAEyAAAADIAAAA2/s8J3mhoZWEAAAT8AAAAFQAAACQQAQgDaG10eAAABRQAAAASAAAAEg2qAKZsb2NhAAAFKAAAABAAAAAQAbwCtm1heHAAAAU4AAAAHgAAACAAewHFbmFtZQAABVgAAAP3AAAJ+oyT8U5wb3N0AAAJUAAAABQAAAAg/1EAfnByZXAAAAlkAAAAiQAAANN4vfIOeJxjYGGfwTiBgZWBg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XpHig7pgO7RQ6BjMlSQJ0sB/wVFyJ4Aedx5XnNIDFBDGU6OqMaj6ASyipY4C7LTeGtov8FaQvMZ7ObYs2+O8RRflkOxD6LJdmxVUQt/OTSvY3EdnVPJI6ScFb50GWp9cMl2sLxo90KysVgVdLiqHP8oDJSQriTL15AxS3yyFN1N/FWyb8HgoF3gvcI+R05G2Mr+BfPMc4T0Ec3wrOXJ4O99+yzFmQF34qUSPw4eOkgX4o2rnW2MHiRnh44Y6aM6s+Dev5KalDDR4d0Kdz0TPWODNsusVO2hwXVommJfip6TjnciYT44jpPO9LZF8qLTPhffTvrKNUc5Y6u55DF0opaK2GrIq7cI0gV/RbI4q2F6ra462ZewKbCfCl/9zPdxp2dx3q/AyCSuhacC62bO1qlS1i5QTStzV27knm1qQXvQv4s3T+g88bLJe5/Df+X23HspnirIvMxxTHdqmNVNFQzRr+b1+MIMcCV9LVHiDbeA/fe1lpCspXIrt/JDs5dfmiotfbFp7avqcSs3qxVLznbo5uCHNWu5yf88o/2XsUmdOfc+3BCTWOb54XznwnTf2//hbv8NDzY4iAB4nGNgZgCD/34M5QyYgB0AKTEBz3ic28CgzbCJkZNJm3ETF4jcztWaG2qrysChvZ07NdhBTwbE4onwsNCQBLF4nc215YVBLD4dFRkRHhCLX05CmI8DxBLg4+FkZwGxBMEAxBLaMKEgwADIYtjOCDeaCW40M9xoFrjRrHCj2eQkoUazw43mgBvNCTd6kzAju/YGBgXX2kwJFwDEASgaAAAA") 
                format("truetype");
            }
        }`;

        // To JSON
        const jsonObject = CSSJSON.toJSON(cssString);

        // Resulting CSS is cut off at semicolon
        const css = CSSJSON.toCSS(jsonObject);
        debugger;

    </script>
</body>

</html>
@igolovic
Copy link
Author

I created a fix for this by allowing CSS's url "function" , these are the changes I did to get this problem fixed.

        // Adding special handling for CSS function "url()" ( and format() if it follows url() ) - if detected, 
        // everything inside it (colons, semicolons etc.) should be treated as attribute value and not as delimiters.
        // Changed are lineAttrX and altX regex, regex should also cover 
        // case when single CSS attribute breaks into more than one line.

I couldn't fork this project and create fix in library but here are steps anyone can replicate in library to fix their downloaded copy:

1 - In un-minified version find:

var lineAttrX = /([^\:]+):([^\;]*);/;

this should be replaced by:

var lineAttrX = /([^\:]+):(\s*url\([\s\S]+?\)|[^\;]*);/;

2 - In un-minified version find:

var altX = /(\/\*[\s\S]*?\*\/)|([^\s\;\{\}][^\;\{\}]*(?=\{))|(\})|([^\;\{\}]+\;(?!\s*\*\/))/gmi;

this should be replaced by:

var altX = /(\/\*[\s\S]*?\*\/)|([^\s\;\{\}][^\;\{\}]*(?=\{))|(\})|(\s*.*\:\s*url\([\s\S]+?\)\;(?!\s*\*\/)|[^\;\{\}]+\;(?!\s*\*\/))/gmi;

Version of library with replacements 1 and 2 should handle url() function correctly even if it contains semicolons, also if url() is followed by CSS function format() - this too will be included.
Also, if url() is in one line, and format() is in the line below, this too should be handled correctly.

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

1 participant