Skip to content

Commit b2bc547

Browse files
committed
indexof
1 parent bbc4354 commit b2bc547

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/toolbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function injectToolbarHtml(
1818
if (config.toolbar === false) return html;
1919
if (toolbarAlreadyInjected(html)) return html;
2020

21-
const closingHeadIndex = html.toLowerCase().lastIndexOf('</head>');
21+
const closingHeadIndex = html.toLowerCase().indexOf('</head>');
2222
if (closingHeadIndex === -1) return html;
2323

2424
return `${html.slice(0, closingHeadIndex)}${toolbarHtml(config, getLocalPort)}${html.slice(closingHeadIndex)}`;

test/toolbar.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { injectToolbarHtml } from '../src/toolbar';
3+
4+
describe('toolbar HTML injection', () => {
5+
it('should inject before the first closing head tag', () => {
6+
const html =
7+
'<html><head><title>App</title></head><body><script>const marker = "</head>";</script></body></html>';
8+
9+
const injected = injectToolbarHtml(
10+
html,
11+
{ clientUrl: 'http://localhost:4000' },
12+
() => 5173,
13+
);
14+
15+
const toolbarIndex = injected.indexOf('/tc/toolbar.js');
16+
const firstClosingHeadIndex = injected.indexOf('</head>');
17+
const scriptMarkerIndex = injected.indexOf('const marker = "</head>";');
18+
19+
expect(toolbarIndex).toBeGreaterThan(-1);
20+
expect(toolbarIndex).toBeLessThan(firstClosingHeadIndex);
21+
expect(firstClosingHeadIndex).toBeLessThan(scriptMarkerIndex);
22+
});
23+
});

0 commit comments

Comments
 (0)