Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
allow for underscores in url()
  • Loading branch information
RobinMalfait committed Oct 22, 2021
commit ccc39661c85863cbdbbf4cca162a38afbaf3da2b
2 changes: 2 additions & 0 deletions src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const PATTERNS = [
/([^<>"'`\s]*\[\w*"[^"`\s]*"?\])/.source, // font-["some_font",sans-serif]
/([^<>"'`\s]*\[\w*\('[^"'`\s]*'\)\])/.source, // bg-[url('...')]
/([^<>"'`\s]*\[\w*\("[^"'`\s]*"\)\])/.source, // bg-[url("...")]
/([^<>"'`\s]*\[\w*\('[^"`\s]*'\)\])/.source, // bg-[url('...'),url('...')]
/([^<>"'`\s]*\[\w*\("[^'`\s]*"\)\])/.source, // bg-[url("..."),url("...")]
Comment on lines +14 to +15
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to simplify all of these regexes, but also a little bit scared of doing it and missing important pieces.

/([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']`
/([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]`
/([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50`
Expand Down
24 changes: 19 additions & 5 deletions src/util/dataTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ let UNDERSCORE = /_(?![^(]*\))/g // Underscore separator that is not located bet

// This is not a data type, but rather a function that can normalize the
// correct values.
export function normalize(value) {
export function normalize(value, isRoot = true) {
// Keep raw strings if it starts with `url(`
if (value.includes('url(')) {
return value
.split(/(url\(.*?\))/g)
.filter(Boolean)
.map((part) => {
if (/^url\(.*?\)$/.test(part)) {
return part
}

return normalize(part, false)
})
.join('')
}

// Convert `_` to ` `, except for escaped underscores `\_`
value = value
.replace(
Expand All @@ -18,10 +33,9 @@ export function normalize(value) {
.replace(/\\_/g, '_')

// Remove leftover whitespace
value = value.trim()

// Keep raw strings if it starts with `url(`
if (value.startsWith('url(')) return value
if (isRoot) {
value = value.trim()
}

// Add spaces around operators inside calc() that do not follow an operator
// or '('.
Expand Down
13 changes: 11 additions & 2 deletions src/util/pluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,18 @@ function splitAtFirst(input, delim) {

export function coerceValue(types, modifier, options, tailwindConfig) {
if (isArbitraryValue(modifier)) {
let [explicitType, value] = splitAtFirst(modifier.slice(1, -1), ':')
let arbitraryValue = modifier.slice(1, -1)
let [explicitType, value] = splitAtFirst(arbitraryValue, ':')

// It could be that this resolves to `url(https` which is not a valid
// identifier. We currently only support "simple" words with dashes or
// underscores. E.g.: family-name
if (!/^[\w-_]+$/g.test(explicitType)) {
value = arbitraryValue
}

if (explicitType !== undefined && !supportedTypes.includes(explicitType)) {
//
else if (explicitType !== undefined && !supportedTypes.includes(explicitType)) {
return []
}

Expand Down
1 change: 1 addition & 0 deletions tests/arbitrary-values.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

<div class="cursor-[pointer]"></div>
<div class="cursor-[url(hand.cur)_2_2,pointer]"></div>
<div class="cursor-[url('./path_to_hand.cur')_2_2,pointer]"></div>
<div class="cursor-[var(--value)]"></div>

<div class="list-['\1F44D']"></div>
Expand Down
32 changes: 32 additions & 0 deletions tests/arbitrary-values.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,35 @@ it('should warn and not generate if arbitrary values are ambiguous', () => {
return expect(result.css).toMatchFormattedCss(css``)
})
})

it('should support colons in URLs', () => {
let config = {
content: [
{ raw: html`<div class="bg-[url('https://www.spacejam.com/1996/img/bg_stars.gif')]"></div>` },
],
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.bg-\[url\(\'https\:\/\/www\.spacejam\.com\/1996\/img\/bg_stars\.gif\'\)\] {
background-image: url('https://www.spacejam.com/1996/img/bg_stars.gif');
}
`)
})
})

it('should support unescaped underscores in URLs', () => {
let config = {
content: [
{ raw: html`<div class="bg-[url('brown_potato.jpg'),_url('red_tomato.png')]"></div>` },
],
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(`
.bg-\\[url\\(\\'brown_potato\\.jpg\\'\\)\\2c _url\\(\\'red_tomato\\.png\\'\\)\\] {
background-image: url('brown_potato.jpg'), url('red_tomato.png');
}
`)
})
})