Skip to content

Instantly share code, notes, and snippets.

@teramako
Last active December 14, 2015 21:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teramako/5148880 to your computer and use it in GitHub Desktop.
Save teramako/5148880 to your computer and use it in GitHub Desktop.
WebComponents の CustomElements の要素名は "x-" で始まるべきか。

#WebComponents

Mozillaが先日WebComponents系のdocument.registerBug783129で実装した。 使用するには、about:config 等から dom.webcomponents.enabledtrue にする必要がある。

##カスタム要素は "x-" から始まっているべきか

テストコードを書いていて気付いたのだが、Mozillaの実装では document.registerの第一引数のタグ名が x- から始まっていないと InvalidCharacterError の例外が発生する。実際にコードもそのものずばりで書かれてある。

しかし、CustomElementscustom element nameの項を見ると、以下のように書かれており、-(ハイフン)は必要だと書いているが x- である必要性は説いていない。

The custom element name identifies a custom element and is a sequence of alphanumeric ASCII characters that must match the NCName production and contain a U+002D HYPHEN-MINUS character.

ところが、Introduction5 Custom Elementsの項を見ると、以下のように書かれており、x- プレフィックスが必要だと書いてある。

The name attribute specifies the name of the custom element, by which it will be referred to in markup. The naming space of the name is the same as element tag names, and to avoid collisions, the x- prefix is required for custom element value.

こちらの記述は、<element extends="button" name="x-fancybutton">...</element> であり、document.register とは違うものの、カスタム要素を作る方法が違うだけでネーミングルールは一緒であるべきだ。

その意味で、Mozillaはより厳しい方を選んだと言える。 ただ、個人的には、要素名の衝突を防ぐために"x-"を入れろという主張は理解できない。 既存のHTML5要素との衝突は回避できるが、そんなのは当たり前の話だし、"-" を含まなければならないルールで十分。 むしろ、"x-"などという固定文字列にすると、別々のライブラリ作者が同名のカスタム要素を作ってしまう可能性が出てくる。 それなら"-"を含むルールにして「名前空間的なものをprefixにすると良い」みたいにした方が柔軟で良いと思うのだが。

###W3C の Bugzilla(2013-08-13T21:30:00:09:00) [W3C-Bug14967]にx-プレフィックスなんてそもそも格好悪いぜっていうバグがたってた。 comment 3にXBLの様にCSSでバインディングする方がスマートだ的なことが書かれてて激しく同意。

##Mozilla の Bugzillaの議論

x-プレフィックスに関してはBug783129でも少し出ていた。

comment 21:

(In reply to :Ms2ger from comment #20)

if (StringBeginsWith(elementName, NS_LITERAL_STRING("x-"))) {

The spec requires that all registered elements' names begin with "x-".

気になるのはコレ、僕が読んだ限りでは"-"の必要性はあるけど、document.register側には"x-"なんてのは出てこないんだよね。 古い仕様を見ちゃっているんじゃないかなと思う。

どうでもいいけど、Ms2gerさんのへその曲げ方が面白かった。

comment 20: Ms2ger

But then I saw

if (StringBeginsWith(elementName, NS_LITERAL_STRING("x-"))) {

comment 23: Ms2ger

My main question is this: will document.createElement("x-foo") or in markup be the expected way to use your own foo-thing? If so, we don't want to do that because the fallback and a11y story sucks.

comment 26: Ms2ger

Alright, then this is unacceptable.

Status: NEW → RESOLVED Resolution: --- → WONTFIX

最後に勝手にWONTFIXにして去っているしw

まぁそれはともかく、WebComponentsに対応していないUAへの対応は面倒そうだと思う(Ms2gerさんの指摘ってコレのことだと思ってる)

[W3C-Bug14967]: https://www.w3.org/Bugs/Public/show_bug.cgi?id=14967 "Bug 14967 – [Custom Elements]: Find a way to avoid "x-" prefix because it's ugly"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment