Skip to content

Commit

Permalink
fix(upgrade): fix empty transclusion content with AngularJS@>=1.5.8
Browse files Browse the repository at this point in the history
The function provided by `ngUpgrade` as `parentBoundTranscludeFn` when
upgrading a component with transclusion, will break in AngularJS v1.5.8+
if no transclusion content is provided. The reason is that AngularJS
will try to destroy the transclusion scope (which would not be needed
any more). But since the transcluded content comes from Angular, not
AngularJS, there is no transclusion scope to destroy.
This commit fixes it by providing a dummy scope object with a no-op
`$destroy()` method.

See angular#13271 (comment).
  • Loading branch information
gkalpak committed Feb 12, 2018
1 parent 92d7060 commit 5e5cda5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
"@types/selenium-webdriver": "3.0.7",
"@types/source-map": "^0.5.1",
"@types/systemjs": "0.19.32",
"angular": "1.5.0",
"angular-animate": "1.5.0",
"angular-mocks": "1.5.0",
"angular": "1.6.9",
"angular-animate": "1.6.9",
"angular-mocks": "1.6.9",
"base64-js": "1.2.1",
"bower": "1.8.2",
"browserstacktunnel-wrapper": "2.0.1",
Expand Down
10 changes: 8 additions & 2 deletions packages/upgrade/src/common/upgrade_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ export class UpgradeHelper {
const transclude = this.directive.transclude;
const contentChildNodes = this.extractChildNodes();
let $template = contentChildNodes;
let attachChildrenFn: angular.ILinkFn|undefined = (scope, cloneAttach) =>
cloneAttach !($template, scope);
let attachChildrenFn: angular.ILinkFn = (scope, cloneAttachFn) => {
// Since AngularJS v1.5.8, `cloneAttachFn` will try to destroy the transclusion scope if
// `$template` is empty. Since the transcluded content comes from Angular, not AngularJS,
// there will be no transclusion scope here.
// Provide a dummy `scope.$destroy()` method to prevent `cloneAttachFn` from throwing.
scope = scope || {$destroy: () => undefined};
return cloneAttachFn!($template, scope);
};

if (transclude) {
const slots = Object.create(null);
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,17 @@ [email protected]:
dependencies:
esprima "~1.2.2"

angular-animate@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/angular-animate/-/angular-animate-1.5.0.tgz#0e31f31fa7ab2ddf5ea5787e476548644d62fb93"
angular-animate@1.6.9:
version "1.6.9"
resolved "https://registry.yarnpkg.com/angular-animate/-/angular-animate-1.6.9.tgz#a0f926c1ba3190bd8929ef966a7fa79760682622"

angular-mocks@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.5.0.tgz#81b3e4b21098d7c545c6f0a0a97f897b26879dd9"
angular-mocks@1.6.9:
version "1.6.9"
resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.6.9.tgz#4fed8c8293a5080e0919a7ff0dcf0f704864b7ba"

angular@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/angular/-/angular-1.5.0.tgz#d96ee97ab6df6cfd0915accbee484d098adb74ec"
angular@1.6.9:
version "1.6.9"
resolved "https://registry.yarnpkg.com/angular/-/angular-1.6.9.tgz#bc812932e18909038412d594a5990f4bb66c0619"

ansi-align@^1.1.0:
version "1.1.0"
Expand Down

0 comments on commit 5e5cda5

Please sign in to comment.