{"action":"created","issue":{"url":"https://api.github.com/repos/w3c/webcomponents/issues/716","repository_url":"https://api.github.com/repos/w3c/webcomponents","labels_url":"https://api.github.com/repos/w3c/webcomponents/issues/716/labels{/name}","comments_url":"https://api.github.com/repos/w3c/webcomponents/issues/716/comments","events_url":"https://api.github.com/repos/w3c/webcomponents/issues/716/events","html_url":"https://github.com/w3c/webcomponents/issues/716","id":280602178,"number":716,"title":"Scoped Custom Element Registries","user":{"login":"justinfagnani","id":522948,"avatar_url":"https://avatars0.githubusercontent.com/u/522948?v=4","gravatar_id":"","url":"https://api.github.com/users/justinfagnani","html_url":"https://github.com/justinfagnani","followers_url":"https://api.github.com/users/justinfagnani/followers","following_url":"https://api.github.com/users/justinfagnani/following{/other_user}","gists_url":"https://api.github.com/users/justinfagnani/gists{/gist_id}","starred_url":"https://api.github.com/users/justinfagnani/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinfagnani/subscriptions","organizations_url":"https://api.github.com/users/justinfagnani/orgs","repos_url":"https://api.github.com/users/justinfagnani/repos","events_url":"https://api.github.com/users/justinfagnani/events{/privacy}","received_events_url":"https://api.github.com/users/justinfagnani/received_events","type":"User","site_admin":false},"labels":[{"id":228779701,"url":"https://api.github.com/repos/w3c/webcomponents/labels/custom-elements","name":"custom-elements","color":"5319e7","default":false},{"id":841509738,"url":"https://api.github.com/repos/w3c/webcomponents/labels/needs%20concrete%20proposal","name":"needs concrete proposal","color":"1cddc7","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":30,"created_at":"2017-12-08T20:05:07Z","updated_at":"2018-04-24T03:29:28Z","closed_at":null,"author_association":"NONE","body":"Since #488 is closed, I thought I'd open up a new issue to discuss a relatively specific proposal I have for Scoped Custom Element Registries.\r\n\r\n# Scoped Custom Element Definitions\r\n\r\n## Overview\r\n\r\nScoped Custom Element definitions is an oft-requested feature of Web Components. The global registry is a possible source of name collisions that may arise from coincidence, or from **an app trying to define multiple versions of the same element**, or from more advanced scenarios like registering mocks during tests, or a component explicitly replacing an element definition for its scope.\r\n\r\nSince the key DOM creation APIs are global, scoping definitions is tricky because we'd need a machanis to determind which scope to use. But if we offer scoped versions of these APIs the problem is tractable. This requires that DOM creation code is upgraded to use the new scoped APIs, something that hopefully could be done in template libraries and frameworks.\r\n\r\nThis proposal adds the ability to construct `CustomElementRegistry`s and chain them in order to inherit custom element definitions. It uses `ShadowRoot` as a scope for definitions. `ShadowRoot` can be associated with a `CustomElementRegistry` when created and gains element creation methods, like `createElement`. When new elements are created within a `ShadowRoot`, that `ShadowRoot`'s registry is used to Custom Element upgrades.\r\n\r\n## API Changes\r\n\r\n### `CustomElementRegistry`\r\n\r\n  * `CustomElementRegistry(parent?: CustomElementRegistry)`\r\n \r\n    `CustomElementRegistry` is constructable, and able to inherit from a parent registry.\r\n    \r\n    New definitions added to a registry are not visible to the parent, and mask any registrations with the same name defined in the parent so that definitions can be overridden.\r\n\r\n  * `CustomElementRegistry.prototype.get(name: string)`\r\n\r\n    `get()` now returns the closest constructor defined for a tag name in a chain of registries.\r\n\r\n  * `CustomElementRegistry.prototype.getRegistry(name: string)`\r\n\r\n    Returns the closest registry in which a tag name is defined.\r\n\r\n### `ShadowRoot`\r\n\r\n`ShadowRoot`s are already the scoping boundary for DOM and CSS, so it's natural to be the scope for custom elements. `ShadowRoot` needs a `CustomElementRegistry` and the DOM creation APIs that current exist on document.\r\n\r\n  * `customElements: CustomElementRegistry`\r\n  \r\n    The `CustomElementRegistry` the `ShadowRoot` uses, set on `attachShadowRoot()`.\r\n  * `createElement()`, `createElementNS()`\r\n    These methods create new elements using the `CustomElementRegistry` of the `ShadowRoot`.\r\n  * `importNode()`\r\n    Imports a node into the document that owns the `ShadowRoot`, using the `CustomElementRegistry` of the `ShadowRoot`.\r\n\r\n    This enables cloning a template into multiple scopes to use different custom element definitions.\r\n\r\n### `Element`\r\n\r\nNew properties:\r\n  * `Element.prototype.scope: Document | ShadowRoot`\r\n    Elements have DOM creation APIs, like `innerHTML`, so they need a reference to their scope. Elements expose this with a `scope` property. One difference between this and `getRootNode()` is that the scope for an element can never change.\r\n\r\n  * `Element.prototype.attachShadow(init: ShadowRootInit)`\r\n\r\n    `ShadowRootInit` adds a new property, `customElements`, in its options argument which is a `CustomElementRegistry`.\r\n\r\nWith a scope, DOM creation APIs like `innerHTML` and `insertAdjacentHTML` will use the element's `scope`'s registry to construct new custom elements. Appending or inserting an existing element doesn't use the scope, nor does it change the scope of the appended element. Scopes are completely defined when an element is created.\r\n\r\n## Example\r\n\r\n```js\r\n// x-foo.js is an existing custom element module that registers a class\r\n// as 'x-foo' in the global registry.\r\nimport {XFoo} from './x-foo.js';\r\n\r\n// Create a new registry that inherits from the global registry\r\nconst myRegistry = new CustomElementRegistry(window.customElements);\r\n\r\n// Define a trivial subclass of XFoo so that we can register it ourselves\r\nclass MyFoo extends XFoo {}\r\n\r\n// Register it as `my-foo` locally.\r\nmyRegistry.define('my-foo', MyFoo);\r\n\r\nclass MyElement extends HTMLElement {\r\n  constructor() {\r\n    super();\r\n    // Use the local registry when creating the ShadowRoot\r\n    this.attachShadow({mode: 'open', customElements: myRegistry});\r\n\r\n    // Use the scoped element creation APIs to create elements:\r\n    const myFoo = this.shadowRoot.createElement('my-foo');\r\n    this.shadowRoot.appendChild(myFoo);\r\n\r\n    // myFoo is now associated with the scope of `this.shadowRoot`, and registy\r\n    // of `myRegistry`. When it creates new DOM, is uses `myRegistry`:\r\n    myFoo.innerHTML = `<my-bar></my-bar>`;\r\n  }\r\n}\r\n```\r\n\r\n## Questions\r\n  * What happens to existing upgraded elements when an overriding definition is added to a child registry?\r\n \r\n    The simplest answer is that elements are only ever upgraded once, and adding a new definition that's visible in an element's scope will not cause a re-upgrade or prototype change.\r\n  * Should classes only be allow to be defined once, across all registries?\r\n    \r\n    This would preserve the 1-1 relationship between a class and a tag name and the ability to do `new MyElement()` even if a class is not registered in the global registry.\r\n\r\n    It's easy to define a trivial subclass if there's a need to register the same class in different registries or with different names.\r\n  * Should registries inherit down the tree-of-trees by default, or only via the parent chain of `CustomElementRegistry`?\r\n \r\n    Inheriting down the DOM tree leads to dynamic-like scoping where definitions can change depending on your position in the tree. Restricting to inheriting in `CustomElementRegistry` means there's a fixed lookup path.\r\n * Should the registry of a `ShadowRoot` be final?\r\n * Is `Element.prototype.scope` neccessary?\r\n\r\n   It requires all elements to remember where they were created, possibly increasing their memory footprint. Scopes could be dynamically looked up during new DOM creation via the `getRootNode()` process instead, but this might slow down operations like `innerHTML`.\r\n  * How does this interact with the Template Instantiation proposal?\r\n    \r\n    With Template Instantiation `document.importNode()` isn't used to create template instances, but `HTMLTemplateElement.prototype.createInstance()`. How will that know which scope to use? Should it take a registry or `ShadowRoot`?\r\n\r\n/cc @domenic @rniwa @hayatoito @TakayoshiKochi\r\n"},"comment":{"url":"https://api.github.com/repos/w3c/webcomponents/issues/comments/383792143","html_url":"https://github.com/w3c/webcomponents/issues/716#issuecomment-383792143","issue_url":"https://api.github.com/repos/w3c/webcomponents/issues/716","id":383792143,"user":{"login":"lacolaco","id":1529180,"avatar_url":"https://avatars3.githubusercontent.com/u/1529180?v=4","gravatar_id":"","url":"https://api.github.com/users/lacolaco","html_url":"https://github.com/lacolaco","followers_url":"https://api.github.com/users/lacolaco/followers","following_url":"https://api.github.com/users/lacolaco/following{/other_user}","gists_url":"https://api.github.com/users/lacolaco/gists{/gist_id}","starred_url":"https://api.github.com/users/lacolaco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lacolaco/subscriptions","organizations_url":"https://api.github.com/users/lacolaco/orgs","repos_url":"https://api.github.com/users/lacolaco/repos","events_url":"https://api.github.com/users/lacolaco/events{/privacy}","received_events_url":"https://api.github.com/users/lacolaco/received_events","type":"User","site_admin":false},"created_at":"2018-04-24T03:29:28Z","updated_at":"2018-04-24T03:29:28Z","author_association":"NONE","body":"Wow, I didn't know that! Thank you for the information!"}}
{"actor":{"display_login":"lacolaco"}}