Skip to content

Commit

Permalink
fix: place preferred char in space token (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros authored Sep 10, 2023
1 parent f7e4a21 commit c3e8bb9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/css-value-parser/src/value-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ function handleToken(
): void {
const { type, value, start, end } = token;
if (type === `space`) {
const firstSpace = value.indexOf(` `);
let firstSpace = value.indexOf(` `);
if (firstSpace === -1) {
firstSpace = value.indexOf(`\n`);
}
if (firstSpace === -1) {
firstSpace = value.indexOf(`\t`);
}
const before = firstSpace !== -1 ? value.substring(0, firstSpace) : ``;
const after = firstSpace !== -1 ? value.substring(firstSpace + 1) : value.substring(1);
ast.push(
space({
value: firstSpace !== -1 ? ` ` : value[0],
value: firstSpace !== -1 ? value[firstSpace] : value[0],
start,
end,
before,
Expand Down
10 changes: 10 additions & 0 deletions packages/css-value-parser/test/value-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ describe(`value-parser`, () => {
}),
],
},
{
type: `\\r\\n`,
source: `\r\n`,
expected: [space({ value: `\n`, before: '\r', start: 0, end: 2 })],
},
{
type: `\\r\\t`,
source: `\r\t`,
expected: [space({ value: `\t`, before: '\r', start: 0, end: 2 })],
},
].forEach(createTest);
});
describe(`literals`, () => {
Expand Down

0 comments on commit c3e8bb9

Please sign in to comment.