A user account is required in order to edit this wiki, but we've had to disable public user registrations due to spam.
To request an account, ask an autoconfirmed user on Chat (such as one of these permanent autoconfirmed members).
Component Model Strawman: Declarative Syntax: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
Let’s start from the minimum form of the element registration. | Let’s start from the minimum form of the element registration. | ||
We can use an element element to registering new element name. | We can use an <tt>element</tt> element to registering new element name. | ||
Now agent recognizes x-comment as HTMLRegisteredElement. | Now agent recognizes <tt>x-comment</tt> as <tt>HTMLRegisteredElement</tt>. | ||
This is just a plain old HTML element which has its own name. | This is just a plain old HTML element which has its own name. | ||
Line 16: | Line 16: | ||
</pre> | </pre> | ||
The element element also provides an opportunity to give a behavior for the instance of the registered element. | The <tt>element</tt> element also provides an opportunity to give a behavior for the instance of the registered element. | ||
<pre> | <pre> | ||
Line 34: | Line 34: | ||
</pre> | </pre> | ||
Using HTMLElementElement.register(), authors can register a set of lifecycle callbacks for each registered element. | Using <tt>HTMLElementElement.register()</tt>, authors can register a set of lifecycle callbacks for each registered element. | ||
There are two types of lifecycle event: open and close. The open is for creating the new instance of the Element. | There are two types of lifecycle event: <tt>open</tt> and <tt>close</tt>. The open is for creating the new instance of the <tt>Element</tt>. | ||
You can override the constructor of the element by providing open callback. | You can override the constructor of the element by providing <tt>open</tt> callback. | ||
The close callback provides the way to enhance created instance. This is useful when you use given attributes to define the behavior. | The <tt>close</tt> callback provides the way to enhance created instance. | ||
This is useful when you use given attributes to define the behavior. | |||
To make the definition self-contained, we can put script blocks inside an element element. | To make the definition self-contained, we can put a <tt>script</tt> blocks inside an <tt>element</tt> element. | ||
<pre> | <pre> | ||
Line 58: | Line 59: | ||
</pre> | </pre> | ||
In this example, the script refers a | In this example, the script refers a static property called <tt>HTMLElementElement.current</tt>, | ||
This example also shows how we can provide an open lifecycle callback. | which points the enclosing <tt>element</tt> element. | ||
This example also shows how we can provide an <tt>open</tt> lifecycle callback. | |||
== Standalone Form == | == Standalone Form == | ||
The element registration is done | The element registration is also done in separate HTML files, which registers the element definitions in the host document. | ||
Each HTML file which provides the element registration should have provide attribute on the root html element. | Each HTML file which provides the element registration should have a <tt>provide</tt> attribute on the root html element. | ||
This attribute prevents accidental loading of existing HTML files. | This attribute prevents accidental loading of existing HTML files. | ||
Line 77: | Line 79: | ||
</pre> | </pre> | ||
The host element can load the html with element definition using a link element with its rel attribute set to component. | The host element can load the html with element definition using a <tt>link</tt> element with its <tt>rel</tt> attribute set to component. | ||
<pre> | <pre> | ||
Line 96: | Line 98: | ||
= Shadow DOM Template = | = Shadow DOM Template = | ||
An initial shape of new shadow tree can be defined using template element. | An initial shape of new shadow tree can be defined using a <tt>template</tt> element. | ||
<pre> | <pre> | ||
Line 110: | Line 112: | ||
== The ShadowRoot constructor == | == The ShadowRoot constructor == | ||
The ShadowRoot constructor accepts a template element to instantiate. | The <tt>ShadowRoot</tt> constructor accepts a <tt>template</tt> element to instantiate. | ||
<pre> | <pre> | ||
Line 120: | Line 122: | ||
== Scripting on DOM Template == | == Scripting on DOM Template == | ||
Even though template can be used to define an extra visual representation for an element, | Even though the <tt>template</tt> can be used to define an extra visual representation for an element, | ||
It would be useful if author can attach | It would be useful if author can attach a script for the template-generated shadow tree. | ||
The script element which is direct child of the template element can be used for that purpose. | The <tt>script</tt. element which is direct child of the <tt>template</tt> element can be used for that purpose. | ||
The script block of the element run as an event | The script block of the element run as an event handler. | ||
<pre> | <pre> |
Revision as of 23:03, 24 October 2011
Element Registration
Embedding Form
Let’s start from the minimum form of the element registration. We can use an element element to registering new element name. Now agent recognizes x-comment as HTMLRegisteredElement. This is just a plain old HTML element which has its own name.
<head> <element for="x-comment"></element> </head>
The element element also provides an opportunity to give a behavior for the instance of the registered element.
<head> <element for="x-comment"></element> <script> var e = document.head.querySelector(“[for=x-comment]”); e.close = function(element) { element.addEventListener(“click”, function(evt) { .. }); }; </script> </head>
Using HTMLElementElement.register(), authors can register a set of lifecycle callbacks for each registered element. There are two types of lifecycle event: open and close. The open is for creating the new instance of the Element. You can override the constructor of the element by providing open callback. The close callback provides the way to enhance created instance. This is useful when you use given attributes to define the behavior.
To make the definition self-contained, we can put a script blocks inside an element element.
<head> <element for="x-comment"> <script> class Comment : HTMLElement { this.shadow = new ShawowRoot(this); ..; } HTMLElementElement.current.open = function() { return new Comment(); }}); </script> </element> </head>
In this example, the script refers a static property called HTMLElementElement.current, which points the enclosing element element. This example also shows how we can provide an open lifecycle callback.
Standalone Form
The element registration is also done in separate HTML files, which registers the element definitions in the host document. Each HTML file which provides the element registration should have a provide attribute on the root html element. This attribute prevents accidental loading of existing HTML files.
<html provide> <head> <element for="x-comment">..</element> </head> </html>
The host element can load the html with element definition using a link element with its rel attribute set to component.
<html> <head> <link rel=”component” type=”text/html” href="comment.html"> </head> </html>
A providing HTML doesn’t have its own document nor the global object. Agents use these of the host.
Effective markup vocabulary of the providing HTML is limited. Agents only recognize a head element as a child of the root element when the HTML document is loaded as required.
Shadow DOM Template
An initial shape of new shadow tree can be defined using a template element.
<head> <template id=”comment-template”> <div><content></content></div> </template> </head>
The ShadowRoot constructor
The ShadowRoot constructor accepts a template element to instantiate.
.. this.shadow = new ShadowRoot(this, document.getElementById(“comment-template”)); ..
Scripting on DOM Template
Even though the template can be used to define an extra visual representation for an element, It would be useful if author can attach a script for the template-generated shadow tree. The script</tt. element which is direct child of the template element can be used for that purpose. The script block of the element run as an event handler.
<head> <template id=”comment-template”> <div><content></content></div> <script> if (event) { event.target.addEventListener(“click”), function(evt) { // .. }); } <script> </template> </head>
The shadow-template CSS property (brainstorming)
The template for shadow root also can be applied using CSS property.
.comment { shadow-template: url(#comment-template); }
Note that imperatively-applied shadows always supersede shadows from the styling. Also, there is no way to access style-originated shadows from the scripting environment.