OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 class _TextFactoryProvider { | |
6 | |
7 factory Text(data) => document._createTextNode(data); | |
8 } | |
9 | |
10 class _EventFactoryProvider { | |
11 factory Event(String type, [bool canBubble = true, | |
12 bool cancelable = true]) { | |
13 _EventImpl e = document._createEvent("Event"); | |
14 e._initEvent(type, canBubble, cancelable); | |
15 return e; | |
16 } | |
17 } | |
18 | |
19 class _MouseEventFactoryProvider { | |
20 factory MouseEvent(String type, Window view, int detail, | |
21 int screenX, int screenY, int clientX, int clientY, int button, | |
22 [bool canBubble = true, bool cancelable = true, bool ctrlKey = false, | |
23 bool altKey = false, bool shiftKey = false, bool metaKey = false, | |
24 EventTarget relatedTarget = null]) { | |
25 final e = document._createEvent("MouseEvent"); | |
26 e._initMouseEvent(type, canBubble, cancelable, view, detail, | |
27 screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, | |
28 button, relatedTarget); | |
29 return e; | |
30 } | |
31 } | |
32 | |
33 class _CSSStyleDeclarationFactoryProvider { | |
34 factory CSSStyleDeclaration.css(String css) { | |
35 var style = new Element.tag('div').style; | |
36 style.cssText = css; | |
37 return style; | |
38 } | |
39 | |
40 factory CSSStyleDeclaration() { | |
41 return new CSSStyleDeclaration.css(''); | |
42 } | |
43 } | |
44 | |
45 final _START_TAG_REGEXP = const RegExp('<(\\w+)'); | |
46 class _ElementFactoryProvider { | |
47 static final _CUSTOM_PARENT_TAG_MAP = const { | |
48 'body' : 'html', | |
49 'head' : 'html', | |
50 'caption' : 'table', | |
51 'td': 'tr', | |
52 'tbody': 'table', | |
53 'colgroup': 'table', | |
54 'col' : 'colgroup', | |
55 'tr' : 'tbody', | |
56 'tbody' : 'table', | |
57 'tfoot' : 'table', | |
58 'thead' : 'table', | |
59 'track' : 'audio', | |
60 }; | |
61 | |
62 /** @domName Document.createElement */ | |
63 factory Element.html(String html) { | |
64 // TODO(jacobr): this method can be made more robust and performant. | |
65 // 1) Cache the dummy parent elements required to use innerHTML rather than | |
66 // creating them every call. | |
67 // 2) Verify that the html does not contain leading or trailing text nodes. | |
68 // 3) Verify that the html does not contain both <head> and <body> tags. | |
69 // 4) Detatch the created element from its dummy parent. | |
70 String parentTag = 'div'; | |
71 String tag; | |
72 final match = _START_TAG_REGEXP.firstMatch(html); | |
73 if (match !== null) { | |
74 tag = match.group(1).toLowerCase(); | |
75 if (_CUSTOM_PARENT_TAG_MAP.containsKey(tag)) { | |
76 parentTag = _CUSTOM_PARENT_TAG_MAP[tag]; | |
77 } | |
78 } | |
79 // TODO(jacobr): make type dom.HTMLElement when dartium allows it. | |
nweiz
2012/03/01 21:18:03
This is no longer necessary
| |
80 _ElementImpl temp = document._createElement(parentTag); | |
nweiz
2012/03/01 21:18:03
Should this be Element rather than _ElementImpl?
| |
81 temp.innerHTML = html; | |
82 | |
83 Element element; | |
84 if (temp.elements.length == 1) { | |
85 element = temp.elements.first; | |
86 } else if (parentTag == 'html' && temp.elements.length == 2) { | |
87 // Work around for edge case in WebKit and possibly other browsers where | |
88 // both body and head elements are created even though the inner html | |
89 // only contains a head or body element. | |
90 element = temp.elements[tag == 'head' ? 0 : 1]; | |
91 } else { | |
92 throw new IllegalArgumentException('HTML had ${temp.elements.length} ' + | |
93 'top level elements but 1 expected'); | |
94 } | |
95 element.remove(); | |
96 return element; | |
97 } | |
98 | |
99 /** @domName Document.createElement */ | |
100 factory Element.tag(String tag) { | |
101 return document._createElement(tag); | |
102 } | |
103 } | |
OLD | NEW |