Skip to content

Commit 5a712d4

Browse files
mattlewis92leonsenft
authored andcommitted
fix(compiler): prevent shimCssText from adding extra blank lines per CSS comment
The comment placeholder restoration in `shimCssText` appended an unconditional `+ '\n'` to each non-hash comment replacement. Because `_commentRe` does not consume the newline that follows a comment in the source, that newline already remains in `cssText`. The extra `'\n'` was therefore inserted on top of the existing one, shifting every line after each comment down by one. In files with many comments (e.g. large SCSS preambles) this shifts all subsequent CSS rules far enough that the CSS sourcemap — generated before `shimCssText` runs — points to completely wrong source locations in browser DevTools. The fix is to drop the `+ '\n'`; internal newlines within a multi-line comment are still preserved via `_newLinesRe`, and the trailing newline that follows the comment in `cssText` is already present without any extra injection.
1 parent 275a436 commit 5a712d4

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

packages/compiler/src/shadow_css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class ShadowCss {
186186
// Replace non hash comments with empty lines.
187187
// This is done so that we do not leak any sensitive data in comments.
188188
const newLinesMatches = m.match(_newLinesRe);
189-
comments.push((newLinesMatches?.join('') ?? '') + '\n');
189+
comments.push(newLinesMatches?.join('') ?? '');
190190
}
191191

192192
return COMMENT_PLACEHOLDER;

packages/compiler/test/shadow_css/shadow_css_spec.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,17 @@ describe('ShadowCss', () => {
368368
describe('comments', () => {
369369
// Comments should be kept in the same position as otherwise inline sourcemaps break due to
370370
// shift in lines.
371-
it('should replace multiline comments with newline', () => {
372-
expect(shim('/* b {c} */ b {c}', 'contenta')).toBe('\n b[contenta] {c}');
371+
it('should remove inline comments without adding extra lines', () => {
372+
expect(shim('/* b {c} */ b {c}', 'contenta')).toBe(' b[contenta] {c}');
373373
});
374374

375-
it('should replace multiline comments with newline in the original position', () => {
376-
expect(shim('/* b {c}\n */ b {c}', 'contenta')).toBe('\n\n b[contenta] {c}');
375+
it('should preserve internal newlines from multiline comments', () => {
376+
expect(shim('/* b {c}\n */ b {c}', 'contenta')).toBe('\n b[contenta] {c}');
377377
});
378378

379-
it('should replace comments with newline in the original position', () => {
379+
it('should remove multiple inline comments without adding extra lines', () => {
380380
expect(shim('/* b {c} */ b {c} /* a {c} */ a {c}', 'contenta')).toBe(
381-
'\n b[contenta] {c} \n a[contenta] {c}',
381+
' b[contenta] {c} a[contenta] {c}',
382382
);
383383
});
384384

@@ -392,9 +392,7 @@ describe('ShadowCss', () => {
392392
});
393393

394394
it('should handle adjacent comments', () => {
395-
expect(shim('/* comment 1 */ /* comment 2 */ b {c}', 'contenta')).toBe(
396-
'\n \n b[contenta] {c}',
397-
);
395+
expect(shim('/* comment 1 */ /* comment 2 */ b {c}', 'contenta')).toBe(' b[contenta] {c}');
398396
});
399397
});
400398
});

0 commit comments

Comments
 (0)