fix(@angular-devkit/build-angular): fix base href insertion when HTML is in a single line

When HTML is in a single line using offset + 1 will cause the insertion of the base href tag in the wrong possition.

Fixes #13851
This commit is contained in:
Alan Agius 2019-03-11 10:38:54 +01:00 committed by vikerman
parent 901042d95b
commit 1e3c6e3ca5
2 changed files with 20 additions and 2 deletions

View File

@ -199,7 +199,7 @@ export class IndexHtmlWebpackPlugin {
treeAdapter.appendChild(baseFragment, baseElement);
indexSource.insert(
headElement.__location.startTag.endOffset + 1,
headElement.__location.startTag.endOffset,
parse5.serialize(baseFragment, { treeAdapter }),
);
} else {

View File

@ -8,7 +8,7 @@
import { Architect } from '@angular-devkit/architect/src/index2';
import { runTargetSpec } from '@angular-devkit/architect/testing';
import { join, normalize, virtualFs } from '@angular-devkit/core';
import { join, normalize, tags, virtualFs } from '@angular-devkit/core';
import { tap } from 'rxjs/operators';
import { BrowserBuilderOutput } from '../../src/browser/index2';
import { browserTargetSpec, createArchitect, host } from '../utils';
@ -41,4 +41,22 @@ describe('Browser Builder base href', () => {
await run.stop();
});
it('should insert base href in the the correct position', async () => {
host.writeMultipleFiles({
'src/index.html': tags.oneLine`
<html><head><meta charset="UTF-8"></head>
<body><app-root></app-root></body></html>
`,
});
const overrides = { baseHref: '/myUrl' };
const run = await architect.scheduleTarget(targetSpec, overrides);
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
const fileName = join(normalize(output.outputPath), 'index.html');
const content = virtualFs.fileBufferToString(await host.read(normalize(fileName)).toPromise());
expect(content).toContain('<head><base href="/myUrl"><meta charset="UTF-8"></head>');
await run.stop();
});
});