diff --git a/src/wtforms/widgets/core.py b/src/wtforms/widgets/core.py index 5be3f40..4941321 100644 --- a/src/wtforms/widgets/core.py +++ b/src/wtforms/widgets/core.py @@ -30,6 +30,10 @@ def html_params(**kwargs): name of the attribute begins with ``data_`` or ``aria_``, then every underscore will be replaced with a hyphen in the generated attribute. + If multiple arguments resolve to the same attribute name (e.g. ``class`` and + ``class_``), their values will be joined with a space, with ``class_``'s + value appended. + >>> html_params(data_attr='user.name', aria_labeledby='name') 'data-attr="user.name" aria-labeledby="name"' @@ -51,18 +55,42 @@ def html_params(**kwargs): ``data_`` args convert all underscores to hyphens, instead of only the first one. """ - params = [] - for k, v in sorted(kwargs.items()): + # Normalize parameter names and collect values + normalized_params = {} + for k, v in kwargs.items(): + # Convert parameter names if k in ("class_", "class__", "for_"): k = k[:-1] elif k.startswith("data_") or k.startswith("aria_"): k = k.replace("_", "-") + + # Skip False values + if v is False: + continue + + # Handle True values (boolean attributes) if v is True: - params.append(k) - elif v is False: - pass + normalized_params[k] = normalized_params.get(k, []) + [True] else: - params.append('{}="{}"'.format(str(k), escape(v))) + # Collect string values + normalized_params[k] = normalized_params.get(k, []) + [str(v)] + + # Build parameter strings + params = [] + for k in sorted(normalized_params.keys()): + values = normalized_params[k] + # Filter out True values for special handling + true_values = [v for v in values if v is True] + str_values = [v for v in values if isinstance(v, str)] + + if true_values: + # Boolean attribute - present if any True value exists + params.append(k) + elif str_values: + # For class attributes, join multiple values with space + # For other attributes, join with space as well (consistent behavior) + params.append('{}="{}"'.format(k, escape(" ".join(str_values)))) + return " ".join(params)