Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/htmlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export function getLanguageService(options: LanguageServiceOptions = defaultLang
};
}

export { convertScriptContentToJavaScript } from './parser/htmlScanner.js';

export function newHTMLDataProvider(id: string, customData: HTMLDataV1): IHTMLDataProvider {
return new HTMLDataProvider(id, customData);
}
Expand Down
23 changes: 23 additions & 0 deletions src/parser/htmlScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,26 @@ export function createScanner(input: string, initialOffset = 0, initialState: Sc
getTokenError: () => tokenError
};
}

/**
* Converts HTML comment markers (`<!--` and `-->`) in inline script content
* to JavaScript block comment markers (`/* ` and ` *​/`), only replacing
* matched pairs. Unpaired `-->` (e.g., inside a JS block comment like
* `/* --> *​/`) will not be replaced.
*/
export function convertScriptContentToJavaScript(content: string): string {
const re = /<!--|-->/g;
let depth = 0;
return content.replace(re, (match) => {
if (match === '<!--') {
depth++;
return '/* ';
}
// match === '-->'
if (depth > 0) {
depth--;
return ' */';
}
return match;
});
}
64 changes: 63 additions & 1 deletion src/test/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { suite, test } from 'node:test';
import * as assert from 'node:assert';
import { createScanner } from '../parser/htmlScanner.js';
import { createScanner, convertScriptContentToJavaScript } from '../parser/htmlScanner.js';
import { TokenType, ScannerState } from '../htmlLanguageTypes.js';

suite('HTML Scanner', () => {
Expand Down Expand Up @@ -876,4 +876,66 @@ suite('HTML Scanner', () => {
]);
});

});

suite('convertScriptContentToJavaScript', () => {

test('replaces matched <!-- and --> pairs', () => {
assert.strictEqual(
convertScriptContentToJavaScript('<!-- var x = 1; -->'),
'/* var x = 1; */'
);
});

test('does not replace unpaired -->', () => {
// Issue #151: --> inside a JS block comment should not be replaced
assert.strictEqual(
convertScriptContentToJavaScript('/* --> a b\n */'),
'/* --> a b\n */'
);
});

test('replaces only paired markers when mixed', () => {
assert.strictEqual(
convertScriptContentToJavaScript('<!-- code --> /* --> */'),
'/* code */ /* --> */'
);
});

test('handles multiple pairs', () => {
assert.strictEqual(
convertScriptContentToJavaScript('<!-- a --> <!-- b -->'),
'/* a */ /* b */'
);
});

test('handles nested <!-- inside already open comment', () => {
// Second <!-- is also replaced since depth tracking allows it
assert.strictEqual(
convertScriptContentToJavaScript('<!-- <!-- --> -->'),
'/* /* */ */'
);
});

test('handles empty script content', () => {
assert.strictEqual(
convertScriptContentToJavaScript(''),
''
);
});

test('handles content with no markers', () => {
assert.strictEqual(
convertScriptContentToJavaScript('var x = 1;'),
'var x = 1;'
);
});

test('leaves unpaired <!-- without replacement of -->', () => {
assert.strictEqual(
convertScriptContentToJavaScript('<!-- var x = 1;'),
'/* var x = 1;'
);
});

});