@@ -425,14 +425,33 @@ final class SizedSpanRepresentation extends LabelRepresentationBehavior {
425425 DomElement get focusTarget => _domText;
426426}
427427
428+ /// The source of the label attribute for a semantic node.
429+ enum LabelSource {
430+ /// The label is provided by the [SemanticsObject.label] property.
431+ label,
432+
433+ /// The label is provided by the [SemanticsObject.value] property.
434+ value,
435+
436+ /// The label is provided by the [SemanticsObject.hint] property.
437+ hint,
438+
439+ /// The label is provided by the [SemanticsObject.tooltip] property.
440+ tooltip,
441+ }
442+
428443/// Renders [SemanticsObject.label] and/or [SemanticsObject.value] to the semantics DOM.
429444///
430445/// The value is not always rendered. Some semantics nodes correspond to
431446/// interactive controls. In such case the value is reported via that element's
432447/// `value` attribute rather than rendering it separately.
433448class LabelAndValue extends RoleManager {
434- LabelAndValue (SemanticsObject semanticsObject, PrimaryRoleManager owner, { required this .preferredRepresentation })
435- : super (Role .labelAndValue, semanticsObject, owner);
449+ LabelAndValue (
450+ SemanticsObject semanticsObject,
451+ PrimaryRoleManager owner, {
452+ required this .preferredRepresentation,
453+ this .labelSources = allLabelSources,
454+ }) : super (Role .labelAndValue, semanticsObject, owner);
436455
437456 /// The preferred representation of the label in the DOM.
438457 ///
@@ -443,6 +462,17 @@ class LabelAndValue extends RoleManager {
443462 /// instead.
444463 LabelRepresentation preferredRepresentation;
445464
465+ /// The sources of the label that are allowed to be used.
466+ final Set <LabelSource > labelSources;
467+
468+ /// All possible sources of the label.
469+ static const Set <LabelSource > allLabelSources = < LabelSource > {
470+ LabelSource .label,
471+ LabelSource .value,
472+ LabelSource .hint,
473+ LabelSource .tooltip,
474+ };
475+
446476 @override
447477 void update () {
448478 final String ? computedLabel = _computeLabel ();
@@ -477,20 +507,34 @@ class LabelAndValue extends RoleManager {
477507 return representation;
478508 }
479509
510+ String ? get _label =>
511+ labelSources.contains (LabelSource .label) && semanticsObject.hasLabel
512+ ? semanticsObject.label
513+ : null ;
514+
515+ String ? get _value =>
516+ labelSources.contains (LabelSource .value) && semanticsObject.hasValue
517+ ? semanticsObject.value
518+ : null ;
519+
520+ String ? get _tooltip =>
521+ labelSources.contains (LabelSource .tooltip) && semanticsObject.hasTooltip
522+ ? semanticsObject.tooltip
523+ : null ;
524+
525+ String ? get _hint =>
526+ labelSources.contains (LabelSource .hint) ? semanticsObject.hint : null ;
527+
480528 /// Computes the final label to be assigned to the node.
481529 ///
482530 /// The label is a concatenation of tooltip, label, hint, and value, whichever
483- /// combination is present.
531+ /// combination is present and allowed by [labelSources] .
484532 String ? _computeLabel () {
485- // If the node is incrementable the value is reported to the browser via
486- // the respective role manager. We do not need to also render it again here.
487- final bool shouldDisplayValue = ! semanticsObject.isIncrementable && semanticsObject.hasValue;
488-
489533 return computeDomSemanticsLabel (
490- tooltip: semanticsObject.hasTooltip ? semanticsObject.tooltip : null ,
491- label: semanticsObject.hasLabel ? semanticsObject.label : null ,
492- hint: semanticsObject.hint ,
493- value: shouldDisplayValue ? semanticsObject.value : null ,
534+ tooltip: _tooltip ,
535+ label: _label ,
536+ hint: _hint ,
537+ value: _value ,
494538 );
495539 }
496540
0 commit comments