2012年4月11日 星期三

Use empty-element tag for iframe will not work in Firefox

[HTML][XML][JavaScript]
以下內容之環境為Firefox 11.0

在XML的規則下,start-tag 與 end-tag 中間沒東西的話,可改寫成 empty-element tag
所以 <iframe></iframe> 可以改寫成:
<iframe />

但是,當你把它存成HTML,並用Browser打開它
第一個 <iframe /> 之後的東西都無法顯示
你必須要改成
<iframe></iframe>
才能正常顯示

另外,將xml字串轉換成DOM物件,再將DOM物轉回xml字串,他會改變你的字串
var xmlText = "<iframe></iframe>";
var parser = new DOMParser();
//轉換成DOM物件
var xmlDoc = parser.parseFromString( xmlText, "text/xml" );
var serializer = new XMLSerializer();
//再將DOM物轉回xml字串
var newXmlText = serializer.serializeToString( xmlDoc );

newXmlText 會等於 "<iframe />"
也就是他會自動把 "<iframe></iframe>" 轉成 "<iframe />"

但是,如果xmlText的 <iframe></iframe> 的中間有個空白鍵
那他就不會自動轉換
也就是說 "<iframe> </iframe>" 依舊還是 "<iframe> </iframe>"

補充:
<iframe> Tag Syntax --- Rules for coding HTML iframe elements
http://www.html-5.com/tags/iframe-tag/syntax.html
...The iframe element is an empty element, with both a start tag and an end tag, not a void element.

Definition of HTML "void element"
http://www.html-5.com/definitions/#void-element

Definition of "empty element"
http://www.html-5.com/definitions/#empty-element


再補充:
,-<http://www.w3.org/TR/xml11/#sec-starttags>
|
| [...]
| Tags for Empty Elements
|
| [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' [WFC: Unique Att Spec]
|
| Empty-element tags may be used for any element which has no content,
| whether or not it is declared using the keyword EMPTY. For
| interoperability, the empty-element tag SHOULD be used, and SHOULD
| only be used, for elements which are declared EMPTY.

See also <http://www.w3.org/TR/xhtml-media-types/#compatGuidelines>:

| A.3. Elements that have no content
|
| If an element permits content (e.g., the div element) but an instance
| of that element has no content (e.g., an empty section), DO NOT use
| the "minimized" tag syntax (e.g., <div />).
|
| Rationale: HTML user agents may give uncertain results when using the
| [...] minimized syntax permitted by XML when an element has no content.

沒有留言:

張貼留言