Skip to content

Commit 06d0cae

Browse files
Alanvikerman
Alan
authored andcommitted
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
1 parent 0fca5f7 commit 06d0cae

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/index-html-webpack-plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class IndexHtmlWebpackPlugin {
200200

201201
treeAdapter.appendChild(baseFragment, baseElement);
202202
indexSource.insert(
203-
headElement.__location.startTag.endOffset + 1,
203+
headElement.__location.startTag.endOffset,
204204
parse5.serialize(baseFragment, { treeAdapter }),
205205
);
206206
} else {

packages/angular_devkit/build_angular/test/browser/base-href_spec_large.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { runTargetSpec } from '@angular-devkit/architect/testing';
10-
import { join, normalize, virtualFs } from '@angular-devkit/core';
10+
import { join, normalize, tags, virtualFs } from '@angular-devkit/core';
1111
import { tap } from 'rxjs/operators';
1212
import { browserTargetSpec, host } from '../utils';
1313

@@ -35,4 +35,24 @@ describe('Browser Builder base href', () => {
3535
}),
3636
).toPromise().then(done, done.fail);
3737
});
38+
39+
it('should insert base href in the the correct position', (done) => {
40+
host.writeMultipleFiles({
41+
'src/index.html': tags.oneLine`
42+
<html><head><meta charset="UTF-8"></head>
43+
<body><app-root></app-root></body></html>
44+
`,
45+
});
46+
47+
const overrides = { baseHref: '/myUrl' };
48+
49+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
50+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
51+
tap(() => {
52+
const fileName = join(outputPath, 'index.html');
53+
const content = virtualFs.fileBufferToString(host.scopedSync().read(fileName));
54+
expect(content).toContain('<head><base href="/myUrl"><meta charset="UTF-8"></head>');
55+
}),
56+
).toPromise().then(done, done.fail);
57+
});
3858
});

0 commit comments

Comments
 (0)