pug

Why is a true value rendered as "value"?


I have:

input(type="hidden" name="x" value=pax?pax.C1a:undefined)

When pax as passed to response.render is { C1a: true } the output is rendered as:

<input type="hidden" name="x" value="value" />

Shouldn't it be "true"?

When pax.C1a is false, it is rendered as:

<input type="hidden" name="x" />

Solution

  • From HTML5 spec:

    A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

    If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

    Note: The values "true" and "false" are not allowed on boolean attributes.

    <input type="hidden" name="x" value="value" /> is the second case mentioned in the second paragraph I quoted; the first case would be <input type="hidden" name="x" value />, both representing the true boolean. The note explicitly says what you think should be happening (value="true") should not be happening.

    If you really want value="true", you have to treat it as a text attribute, and render with { C1a: "true" }.