OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * Custom DOM elements. | |
7 * | |
8 * This library provides access to the Polymer project's | |
9 * [Custom Elements] | |
10 * (http://www.polymer-project.org/platform/custom-elements.html) | |
11 * API, which lets you define your own elements. With custom elements, you | |
12 * associate code with custom tag names, and then use those custom tag names | |
13 * as you would any standard tag. For more information, see the | |
14 * [Polymer.dart homepage](https://www.dartlang.org/polymer-dart/) and its | |
15 * [custom element example] | |
16 * (https://www.dartlang.org/polymer-dart/#custom-elements). | |
17 */ | |
18 library custom_element; | |
19 | |
20 import 'dart:async'; | |
21 import 'dart:html'; | |
22 import 'src/custom_tag_name.dart'; | |
23 | |
24 /** | |
25 * *Deprecated* -- do not use. Extend [HtmlElement] and use | |
26 * [document.register] instead. If running on a browser without native | |
27 * document.register, you can add the polyfill script to your page: | |
28 * | |
29 * <script src="packages/custom_element/custom-elements.debug.js"></script> | |
30 * | |
31 * You can also use "custom-elements.min.js" for the minified version. | |
32 */ | |
33 // This is only used by Dart Web UI. | |
34 class CustomElement implements Element { | |
35 /** The web component element wrapped by this class. */ | |
36 Element _host; | |
37 List _shadowRoots; | |
38 | |
39 /** | |
40 * Shadow roots generated by dwc for each custom element, indexed by the | |
41 * custom element tag name. | |
42 */ | |
43 Map<String, dynamic> _generatedRoots = {}; | |
44 | |
45 /** | |
46 * Temporary property until components extend [Element]. An element can | |
47 * only be associated with one host, and it is an error to use a web component | |
48 * without an associated host element. | |
49 */ | |
50 Element get host { | |
51 if (_host == null) throw new StateError('host element has not been set.'); | |
52 return _host; | |
53 } | |
54 | |
55 set host(Element value) { | |
56 if (value == null) { | |
57 throw new ArgumentError('host must not be null.'); | |
58 } | |
59 // TODO(jmesserly): xtag used to return "null" if unset, now it checks for | |
60 // "this". Temporarily allow both. | |
61 var xtag = value.xtag; | |
62 if (xtag != null && xtag != value) { | |
63 throw new ArgumentError('host must not have its xtag property set.'); | |
64 } | |
65 if (_host != null) { | |
66 throw new StateError('host can only be set once.'); | |
67 } | |
68 | |
69 value.xtag = this; | |
70 _host = value; | |
71 } | |
72 | |
73 /** | |
74 * **Note**: This is an implementation helper and should not need to be called | |
75 * from your code. | |
76 * | |
77 * Creates the [ShadowRoot] backing this component. | |
78 */ | |
79 createShadowRoot([String componentName]) { | |
80 var root = host.createShadowRoot(); | |
81 if (componentName != null) { | |
82 _generatedRoots[componentName] = root; | |
83 } | |
84 return root; | |
85 } | |
86 | |
87 getShadowRoot(String componentName) => _generatedRoots[componentName]; | |
88 | |
89 /** | |
90 * Invoked when this component gets created. | |
91 * Note that [root] will be a [ShadowRoot] if the browser supports Shadow DOM. | |
92 */ | |
93 void created() {} | |
94 // Added for analyzer warnings | |
95 @deprecated | |
96 void createdCallback() {} | |
97 | |
98 /** Invoked when this component gets inserted in the DOM tree. */ | |
99 void inserted() {} | |
100 @deprecated | |
101 void enteredView() {} | |
102 | |
103 /** Invoked when this component is removed from the DOM tree. */ | |
104 void removed() {} | |
105 @deprecated | |
106 void leftView() {} | |
107 | |
108 /** Invoked when any attribute of the component is modified. */ | |
109 void attributeChanged(String name, String oldValue, String newValue) => | |
110 host.attributeChanged(name, oldValue, newValue); | |
111 | |
112 // TODO(efortuna): Update these when we decide what to do with these | |
113 // properties. | |
114 @deprecated | |
115 String getAttribute(String name) => | |
116 host.getAttribute(name); | |
117 | |
118 @deprecated | |
119 String getAttributeNS(String namespaceUri, String localName) => | |
120 host.getAttributeNS(namespaceUri, localName); | |
121 | |
122 @deprecated | |
123 String setAttributeNS( | |
124 String namespaceUri, String localName, String value) { | |
125 host.setAttributeNS(namespaceUri, localName, value); | |
126 } | |
127 | |
128 @deprecated | |
129 void setAttribute(String name, String value) => | |
130 host.setAttribute(name, value); | |
131 | |
132 @deprecated | |
133 List<Node> get childNodes => host.childNodes; | |
134 | |
135 // TODO(jmesserly): this forwarding is temporary until Dart supports | |
136 // subclassing Elements. | |
137 // TODO(jmesserly): we were missing the setter for title, are other things | |
138 // missing setters? | |
139 | |
140 List<Node> get nodes => host.nodes; | |
141 | |
142 set nodes(Iterable<Node> value) { host.nodes = value; } | |
143 | |
144 /** | |
145 * Replaces this node with another node. | |
146 */ | |
147 Node replaceWith(Node otherNode) { host.replaceWith(otherNode); } | |
148 | |
149 /** | |
150 * Removes this node from the DOM. | |
151 */ | |
152 void remove() => host.remove(); | |
153 | |
154 Node get nextNode => host.nextNode; | |
155 | |
156 String get nodeName => host.nodeName; | |
157 | |
158 Document get ownerDocument => host.ownerDocument; | |
159 | |
160 Node get previousNode => host.previousNode; | |
161 | |
162 String get text => host.text; | |
163 | |
164 set text(String v) { host.text = v; } | |
165 | |
166 bool contains(Node other) => host.contains(other); | |
167 | |
168 bool hasChildNodes() => host.hasChildNodes(); | |
169 | |
170 Node insertBefore(Node newChild, Node refChild) => | |
171 host.insertBefore(newChild, refChild); | |
172 | |
173 Node insertAllBefore(Iterable<Node> newChild, Node refChild) => | |
174 host.insertAllBefore(newChild, refChild); | |
175 | |
176 Map<String, String> get attributes => host.attributes; | |
177 set attributes(Map<String, String> value) { | |
178 host.attributes = value; | |
179 } | |
180 | |
181 List<Element> get elements => host.children; | |
182 | |
183 set elements(List<Element> value) { | |
184 host.children = value; | |
185 } | |
186 | |
187 List<Element> get children => host.children; | |
188 | |
189 set children(List<Element> value) { | |
190 host.children = value; | |
191 } | |
192 | |
193 Set<String> get classes => host.classes; | |
194 | |
195 set classes(Iterable<String> value) { | |
196 host.classes = value; | |
197 } | |
198 | |
199 CssRect get contentEdge => host.contentEdge; | |
200 CssRect get paddingEdge => host.paddingEdge; | |
201 CssRect get borderEdge => host.borderEdge; | |
202 CssRect get marginEdge => host.marginEdge; | |
203 Point get documentOffset => host.documentOffset; | |
204 Point offsetTo(Element parent) => host.offsetTo(parent); | |
205 | |
206 Map<String, String> getNamespacedAttributes(String namespace) => | |
207 host.getNamespacedAttributes(namespace); | |
208 | |
209 CssStyleDeclaration getComputedStyle([String pseudoElement]) | |
210 => host.getComputedStyle(pseudoElement); | |
211 | |
212 Element clone(bool deep) => host.clone(deep); | |
213 | |
214 Element get parent => host.parent; | |
215 | |
216 Node get parentNode => host.parentNode; | |
217 | |
218 String get nodeValue => host.nodeValue; | |
219 | |
220 Events get on => host.on; | |
221 | |
222 String get contentEditable => host.contentEditable; | |
223 set contentEditable(String v) { host.contentEditable = v; } | |
224 | |
225 String get dir => host.dir; | |
226 set dir(String v) { host.dir = v; } | |
227 | |
228 bool get draggable => host.draggable; | |
229 set draggable(bool v) { host.draggable = v; } | |
230 | |
231 bool get hidden => host.hidden; | |
232 set hidden(bool v) { host.hidden = v; } | |
233 | |
234 String get id => host.id; | |
235 set id(String v) { host.id = v; } | |
236 | |
237 String get innerHtml => host.innerHtml; | |
238 void set innerHtml(String v) { | |
239 host.innerHtml = v; | |
240 } | |
241 | |
242 void setInnerHtml(String html, | |
243 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) { | |
244 host.setInnerHtml(html, validator: validator, treeSanitizer: treeSanitizer); | |
245 } | |
246 | |
247 DocumentFragment createFragment(String html, | |
248 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) => | |
249 host.createFragment(html, | |
250 validator: validator, treeSanitizer: treeSanitizer); | |
251 | |
252 InputMethodContext get inputMethodContext => host.inputMethodContext; | |
253 | |
254 bool get isContentEditable => host.isContentEditable; | |
255 | |
256 String get lang => host.lang; | |
257 set lang(String v) { host.lang = v; } | |
258 | |
259 String get outerHtml => host.outerHtml; | |
260 | |
261 bool get spellcheck => host.spellcheck; | |
262 set spellcheck(bool v) { host.spellcheck = v; } | |
263 | |
264 int get tabIndex => host.tabIndex; | |
265 set tabIndex(int i) { host.tabIndex = i; } | |
266 | |
267 String get title => host.title; | |
268 | |
269 set title(String value) { host.title = value; } | |
270 | |
271 bool get translate => host.translate; | |
272 set translate(bool v) { host.translate = v; } | |
273 | |
274 String get dropzone => host.dropzone; | |
275 set dropzone(String v) { host.dropzone = v; } | |
276 | |
277 void click() { host.click(); } | |
278 | |
279 List<Node> getDestinationInsertionPoints() => | |
280 host.getDestinationInsertionPoints(); | |
281 | |
282 Element insertAdjacentElement(String where, Element element) => | |
283 host.insertAdjacentElement(where, element); | |
284 | |
285 void insertAdjacentHtml(String where, String html) { | |
286 host.insertAdjacentHtml(where, html); | |
287 } | |
288 | |
289 void insertAdjacentText(String where, String text) { | |
290 host.insertAdjacentText(where, text); | |
291 } | |
292 | |
293 Map<String, String> get dataset => host.dataset; | |
294 | |
295 set dataset(Map<String, String> value) { | |
296 host.dataset = value; | |
297 } | |
298 | |
299 Element get nextElementSibling => host.nextElementSibling; | |
300 | |
301 Element get offsetParent => host.offsetParent; | |
302 | |
303 Element get previousElementSibling => host.previousElementSibling; | |
304 | |
305 CssStyleDeclaration get style => host.style; | |
306 | |
307 String get tagName => host.tagName; | |
308 | |
309 String get pseudo => host.pseudo; | |
310 | |
311 void set pseudo(String value) { | |
312 host.pseudo = value; | |
313 } | |
314 | |
315 // Note: we are not polyfilling the shadow root here. This will be fixed when | |
316 // we migrate to the JS Shadow DOM polyfills. You can still use getShadowRoot | |
317 // to retrieve a node that behaves as the shadow root when Shadow DOM is not | |
318 // enabled. | |
319 ShadowRoot get shadowRoot => host.shadowRoot; | |
320 | |
321 void blur() { host.blur(); } | |
322 | |
323 void focus() { host.focus(); } | |
324 | |
325 void scrollByLines(int lines) { | |
326 host.scrollByLines(lines); | |
327 } | |
328 | |
329 void scrollByPages(int pages) { | |
330 host.scrollByPages(pages); | |
331 } | |
332 | |
333 void scrollIntoView([ScrollAlignment alignment]) { | |
334 host.scrollIntoView(alignment); | |
335 } | |
336 | |
337 bool matches(String selectors) => host.matches(selectors); | |
338 | |
339 bool matchesWithAncestors(String selectors) => | |
340 host.matchesWithAncestors(selectors); | |
341 | |
342 @deprecated | |
343 void requestFullScreen(int flags) { requestFullscreen(); } | |
344 | |
345 void requestFullscreen() { host.requestFullscreen(); } | |
346 | |
347 void requestPointerLock() { host.requestPointerLock(); } | |
348 | |
349 Element querySelector(String selectors) => host.querySelector(selectors); | |
350 | |
351 ElementList querySelectorAll(String selectors) => | |
352 host.querySelectorAll(selectors); | |
353 | |
354 @deprecated | |
355 Element query(String selectors) => host.querySelector(selectors); | |
356 | |
357 @deprecated | |
358 ElementList queryAll(String selectors) => host.querySelectorAll(selectors); | |
359 | |
360 String get className => host.className; | |
361 set className(String value) { host.className = value; } | |
362 | |
363 @deprecated | |
364 int get clientHeight => client.height; | |
365 | |
366 @deprecated | |
367 int get clientLeft => client.left; | |
368 | |
369 @deprecated | |
370 int get clientTop => client.top; | |
371 | |
372 @deprecated | |
373 int get clientWidth => client.width; | |
374 | |
375 Rectangle get client => host.client; | |
376 | |
377 @deprecated | |
378 int get offsetHeight => offset.height; | |
379 | |
380 @deprecated | |
381 int get offsetLeft => offset.left; | |
382 | |
383 @deprecated | |
384 int get offsetTop => offset.top; | |
385 | |
386 @deprecated | |
387 int get offsetWidth => offset.width; | |
388 | |
389 Rectangle get offset => host.offset; | |
390 | |
391 int get scrollHeight => host.scrollHeight; | |
392 | |
393 int get scrollLeft => host.scrollLeft; | |
394 | |
395 int get scrollTop => host.scrollTop; | |
396 | |
397 set scrollLeft(int value) { host.scrollLeft = value; } | |
398 | |
399 set scrollTop(int value) { host.scrollTop = value; } | |
400 | |
401 int get scrollWidth => host.scrollWidth; | |
402 | |
403 Rectangle getBoundingClientRect() => host.getBoundingClientRect(); | |
404 | |
405 List<Rectangle> getClientRects() => host.getClientRects(); | |
406 | |
407 List<Node> getElementsByClassName(String name) => | |
408 host.getElementsByClassName(name); | |
409 | |
410 Node get firstChild => host.firstChild; | |
411 | |
412 Node get lastChild => host.lastChild; | |
413 | |
414 String get localName => host.localName; | |
415 | |
416 String get namespaceUri => host.namespaceUri; | |
417 | |
418 int get nodeType => host.nodeType; | |
419 | |
420 void addEventListener(String type, EventListener listener, | |
421 [bool useCapture]) { | |
422 host.addEventListener(type, listener, useCapture); | |
423 } | |
424 | |
425 bool dispatchEvent(Event event) => host.dispatchEvent(event); | |
426 | |
427 void removeEventListener(String type, EventListener listener, | |
428 [bool useCapture]) { | |
429 host.removeEventListener(type, listener, useCapture); | |
430 } | |
431 | |
432 get xtag => host.xtag; | |
433 | |
434 set xtag(value) { host.xtag = value; } | |
435 | |
436 Node append(Node e) => host.append(e); | |
437 | |
438 void appendText(String text) => host.appendText(text); | |
439 | |
440 void appendHtml(String html) => host.appendHtml(html); | |
441 | |
442 String get regionOverset => host.regionOverset; | |
443 | |
444 List<Range> getRegionFlowRanges() => host.getRegionFlowRanges(); | |
445 | |
446 // TODO(jmesserly): rename "created" to "onCreated". | |
447 void onCreated() => created(); | |
448 | |
449 Stream<Event> get onAbort => host.onAbort; | |
450 Stream<Event> get onBeforeCopy => host.onBeforeCopy; | |
451 Stream<Event> get onBeforeCut => host.onBeforeCut; | |
452 Stream<Event> get onBeforePaste => host.onBeforePaste; | |
453 Stream<Event> get onBlur => host.onBlur; | |
454 Stream<Event> get onChange => host.onChange; | |
455 Stream<MouseEvent> get onClick => host.onClick; | |
456 Stream<MouseEvent> get onContextMenu => host.onContextMenu; | |
457 Stream<Event> get onCopy => host.onCopy; | |
458 Stream<Event> get onCut => host.onCut; | |
459 Stream<Event> get onDoubleClick => host.onDoubleClick; | |
460 Stream<MouseEvent> get onDrag => host.onDrag; | |
461 Stream<MouseEvent> get onDragEnd => host.onDragEnd; | |
462 Stream<MouseEvent> get onDragEnter => host.onDragEnter; | |
463 Stream<MouseEvent> get onDragLeave => host.onDragLeave; | |
464 Stream<MouseEvent> get onDragOver => host.onDragOver; | |
465 Stream<MouseEvent> get onDragStart => host.onDragStart; | |
466 Stream<MouseEvent> get onDrop => host.onDrop; | |
467 Stream<Event> get onError => host.onError; | |
468 Stream<Event> get onFocus => host.onFocus; | |
469 Stream<Event> get onInput => host.onInput; | |
470 Stream<Event> get onInvalid => host.onInvalid; | |
471 Stream<KeyboardEvent> get onKeyDown => host.onKeyDown; | |
472 Stream<KeyboardEvent> get onKeyPress => host.onKeyPress; | |
473 Stream<KeyboardEvent> get onKeyUp => host.onKeyUp; | |
474 Stream<Event> get onLoad => host.onLoad; | |
475 Stream<MouseEvent> get onMouseDown => host.onMouseDown; | |
476 Stream<MouseEvent> get onMouseEnter => host.onMouseEnter; | |
477 Stream<MouseEvent> get onMouseLeave => host.onMouseLeave; | |
478 Stream<MouseEvent> get onMouseMove => host.onMouseMove; | |
479 Stream<Event> get onFullscreenChange => host.onFullscreenChange; | |
480 Stream<Event> get onFullscreenError => host.onFullscreenError; | |
481 Stream<Event> get onPaste => host.onPaste; | |
482 Stream<Event> get onReset => host.onReset; | |
483 Stream<Event> get onScroll => host.onScroll; | |
484 Stream<Event> get onSearch => host.onSearch; | |
485 Stream<Event> get onSelect => host.onSelect; | |
486 Stream<Event> get onSelectStart => host.onSelectStart; | |
487 Stream<Event> get onSubmit => host.onSubmit; | |
488 Stream<MouseEvent> get onMouseOut => host.onMouseOut; | |
489 Stream<MouseEvent> get onMouseOver => host.onMouseOver; | |
490 Stream<MouseEvent> get onMouseUp => host.onMouseUp; | |
491 Stream<TouchEvent> get onTouchCancel => host.onTouchCancel; | |
492 Stream<TouchEvent> get onTouchEnd => host.onTouchEnd; | |
493 Stream<TouchEvent> get onTouchEnter => host.onTouchEnter; | |
494 Stream<TouchEvent> get onTouchLeave => host.onTouchLeave; | |
495 Stream<TouchEvent> get onTouchMove => host.onTouchMove; | |
496 Stream<TouchEvent> get onTouchStart => host.onTouchStart; | |
497 Stream<TransitionEvent> get onTransitionEnd => host.onTransitionEnd; | |
498 | |
499 // TODO(sigmund): do the normal forwarding when dartbug.com/7919 is fixed. | |
500 Stream<WheelEvent> get onMouseWheel { | |
501 throw new UnsupportedError('onMouseWheel is not supported'); | |
502 } | |
503 } | |
OLD | NEW |