开发者

Valid Characters in Option Value

开发者 https://www.devze.com 2023-03-07 07:00 出处:网络
I was just w开发者_运维百科ondering what valid characters can be included as the value for a <option>

I was just w开发者_运维百科ondering what valid characters can be included as the value for a <option>

i.e. is this valid?

<select>
    <option value='0dbl,2sgl'>0 Double and 2 Singles</option>
    <option value='1dbl,0sgl'>1 Double and 0 Singles</option>
</select>


Yes, that's perfectly valid. See the specification: it says the content of value should be CDATA, in which pretty much everything is valid, with the following caveats:

  • Replace character entities with characters,
  • Ignore line feeds,
  • Replace each carriage return or tab with a single space.


For HTML4:

OPTION Attribute definitions

selected [CI]
  When set, this boolean attribute specifies that this option is pre-selected.
value = cdata [CS]
  This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.
label = text [CS]
  This attribute allows authors to specify a shorter label for an option than the content of the OPTION element. When specified, user agents should use the value of this attribute rather than the content of the OPTION element as the option label.

Source: http://www.w3.org/TR/html401/interact/forms.html#h-17.6

So we go to the definition of CDATA:

CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:

  • Replace character entities with characters,
  • Ignore line feeds,
  • Replace each carriage return or tab with a single space. User agents may ignore leading and trailing white space in CDATA attribute values (e.g., " myval " may be interpreted as "myval"). Authors should not declare attribute values with leading or trailing white space.

For some HTML 4 attributes with CDATA attribute values, the specification imposes further constraints on the set of legal values for the attribute that may not be expressed by the DTD.

Source: http://www.w3.org/TR/html401/types.html#type-cdata

As there is no constraint noted, the valid content of value must have properly escaped entities, properly defined entities, and be within the scope of the document's encoding.

0

精彩评论

暂无评论...
验证码 换一张
取 消