/
environment.ts
160 lines (136 loc) · 4.43 KB
/
environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { OWNER, Owner } from '@ember/-internals/owner';
import { constructStyleDeprecationMessage, lookupComponent } from '@ember/-internals/views';
import { warn } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { Option, Simple } from '@glimmer/interfaces';
import { OpaqueIterable, VersionedReference } from '@glimmer/reference';
import {
ElementBuilder,
Environment as GlimmerEnvironment,
SimpleDynamicAttribute,
} from '@glimmer/runtime';
import { Destroyable, Opaque } from '@glimmer/util';
import DebugStack from './utils/debug-stack';
import createIterable from './utils/iterable';
import { ConditionalReference, UpdatableReference } from './utils/references';
import { isHTMLSafe } from './utils/string';
import installPlatformSpecificProtocolForURL from './protocol-for-url';
import { OwnedTemplate } from './template';
export interface CompilerFactory {
id: string;
new (template: OwnedTemplate): any;
}
export default class Environment extends GlimmerEnvironment {
static create(options: any) {
return new this(options);
}
public owner: Owner;
public isInteractive: boolean;
public destroyedComponents: Destroyable[];
public debugStack: typeof DebugStack;
public inTransaction = false;
constructor(injections: any) {
super(injections);
this.owner = injections[OWNER];
this.isInteractive = this.owner.lookup<any>('-environment:main').isInteractive;
// can be removed once https://github.com/tildeio/glimmer/pull/305 lands
this.destroyedComponents = [];
installPlatformSpecificProtocolForURL(this);
if (DEBUG) {
this.debugStack = new DebugStack();
}
}
// this gets clobbered by installPlatformSpecificProtocolForURL
// it really should just delegate to a platform specific injection
protocolForURL(s: string): string {
return s;
}
lookupComponent(name: string, meta: any) {
return lookupComponent(meta.owner, name, meta);
}
toConditionalReference(reference: UpdatableReference): VersionedReference<boolean> {
return ConditionalReference.create(reference);
}
iterableFor(ref: VersionedReference, key: string): OpaqueIterable {
return createIterable(ref, key);
}
scheduleInstallModifier(modifier: any, manager: any): void {
if (this.isInteractive) {
super.scheduleInstallModifier(modifier, manager);
}
}
scheduleUpdateModifier(modifier: any, manager: any): void {
if (this.isInteractive) {
super.scheduleUpdateModifier(modifier, manager);
}
}
didDestroy(destroyable: Destroyable): void {
destroyable.destroy();
}
begin(): void {
this.inTransaction = true;
super.begin();
}
commit(): void {
let destroyedComponents = this.destroyedComponents;
this.destroyedComponents = [];
// components queued for destruction must be destroyed before firing
// `didCreate` to prevent errors when removing and adding a component
// with the same name (would throw an error when added to view registry)
for (let i = 0; i < destroyedComponents.length; i++) {
destroyedComponents[i].destroy();
}
try {
super.commit();
} finally {
this.inTransaction = false;
}
}
}
if (DEBUG) {
class StyleAttributeManager extends SimpleDynamicAttribute {
set(dom: ElementBuilder, value: Opaque, env: GlimmerEnvironment): void {
warn(
constructStyleDeprecationMessage(value),
(() => {
if (value === null || value === undefined || isHTMLSafe(value)) {
return true;
}
return false;
})(),
{ id: 'ember-htmlbars.style-xss-warning' }
);
super.set(dom, value, env);
}
update(value: Opaque, env: GlimmerEnvironment): void {
warn(
constructStyleDeprecationMessage(value),
(() => {
if (value === null || value === undefined || isHTMLSafe(value)) {
return true;
}
return false;
})(),
{ id: 'ember-htmlbars.style-xss-warning' }
);
super.update(value, env);
}
}
Environment.prototype.attributeFor = function(
element,
attribute: string,
isTrusting: boolean,
namespace: Option<Simple.AttrNamespace>
) {
if (attribute === 'style' && !isTrusting) {
return new StyleAttributeManager({ element, name: attribute, namespace });
}
return GlimmerEnvironment.prototype.attributeFor.call(
this,
element,
attribute,
isTrusting,
namespace
);
};
}