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 IRC (such as one of these permanent autoconfirmed members) or send an e-mail to admin@wiki.whatwg.org with your desired username and an explanation of the first edit you'd like to make. (Do not use this e-mail address for any other inquiries, as they will be ignored or politely declined.)

Introduction

The Component Model introduces comprehensive support for creating DOM elements. Examples include layout managers, combinations of Dojo and jQuery widgets, isolated widgets, such as Like/+1 buttons, and built-in HTML elements themselves. Reflecting on the experience of Mozilla's XBL and Microsoft Internet Explorer's HTML components, the Component Model formalizes the concept of loosely coupled, coherent units of behavior in the Web platform. The functionality goals of the Component Model resemble the goals of XBL2; unlike XBL2, the Component Model seeks to be more incremental and modular while leveraging and integrating with new technologies.

Properties

The component model aims to satisfy several key properties. These properties were extracted from the use cases. This section explains component model basics by studying each property.

Extensibility

The component model enables creation of new types of DOM elements by allowing the use of existing DOM elements in JavaScript prototype chain. For example, here's how you would create a new sub-type of HTMLElement:

function LayoutManagerPanel() {
HTMLElement.call(this);
}
LayoutManagerPanel.prototype = Object.create(HTMLElement.prototype);
// ...
// 1) Will not be able to add instances of LayoutManagerPanel to document without this call.
// 2) First parameter specifies the tagName of the element being registered and must be a string prefixed with "x-".
Element.register("x-layout-manager-panel", LayoutManagerPanel);
// ...
var panel = new LayoutManagerPanel();
document.body.appendChild(panel);
// or
document.body.innerHTML = "<x-layout-manager-panel></x-layout-manager-panel>";

The resulting panel instance is a Javascript object that is a valid DOM element, which can be added to the DOM tree. You can then extend this object using standard Javascript prototype inheritance.

Consistency

Because components are just DOM objects, they inherently share the same traversal and manipulation APIs, as defined by the DOM4. The authors of components can extend these APIs by adding custom methods and properties on DOM objects, using standard JavaScript inheritance:

Encapsulation

Encapsulation refers to ability of the component to hide its implementation details and state from the document. To enable hiding of the implementation details, the component model provides a way to build a DOM tree that is not accessible from the document DOM tree, but is rendered as part of the document. This DOM tree, associated with the component is the shadow DOM. The boundary between the document DOM tree and shadow DOM tree provides complete encapsulation, and ensures that:

Every DOM element instance may only have (or host) one shadow DOM tree. A shadow tree is instantiated by creating an instance of the ShadowRoot object, which takes a reference to the hosting element as a parameter. Attempting to create more than one instance for the same element throws an exception:

Composability

Being DOM objects, components fit naturally into the document DOM tree and support all of its composition properties. In addition, the content element allows controlling interaction between shadow and document DOM trees. A content element specifies places where immediate document tree children of the component are rendered inside the shadow tree.

There can be more than one content element in the shadow tree. The includes attribute provides a convenient way to sort element's children by CSS selector. For example, a DockLayoutPanel component could be used like this in the document DOM tree:

Desugaring

The component model also explains pretty well how the native HTML elements are built and dispels some of the magic associated with the "DOM object vs. JavaScript object" juxtaposition.

Allowing DOM elements to participate in the JavaScript inheritance chain makes DOM elements more approachable and easier to work with.

Complex DOM elements that are rendered with more than one CSS box (and aren't specified in terms of CSS, like lists) are just components that have shadow DOM. Coincidentally, this also explains why you can't add shadow DOM subtrees to input or details elements -- their ShadowRoots are claimed by HTMLInputElement and HTMLDetailsElement constructors, respectively.

Performance

Considering the way Web works, the component model must allow decoupling of the instantiation and the declaration of the components in order to provide reasonable performance characteristics. It's an unfortunate, but necessary requirement. In other words, we must be able to handle situations where a component instance is created before it is declared. Here's an simple example:

In this situation, there is no room for error: view.jsmust wait for layout.js to load before executing. You can't load layout.js lazily or in any different order, since it defines the components that are used in view.js. Given that asynchronous, deferred or lazy-loading of script are all common performance techniques in today's Web, we must do better than block or throw an exception in such cases.

The component model offers this solution:

When an unknown DOM element with an "x-"-prefixed tagName is encountered, we put a placeholder HTMLUnknownElement instance in its place. As soon as the element is defined, all placeholder instances are replaced (Document.renameNode) with the new, proper DOM element.

Confinement

Building Blocks

The component model is comprised of the following building blocks.

Shadow DOM

Any DOM element can host a shadow DOM subtree. The shadow DOM subtree originates at ShadowRoot, which is coupled the hosting element at the time of its construction. You don't need any other building blocks in order to take advantage of the shadow DOM:

A ShadowRoot instance is a Node, and acts as the root of the element's shadow DOM subtree. The ShadowRoot itself is never rendered, nor has styles. In this regard, it's similar to the DocumentFragment. It has two properties:

applyAuthorSheets, which is either true (that is, apply author style sheets from the document), or false (don't);

shadowHost, which points to the hosting element.

Content Element

The content element is used with Shadow DOM and provides a mechanism for distributing hosting element's children inside of its shadow subtree. To preserve the encapsulation property, the content elements act as insertion points and do not leak any information about hosting element's children to the shadow DOM subtree or vise versa.

Constructable DOM Types

The inability to construct DOM element using new (with some exceptions) or use them in the prototype inheritance chain had confounded many Web developers since the beginning of DOM. This building block intends to rectify at least the latter by allowing HTMLElement.call invocation and thus enabling creation of JavaScript objects with DOM elements in the prototype chain.

Registering Elements

Working in conjunction with Constructable DOM Types, this building block makes the list of valid markup tag names extensible by exposing Element.register:

It is possible to envisage a milder (though less elegant) version of element registration by keeping DOM element construction magical (thus decoupling it from Constructable DOM Types) and making Element.register use a callback, rather than constructor as parameter. The callback would be invoked with an already-constructed DOM object with the specified tagName, leaving the work of setting up properties on this object to the callback.

Confinement Primitives

The API surface of the component model lends itself well to proper confinement. Here's an approach that could be used to provide it (very early brainstorming):

Confinement is not tied to the component model. Instead, it's a new twist on the method of loading scripts. A script could be loaded as usual or it could be confined, or loaded into its own context.

When confined, a script has its own global and document objects. These objects only reveal some safe limited subset of the actual document objects. Think of it as a same-origin iframe with restrictions on document and window.

You can communicate with the main document using window.postMessage.

The confined document DOM tree is always empty. Attempting to insert into it throws exceptions.

... except when you append to elements in shadow DOM. That's right, you can do Element.register and new ShadowRoot in the confined document.

Whenever you register an element, it registers in the main document as well. Creating an instance of a component from an confined document produces a functional DOM element shell that proxies to the actual element in the confined document.

Proposed API: introduce a new confined attribute to the script element. Presence of this attribute triggers loading scripts in the confined context.

Confinement of script execution could be useful outside of the component model and also could be related to Content Security Policy.

Almost. Implementation provides ways to add properties, methods, events to element API, and the components are almost DOM elements. However, custom tags don't support all properties of a DOM element.

Not much. There are some provisions for event encapsulation, nothing else.

Not much. No provisions for handling document children vs. component-defined children.

No.

Not sure.

No.

XBL

No. The mechanism for extending bindings is entirely independent of DOM type or Javascript extensibility.

Yes-ish. Bindings can override properties on the DOM element, although the mechanism is highly unnatural (XML declarations).

Yes? Hard to say looking at TR note.

Yes

Yes. Some HTML elements in Gecko are implemented using it.

Not sure.

No.

XBL2

No. Same as XBL.

No. Bindings are discouraged from establishing an API surface on a bound element.

Yes.

Yes.

No. The spec didn't intend to describe how native elements are built.

Somewhat. The lifecycle of a binding is well-defined and initialization stages are well-decoupled. However, there is no way to build a binding imperatively without loading a binding document.

No.

The key differences between XBL2 and the Component Model are:

The Component Model fully embraces the element behavior attachment by allowing creation of new types of DOM elements within a document. This is something Web Framework developers always wanted and repeatedly try to do with parallel object hierarchies. Eliminating ephemerality of the attachment simplifies and clarifies how things work conceptually -- there is no longer a place for "spooky action at a distance", caused by a change of a random CSS selector or even removing an element from the tree.

The Component Model is strongly biased toward reusing existing bits of the platform and well-established techniques instead of inventing new ones. For instance, instead of bindings inheritance, we rely on prototype inheritance. Instead of endeavoring to solve the incredibly complex, yet dubious problem of multiple bindings or shadow DOM trees, we simply suggest composition.

The Component Model strives to add capabilities in layers (see building blocks), each able to function on its own. The omnibus nature of XBL2 removes opportunities for layering, particularly as element identity, life cycle, and scripting are concerned. Perhaps this could be repaired somehow, but Component Model solves these issues without further complicating an already large, difficult to understand spec.