* Create the overlay container element, which is simply a div * with the 'cdk-overlay-container' class on the document body.
()
| 52 | * with the 'cdk-overlay-container' class on the document body. |
| 53 | */ |
| 54 | protected _createContainer(): void { |
| 55 | const containerClass = 'cdk-overlay-container'; |
| 56 | |
| 57 | // TODO(crisbeto): remove the testing check once we have an overlay testing |
| 58 | // module or Angular starts tearing down the testing `NgModule`. See: |
| 59 | // https://github.com/angular/angular/issues/18831 |
| 60 | if (this._platform.isBrowser || _isTestEnvironment()) { |
| 61 | const oppositePlatformContainers = this._document.querySelectorAll( |
| 62 | `.${containerClass}[platform="server"], ` + `.${containerClass}[platform="test"]`, |
| 63 | ); |
| 64 | |
| 65 | // Remove any old containers from the opposite platform. |
| 66 | // This can happen when transitioning from the server to the client. |
| 67 | for (let i = 0; i < oppositePlatformContainers.length; i++) { |
| 68 | oppositePlatformContainers[i].remove(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const container = this._document.createElement('div'); |
| 73 | container.classList.add(containerClass); |
| 74 | |
| 75 | // A long time ago we kept adding new overlay containers whenever a new app was instantiated, |
| 76 | // but at some point we added logic which clears the duplicate ones in order to avoid leaks. |
| 77 | // The new logic was a little too aggressive since it was breaking some legitimate use cases. |
| 78 | // To mitigate the problem we made it so that only containers from a different platform are |
| 79 | // cleared, but the side-effect was that people started depending on the overly-aggressive |
| 80 | // logic to clean up their tests for them. Until we can introduce an overlay-specific testing |
| 81 | // module which does the cleanup, we try to detect that we're in a test environment and we |
| 82 | // always clear the container. See #17006. |
| 83 | // TODO(crisbeto): remove the test environment check once we have an overlay testing module. |
| 84 | if (_isTestEnvironment()) { |
| 85 | container.setAttribute('platform', 'test'); |
| 86 | } else if (!this._platform.isBrowser) { |
| 87 | container.setAttribute('platform', 'server'); |
| 88 | } |
| 89 | |
| 90 | this._document.body.appendChild(container); |
| 91 | this._containerElement = container; |
| 92 | } |
| 93 | |
| 94 | /** Loads the structural styles necessary for the overlay to work. */ |
| 95 | protected _loadStyles(): void { |
no test coverage detected