2017-02-05 16:49:01 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2017-03-23 15:52:06 -04:00
|
|
|
import {Component, Renderer2, ViewEncapsulation} from '@angular/core';
|
2017-02-05 16:49:01 -05:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
|
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2017-03-23 15:52:06 -04:00
|
|
|
import {NAMESPACE_URIS} from '../../src/dom/dom_renderer';
|
2017-02-05 16:49:01 -05:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2017-03-23 15:52:06 -04:00
|
|
|
describe('DefaultDomRendererV2', () => {
|
2017-12-18 01:18:50 -05:00
|
|
|
if (isNode) return;
|
2017-03-23 15:52:06 -04:00
|
|
|
let renderer: Renderer2;
|
2017-02-05 16:49:01 -05:00
|
|
|
|
2017-03-23 15:52:06 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
declarations: [
|
|
|
|
TestCmp, SomeApp, CmpEncapsulationEmulated, CmpEncapsulationNative, CmpEncapsulationNone
|
|
|
|
]
|
|
|
|
});
|
|
|
|
renderer = TestBed.createComponent(TestCmp).componentInstance.renderer;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setAttribute', () => {
|
|
|
|
describe('with namespace', () => {
|
|
|
|
it('xmlns', () => shouldSetAttributeWithNs('xmlns'));
|
|
|
|
it('xml', () => shouldSetAttributeWithNs('xml'));
|
|
|
|
it('svg', () => shouldSetAttributeWithNs('svg'));
|
|
|
|
it('xhtml', () => shouldSetAttributeWithNs('xhtml'));
|
|
|
|
it('xlink', () => shouldSetAttributeWithNs('xlink'));
|
|
|
|
|
|
|
|
it('unknown', () => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
expect(div.hasAttribute('unknown:name')).toBe(false);
|
|
|
|
|
|
|
|
renderer.setAttribute(div, 'name', 'value', 'unknown');
|
|
|
|
|
|
|
|
expect(div.getAttribute('unknown:name')).toBe('value');
|
|
|
|
});
|
|
|
|
|
|
|
|
function shouldSetAttributeWithNs(namespace: string): void {
|
|
|
|
const namespaceUri = NAMESPACE_URIS[namespace];
|
|
|
|
const div = document.createElement('div');
|
|
|
|
expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(false);
|
|
|
|
|
|
|
|
renderer.setAttribute(div, 'name', 'value', namespace);
|
|
|
|
|
|
|
|
expect(div.getAttributeNS(namespaceUri, 'name')).toBe('value');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeAttribute', () => {
|
|
|
|
describe('with namespace', () => {
|
|
|
|
it('xmlns', () => shouldRemoveAttributeWithNs('xmlns'));
|
|
|
|
it('xml', () => shouldRemoveAttributeWithNs('xml'));
|
|
|
|
it('svg', () => shouldRemoveAttributeWithNs('svg'));
|
|
|
|
it('xhtml', () => shouldRemoveAttributeWithNs('xhtml'));
|
|
|
|
it('xlink', () => shouldRemoveAttributeWithNs('xlink'));
|
|
|
|
|
|
|
|
it('unknown', () => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.setAttribute('unknown:name', 'value');
|
|
|
|
expect(div.hasAttribute('unknown:name')).toBe(true);
|
|
|
|
|
|
|
|
renderer.removeAttribute(div, 'name', 'unknown');
|
|
|
|
|
|
|
|
expect(div.hasAttribute('unknown:name')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function shouldRemoveAttributeWithNs(namespace: string): void {
|
|
|
|
const namespaceUri = NAMESPACE_URIS[namespace];
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.setAttributeNS(namespaceUri, `${namespace}:name`, 'value');
|
|
|
|
expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(true);
|
|
|
|
|
|
|
|
renderer.removeAttribute(div, 'name', namespace);
|
|
|
|
|
|
|
|
expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-02-05 16:49:01 -05:00
|
|
|
|
|
|
|
// other browsers don't support shadow dom
|
|
|
|
if (browserDetection.isChromeDesktop) {
|
2017-02-16 14:56:17 -05:00
|
|
|
it('should allow to style components with emulated encapsulation and no encapsulation inside of components with shadow DOM',
|
|
|
|
() => {
|
|
|
|
const fixture = TestBed.createComponent(SomeApp);
|
|
|
|
|
|
|
|
const cmp = fixture.debugElement.query(By.css('cmp-native')).nativeElement;
|
|
|
|
|
|
|
|
|
|
|
|
const native = cmp.shadowRoot.querySelector('.native');
|
|
|
|
expect(window.getComputedStyle(native).color).toEqual('rgb(255, 0, 0)');
|
|
|
|
|
|
|
|
const emulated = cmp.shadowRoot.querySelector('.emulated');
|
|
|
|
expect(window.getComputedStyle(emulated).color).toEqual('rgb(0, 0, 255)');
|
|
|
|
|
|
|
|
const none = cmp.shadowRoot.querySelector('.none');
|
|
|
|
expect(window.getComputedStyle(none).color).toEqual('rgb(0, 255, 0)');
|
|
|
|
});
|
2017-02-05 16:49:01 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'cmp-native',
|
2017-03-23 15:52:06 -04:00
|
|
|
template: `<div class="native"></div><cmp-emulated></cmp-emulated><cmp-none></cmp-none>`,
|
2017-02-16 14:56:17 -05:00
|
|
|
styles: [`.native { color: red; }`],
|
2017-02-05 16:49:01 -05:00
|
|
|
encapsulation: ViewEncapsulation.Native
|
|
|
|
})
|
|
|
|
class CmpEncapsulationNative {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'cmp-emulated',
|
2017-02-16 14:56:17 -05:00
|
|
|
template: `<div class="emulated"></div>`,
|
|
|
|
styles: [`.emulated { color: blue; }`],
|
2017-02-05 16:49:01 -05:00
|
|
|
encapsulation: ViewEncapsulation.Emulated
|
|
|
|
})
|
|
|
|
class CmpEncapsulationEmulated {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'cmp-none',
|
2017-02-16 14:56:17 -05:00
|
|
|
template: `<div class="none"></div>`,
|
|
|
|
styles: [`.none { color: lime; }`],
|
2017-02-05 16:49:01 -05:00
|
|
|
encapsulation: ViewEncapsulation.None
|
|
|
|
})
|
|
|
|
class CmpEncapsulationNone {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'some-app',
|
|
|
|
template: `
|
|
|
|
<cmp-native></cmp-native>
|
|
|
|
<cmp-emulated></cmp-emulated>
|
|
|
|
<cmp-none></cmp-none>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
export class SomeApp {
|
|
|
|
}
|
|
|
|
|
2017-03-23 15:52:06 -04:00
|
|
|
@Component({selector: 'test-cmp', template: ''})
|
|
|
|
class TestCmp {
|
|
|
|
constructor(public renderer: Renderer2) {}
|
2017-02-05 16:49:01 -05:00
|
|
|
}
|