Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): fix base href insertion when HTML…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Alan Agius authored and vikerman committed Mar 11, 2019
1 parent 901042d commit 1e3c6e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 1e3c6e3

Please sign in to comment.