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 authored and vikerman committed Mar 13, 2019
1 parent 0fca5f7 commit 06d0cae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Expand Up @@ -200,7 +200,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 @@ -7,7 +7,7 @@
*/

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 { browserTargetSpec, host } from '../utils';

Expand Down Expand Up @@ -35,4 +35,24 @@ describe('Browser Builder base href', () => {
}),
).toPromise().then(done, done.fail);
});

it('should insert base href in the the correct position', (done) => {
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' };

runTargetSpec(host, browserTargetSpec, overrides).pipe(
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
tap(() => {
const fileName = join(outputPath, 'index.html');
const content = virtualFs.fileBufferToString(host.scopedSync().read(fileName));
expect(content).toContain('<head><base href="/myUrl"><meta charset="UTF-8"></head>');
}),
).toPromise().then(done, done.fail);
});
});

0 comments on commit 06d0cae

Please sign in to comment.