From 3b9b3218b76744bacddd2b4931936aa9497068c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 27 Dec 2023 19:31:32 +0100 Subject: [PATCH] Fixed `&` search after consuming parenthesis (#322) * Fixed `&` search after consuming parenthesis * add an extra test case * perf squeezing * bring back `'l'` check --- src/Parser.js | 2 +- src/Prefixer.js | 4 ++-- src/Utility.js | 5 +++-- test/Parser.js | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Parser.js b/src/Parser.js index e7472be..fda8e2f 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -44,7 +44,7 @@ export function parse (value, root, parent, rule, rules, rulesets, pseudo, point // ( case 40: if (previous != 108 && charat(characters, length - 1) == 58) { - if (indexof(characters += replace(delimit(character), '&', '&\f'), '&\f') != -1) + if (indexof(characters += replace(delimit(character), '&', '&\f'), '&\f', abs(index ? points[index - 1] : 0)) != -1) ampersand = -1 break } diff --git a/src/Prefixer.js b/src/Prefixer.js index c29bc19..440ae75 100644 --- a/src/Prefixer.js +++ b/src/Prefixer.js @@ -87,7 +87,7 @@ export function prefix (value, length, children) { // grid-(row|column)-start case 4384: case 3616: if (children && children.some(function (element, index) { return length = index, match(element.props, /grid-\w+-end/) })) { - return ~indexof(value + (children = children[length].value), 'span') ? value : (MS + replace(value, '-start', '') + value + MS + 'grid-row-span:' + (~indexof(children, 'span') ? match(children, /\d+/) : +match(children, /\d+/) - +match(value, /\d+/)) + ';') + return ~indexof(value + (children = children[length].value), 'span', 0) ? value : (MS + replace(value, '-start', '') + value + MS + 'grid-row-span:' + (~indexof(children, 'span', 0) ? match(children, /\d+/) : +match(children, /\d+/) - +match(value, /\d+/)) + ';') } return MS + replace(value, '-start', '') + value // grid-(row|column)-end @@ -113,7 +113,7 @@ export function prefix (value, length, children) { return replace(value, /(.+:)(.+)-([^]+)/, '$1' + WEBKIT + '$2-$3' + '$1' + MOZ + (charat(value, length + 3) == 108 ? '$3' : '$2-$3')) + value // (s)tretch case 115: - return ~indexof(value, 'stretch') ? prefix(replace(value, 'stretch', 'fill-available'), length, children) + value : value + return ~indexof(value, 'stretch', 0) ? prefix(replace(value, 'stretch', 'fill-available'), length, children) + value : value } break // grid-(column|row) diff --git a/src/Utility.js b/src/Utility.js index 8357dbc..d89838d 100644 --- a/src/Utility.js +++ b/src/Utility.js @@ -55,10 +55,11 @@ export function replace (value, pattern, replacement) { /** * @param {string} value * @param {string} search + * @param {number} position * @return {number} */ -export function indexof (value, search) { - return value.indexOf(search) +export function indexof (value, search, position) { + return value.indexOf(search, position) } /** diff --git a/test/Parser.js b/test/Parser.js index de0ddf4..91876cf 100644 --- a/test/Parser.js +++ b/test/Parser.js @@ -142,6 +142,21 @@ describe('Parser', () => { ).to.equal(`.user [href="https://css-tricks.com?a=1&b=2"]{color:red;}`) }) + test('& in first selector within a comma-separated list', () => { + expect( + stylis(` + div { + display: flex; + + &.foo, + p:not(:last-child) { + background: red; + } + } + `) + ).to.equal(`.user div{display:flex;}.user div.foo,.user div p:not(:last-child){background:red;}`) + }); + test('escaped chars in selector identifiers', () => { expect( stylis(` @@ -858,6 +873,15 @@ describe('Parser', () => { ].join('')) }) + test('context character IX', () => { + expect( + stylis(`background: url(i&m&g.png);.a {background: url(i&m&g.png);}`) + ).to.equal([ + `.user{background:url(i&m&g.png);}`, + `.user .a{background:url(i&m&g.png);}` + ].join('')) + }) + test('`--` in an identifier (#220)', () => { expect( stylis(`