Skip to content

Missing quotes in url() for data URIs returned from another loader #615

Closed
@edmorley

Description

@edmorley

Hi!

STR:

Using node 8.5.0:

  1. Clone https://github.com/edmorley/css-loader-missing-quotes-testcase
  2. yarn install
  3. yarn build
  4. Inspect dist/styles.bundle.css

Expected:

#inline-data-uri {
  background-image: url("data:image/svg+xml,%3C?xml version='1.0'?%3E %3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Crect x='10' y='10' height='100' width='100' style='stroke:%23ff0000; fill: %230000ff'/%3E %3C/svg%3E");
}

#data-uri-from-loader {
  background-image: url("data:image/svg+xml,%3C?xml version='1.0'?%3E %3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Crect x='10' y='10' height='100' width='100' style='stroke:%23ff0000; fill: %230000ff'/%3E %3C/svg%3E");
}

Actual

#inline-data-uri {
  background-image: url("data:image/svg+xml,%3C?xml version='1.0'?%3E %3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Crect x='10' y='10' height='100' width='100' style='stroke:%23ff0000; fill: %230000ff'/%3E %3C/svg%3E");
}

#data-uri-from-loader {
  background-image: url(data:image/svg+xml,%3C?xml version='1.0'?%3E %3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Crect x='10' y='10' height='100' width='100' style='stroke:%23ff0000; fill: %230000ff'/%3E %3C/svg%3E);
}

ie: Including the data URI inline in the CSS file works fine (thanks to the fix in #15), however when the resource is url('./svg-data-uri.txt') and so processed by another loader (which for simplicity for this testcase is just the raw-loader) the data URI string returned by the loader isn't wrapped in quotes, so is invalid CSS.

Contrast this to html-loader by inspecting dist/index.html, which successfully converts:

<img src="./svg-data-uri.txt">

...to:

<img src="data:image/svg+xml,%3C?xml version='1.0'?%3E %3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Crect x='10' y='10' height='100' width='100' style='stroke:%23ff0000; fill: %230000ff'/%3E %3C/svg%3E">

Due to this issue, svg-url-loader unfortunately defaults to adding extra quotes around the string it returns, so that CSS url() references work. However this then breaks the html-loader (bhovhannes/svg-url-loader#126) and "require() string from JS" use-cases, which require consumers to remember to set the loader's noquotes option to false (eg neutrinojs/neutrino#334).

However I think this is ideally something that should be fixed in css-loader itself, meaning svg-url-loader could stop wrapping everything in quotes, solving the other problems.

Thoughts? :-)

For quick reference, the relevant package versions in the testcase repo's yarn.lock are:
webpack 3.6.0, css-loader 0.28.7, raw-loader 0.5.1, html-loader 0.5.1, extract-text-webpack-plugin 3.3.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions