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

Escape characters in CSS content broke css processing #2

Open
kuka-radovan opened this issue Mar 14, 2019 · 12 comments
Open

Escape characters in CSS content broke css processing #2

kuka-radovan opened this issue Mar 14, 2019 · 12 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@kuka-radovan
Copy link

If I add content rule to SCSS file, e.g.:

&::before {
     content: '\2014';
}

Result from scss processing will be empty <style></style> tag.

@kuka-radovan
Copy link
Author

@drdreo Did you have time to look at this issue?

@drdreo
Copy link
Owner

drdreo commented Mar 27, 2019

Thanks for the hint @kuka-radovan . You might as well open a pull request if you want to.

I investigated into it and found out it's the unescaped backslash which seems to set the imported stylesheet to undefined.

Since the output of my loader returns something like:

import {css} from 'lit-element'; 
export default css`div{background-color:#ff0}div::before{content:'\2014'}`;

I don't seem to find a quick solution for this except than writing the css with double backslashes. But that is not satisfying. I thought of programmatically replacing one backslash with two, but that doesn't work either, or at least i haven't found a way to do that successfully.

&::before {
     content: '\\2014';
}

@drdreo drdreo self-assigned this Mar 27, 2019
@drdreo drdreo added the bug Something isn't working label Mar 27, 2019
@kuka-radovan
Copy link
Author

I really would love to, but I am booked out for few weeks and I will need this pretty much. I would appreciate if you could fix that issue. If not, I will look at it after this hectic time. Thank you for your time.

@drdreo drdreo added the help wanted Extra attention is needed label Mar 27, 2019
@drdreo
Copy link
Owner

drdreo commented Mar 29, 2019

After more debugging, i found out it has nothing to do with this loader but lit-element itself.
I filed an issue there lit/lit-element#637

@ryangroth5
Copy link

You could point the finger in either direction. The lit folks don't claim it as their problem either. The fact is that css in the wild will have these escapes. Outputting something like:

import {css} from 'lit-element'; 
export default css`div{background-color:#ff0}div::before{content:'\\2014'}`;

should fix it. Currently I am forking lit-scss-loader to see if I can change it since I think it is less likely to change than lit-element.

@ryangroth5
Copy link

ryangroth5 commented Apr 12, 2019

This is what I came up with. It works. I don't know if it does what you want it to though. It will produce a valid css from a file that is imported with the [0-9] looking escape.

diff --git a/node_modules/lit-scss-loader/src/templateGenerator.js b/node_modules/lit-scss-loader/src/templateGenerator.js
index aef3ce8..70633cd 100644
--- a/node_modules/lit-scss-loader/src/templateGenerator.js
+++ b/node_modules/lit-scss-loader/src/templateGenerator.js
@@ -1,4 +1,6 @@
 module.exports = function (parsedFileContents) {
+
+  parsedFileContents = parsedFileContents.replace(/\\([0-9])/g,'\\\\$1');
   return `
         ${generateCSSImport()}   
         ${createCssExport(parsedFileContents)}
\ No newline at end of file
diff --git a/node_modules/lit-scss-loader/src/templateGenerator.min.js b/node_modules/lit-scss-loader/src/templateGenerator.min.js
index e175459..eda33bd 100644
--- a/node_modules/lit-scss-loader/src/templateGenerator.min.js
+++ b/node_modules/lit-scss-loader/src/templateGenerator.min.js
@@ -1,4 +1,5 @@
 module.exports = function (parsedFileContents) {
+  parsedFileContents = parsedFileContents.replace(/\\([0-9])/g,'\\\\$1');
   return `${generateCSSImport()} ${createCssExport(parsedFileContents)}
     `
 }
\ No newline at end of file

@kuka-radovan
Copy link
Author

I escaped it the same like you, but in custom webpack-loader which I use before lit-scss-loader

@ryangroth5
Copy link

ryangroth5 commented Apr 23, 2019 via email

@kuka-radovan
Copy link
Author

This is it, but it solves our specific case... it is not supposed to be generic solution

module.exports = function(source) {
  const regex = /content: ["|']((?:\\\w+\s*)+)+["|']/gi;

  let match;
  let stripedOutStyle = source;

  while ((match = regex.exec(source)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (match.index === regex.lastIndex) {
      regex.lastIndex++;
    }

    // We need stipeOut each part of content value (e.g. 'content: \2014 \00A0";')
    const stripedMatchValue = match[1]
      .split(" ")
      .map(part => `\\${part}`)
      .join(" ");

    stripedOutStyle = stripedOutStyle.replace(
      match[0],
      `content: "${stripedMatchValue}";`
    );
  }

  return stripedOutStyle;
};

@drdreo
Copy link
Owner

drdreo commented May 17, 2019

Thanks for collaborating. I didn't have the time to dive deeper into this.
My work project now switched from lit to stencil for convenience reasons. Feel free to develop this further.

@margani
Copy link

margani commented Jun 1, 2020

I had this issue when using Bulma sass files, and in Breadcrumb there was couple of content settings like:

    & + li::before
      color: $breadcrumb-item-separator-color
      content: "\0002f"

This caused webpack css processing to fail.
I fixed it by defining this:

$content1: "\0002f"

and

    & + li::before
      color: $breadcrumb-item-separator-color
      content: $content1

@margani
Copy link

margani commented Jun 2, 2020

This can be related to css-loader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants