Skip to content

Commit 0537718

Browse files
committed
Fix: get values even if they have some line breaks and whitespaces
1 parent af50ba1 commit 0537718

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

__tests__/replace.html.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,19 @@ it('should replace vue class bindings | issue gulp-rcs#14', () => {
274274
`,
275275
);
276276
});
277+
278+
it('should replace line breaks | issue rename-css-selectors#70', () => {
279+
replaceHtmlMacro(
280+
'.container {} .text-center {} .d-flex {} .flex-column {} .justify-content-center {} .align-items-center {} .align-content-center {} .ct-fluid {}',
281+
`
282+
<div class="container text-center d-flex flex-column justify-content-center align-items-center
283+
align-content-center ct-fluid">
284+
</div>
285+
`,
286+
`
287+
<div class="a b c d e f
288+
g h">
289+
</div>
290+
`,
291+
);
292+
});

__tests__/selectorsLibrary.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ it('get | should not get any', () => {
4545
expect(selector).toBe('nothing-to-get');
4646
});
4747

48+
it('get | should get a value with line breaks and whitespace', () => {
49+
rcs.selectorsLibrary.set('.test');
50+
51+
expect(rcs.selectorsLibrary.get('.test\n')).toBe('a\n');
52+
expect(rcs.selectorsLibrary.get('.test ')).toBe('a ');
53+
expect(rcs.selectorsLibrary.get(' .test\n')).toBe(' a\n');
54+
expect(rcs.selectorsLibrary.get(`
55+
.test
56+
57+
`)).toBe(`
58+
a
59+
60+
`);
61+
});
62+
4863
it('get | should not get any excludeList selector', () => {
4964
rcs.selectorsLibrary.set('.test');
5065
rcs.selectorsLibrary.set('.header');

lib/selectorsLibrary.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,31 @@ export class SelectorsLibrary extends BaseLibrary {
127127
}
128128

129129
get(value: string, opts: BaseLibraryOptions = {}): string {
130-
const hasType = AttributeLibrary.isSelector(value);
130+
const stringifyLineBreaks = value.replace(/\n/g, '\\n');
131+
const [beginningWhitespace] = stringifyLineBreaks.match(/^(\\.| )+/) || [''];
132+
const [endWhitespace] = stringifyLineBreaks.match(/(\\.| )+$/) || [''];
133+
const modifiedValue = stringifyLineBreaks.replace(beginningWhitespace, '').replace(endWhitespace, '');
134+
const hasType = AttributeLibrary.isSelector(modifiedValue);
135+
136+
const reconstructValue = (v: string): string => (
137+
(beginningWhitespace + v + endWhitespace).replace(/\\n/g, '\n')
138+
);
131139

132140
if (!hasType) {
133-
const ret = this.selectors[0].get(value, opts);
141+
const ret = this.selectors[0].get(modifiedValue, opts);
134142

135-
return ret === value ? this.selectors[1].get(value, opts) : ret;
143+
return reconstructValue((
144+
ret === modifiedValue
145+
? this.selectors[1].get(modifiedValue, opts)
146+
: ret
147+
));
136148
}
137149

138-
if (this.selectors[0].isValidSelector(value)) {
139-
return this.selectors[0].get(value, opts);
150+
if (this.selectors[0].isValidSelector(modifiedValue)) {
151+
return reconstructValue(this.selectors[0].get(modifiedValue, opts));
140152
}
141153

142-
return this.selectors[1].get(value, opts);
154+
return reconstructValue(this.selectors[1].get(modifiedValue, opts));
143155
}
144156

145157
// If it's reserved in any case, consider it reserved everywhere

0 commit comments

Comments
 (0)