Implement Custom Elements
This CL implements custom elements. The design is as follows:
1) Authors subclass Element and call registerElement.
2) When we create C++ elements for custom elements, we call the author's
constructor synchronously.
3) The attach/detach/attributeChanged callbacks are called either:
a) when exiting the current custom element callback scoped (e.g., before
returning from appendChild), or
b) when draining the microtask queue.
The implementation in this CL is a bit fragile because we don't detect name
registration conflicts and we let you create custom elements with the same name
as built-in elements. Also, not every part of the engine is prepared to execute
script synchronously below createElement. We'll need to iron out these issues
over time, but this CL is a start.
R=ojan@chromium.org
Review URL: https://codereview.chromium.org/943013002
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
CONSOLE: unittest-suite-wait-for-done
|
||||
CONSOLE: PASS: callbacks should be called
|
||||
CONSOLE:
|
||||
CONSOLE: All 1 tests passed.
|
||||
CONSOLE: unittest-suite-success
|
||||
DONE
|
||||
55
engine/src/flutter/tests/custom-elements/callbacks.sky
Normal file
55
engine/src/flutter/tests/custom-elements/callbacks.sky
Normal file
@@ -0,0 +1,55 @@
|
||||
<sky>
|
||||
<script>
|
||||
import "../resources/third_party/unittest/unittest.dart";
|
||||
import "../resources/unit.dart";
|
||||
|
||||
import "dart:sky";
|
||||
|
||||
class FooElement extends Element {
|
||||
FooElement() : super("foo");
|
||||
|
||||
attachedCallback() {
|
||||
++attachedCallbackCount;
|
||||
}
|
||||
|
||||
detachedCallback() {
|
||||
++detachedCallbackCount;
|
||||
}
|
||||
|
||||
attributeChangedCallback(String name, String oldValue, String newValue) {
|
||||
++attributeChangedCallbackCount;
|
||||
}
|
||||
|
||||
int attachedCallbackCount = 0;
|
||||
int detachedCallbackCount = 0;
|
||||
int attributeChangedCallbackCount = 0;
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
initUnit();
|
||||
|
||||
document.registerElement("foo", FooElement);
|
||||
|
||||
test("callbacks should be called", () {
|
||||
Element sky = document.querySelector("sky");
|
||||
FooElement foo = document.createElement("foo");
|
||||
expect(foo.attachedCallbackCount, equals(0));
|
||||
expect(foo.detachedCallbackCount, equals(0));
|
||||
expect(foo.attributeChangedCallbackCount, equals(0));
|
||||
sky.appendChild(foo);
|
||||
expect(foo.attachedCallbackCount, equals(1));
|
||||
expect(foo.detachedCallbackCount, equals(0));
|
||||
expect(foo.attributeChangedCallbackCount, equals(0));
|
||||
foo.setAttribute("bar", "baz");
|
||||
expect(foo.attachedCallbackCount, equals(1));
|
||||
expect(foo.detachedCallbackCount, equals(0));
|
||||
expect(foo.attributeChangedCallbackCount, equals(1));
|
||||
foo.remove();
|
||||
expect(foo.attachedCallbackCount, equals(1));
|
||||
expect(foo.detachedCallbackCount, equals(1));
|
||||
expect(foo.attributeChangedCallbackCount, equals(1));
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</sky>
|
||||
@@ -0,0 +1,2 @@
|
||||
CONSOLE: PASS: FooElement attached.
|
||||
|
||||
27
engine/src/flutter/tests/custom-elements/registration.sky
Normal file
27
engine/src/flutter/tests/custom-elements/registration.sky
Normal file
@@ -0,0 +1,27 @@
|
||||
<import src="../resources/dump-as-text.sky" />
|
||||
<script>
|
||||
import "dart:sky";
|
||||
|
||||
class FooElement extends Element {
|
||||
FooElement() : super("foo");
|
||||
|
||||
attachedCallback() {
|
||||
print("PASS: FooElement attached.");
|
||||
}
|
||||
|
||||
detachedCallback() {
|
||||
}
|
||||
|
||||
attributeChangedCallbackCount(String name, String oldValue, String newValue) {
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
document.registerElement("foo", FooElement);
|
||||
}
|
||||
</script>
|
||||
<foo />
|
||||
<script>
|
||||
// This script element creates a microtask checkpoint and ensures that
|
||||
// the <foo /> attached callback runs before the test ends.
|
||||
</script>
|
||||
Reference in New Issue
Block a user