Repository navigation
Conversation
d436ffc to
cd52aa6
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces several enhancements to the Craft engine and A2UI integration, including visible field labels for input controls, support for labelled options in Select blocks, and improved reporting for empty catalogs. It also updates the template registry to use deep copies of template libraries to prevent external modifications, refines line/column error reporting to be 1-based, and adds dark mode contrast tests. The review feedback highlights opportunities to simplify pattern matching in selectOptions and recommends defensive null-checks in the newly added dark_mode_test.dart to prevent potential NullCheckError crashes during testing.
| Map<String, String> selectOptions(Object? options) { | ||
| final Map<String, String> result = <String, String>{}; | ||
| if (options is! List<Object?>) return result; | ||
| for (final Object? option in options) { | ||
| switch (option) { | ||
| case final String value: | ||
| result.putIfAbsent(value, () => value); | ||
| case {'value': final String value}: | ||
| result.putIfAbsent( | ||
| value, | ||
| () => switch (option['label']) { | ||
| final String label => label, | ||
| _ => value, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| return result; | ||
| } |
There was a problem hiding this comment.
We can simplify the pattern matching in selectOptions by matching both 'value' and 'label' directly in the map pattern. This avoids the nested switch expression and makes the code more concise and readable.
Map<String, String> selectOptions(Object? options) {
final Map<String, String> result = <String, String>{};
if (options is! List<Object?>) return result;
for (final Object? option in options) {
switch (option) {
case final String value:
result.putIfAbsent(value, () => value);
case {'value': final String value, 'label': final String label}:
result.putIfAbsent(value, () => label);
case {'value': final String value}:
result.putIfAbsent(value, () => value);
}
}
return result;
}| bool _isLarge(TextStyle style) { | ||
| final double size = style.fontSize!; | ||
| final FontWeight weight = style.fontWeight ?? FontWeight.normal; | ||
| return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value); | ||
| } |
There was a problem hiding this comment.
In Dart, TextStyle.fontSize and TextStyle.fontWeight are nullable. To prevent potential NullCheckError runtime exceptions during widget tests, we should defensively handle null values for style and its properties.
| bool _isLarge(TextStyle style) { | |
| final double size = style.fontSize!; | |
| final FontWeight weight = style.fontWeight ?? FontWeight.normal; | |
| return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value); | |
| } | |
| bool _isLarge(TextStyle? style) { | |
| final double? size = style?.fontSize; | |
| if (size == null) return false; | |
| final FontWeight weight = style?.fontWeight ?? FontWeight.normal; | |
| return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value); | |
| } |
| void _checkTexts(WidgetTester tester, String where) { | ||
| int checked = 0; | ||
| for (final Element e | ||
| in find | ||
| .descendant( | ||
| of: find.byType(A2uiSurface), | ||
| matching: find.byType(RichText), | ||
| ) | ||
| .evaluate()) { | ||
| final TextSpan span = (e.renderObject! as RenderParagraph).text as TextSpan; | ||
| final String text = span.toPlainText(); | ||
| if (text.trim().isEmpty) continue; | ||
| final TextStyle style = span.style!; | ||
| final bool icon = e.findAncestorWidgetOfExactType<Icon>() != null; | ||
| expect( | ||
| _contrast(style.color!, _background(e)), | ||
| greaterThanOrEqualTo(icon || _isLarge(style) ? 3 : 4.5), | ||
| reason: '$where: "$text" in ${style.color}, ${style.fontSize} px', | ||
| ); | ||
| checked += 1; | ||
| } | ||
| expect(checked, greaterThan(5), reason: where); | ||
| } |
There was a problem hiding this comment.
In _checkTexts, span.style and style.color are nullable. If a text span does not have an explicit style or color, calling span.style! or style.color! will throw a NullCheckError and crash the test. We should defensively check for nulls.
void _checkTexts(WidgetTester tester, String where) {
int checked = 0;
for (final Element e
in find
.descendant(
of: find.byType(A2uiSurface),
matching: find.byType(RichText),
)
.evaluate()) {
final TextSpan span = (e.renderObject! as RenderParagraph).text as TextSpan;
final String text = span.toPlainText();
if (text.trim().isEmpty) continue;
final TextStyle? style = span.style;
final Color? color = style?.color;
if (color == null) continue;
final bool icon = e.findAncestorWidgetOfExactType<Icon>() != null;
expect(
_contrast(color, _background(e)),
greaterThanOrEqualTo(icon || _isLarge(style) ? 3 : 4.5),
reason: '$where: "$text" in $color, ${style?.fontSize} px',
);
checked += 1;
}
expect(checked, greaterThan(5), reason: where);
}…talogs, Wasm tests
From writing the examples and the getting-started guide:
- Building blocks: TextInput, Select and DatePicker draw their `label` above
the field (label style, disabled color, tapping it focuses the field; the
field keeps its semantics name, and its text and focus when the label
comes or goes). Select options may be `{label, value}` pairs. The example
app and the docs drop their `Text` workarounds.
- templateCatalog reports a catalog made with no components, with the reason
(`emptyCatalogReason`: no template library yet, not a template library, no
exported declaration, none available): to `onEmpty`, else to
FlutterError.reportError and a failing assertion in debug builds.
- Parser: positions at a line break, and at the end of a source that is
empty or ends with one, are at column 1 instead of 0; specified in
craft_text_format, conformance cases pin the columns.
- TemplateRegistry keeps an unmodifiable deep copy of each template library
(open nit from #28), switch keys included, and gives back a new copy of it
with `templateLibrary`, so source positions set on what is read back don't
move the positions it reports; `templateLibraryNames` lists them.
- The conformance harness loads its files under dart2wasm (Node's fs via
process.getBuiltinModule); CI runs the engine tests compiled to Wasm, and
the building blocks tests compiled to Wasm in Chrome. No engine case is
skipped there; web_skips.dart would list one, with its reason.
- dart2wasm 3.14 traps when a switch over an `Object?` matches string
constants against a value of another type: `primitiveArg` (metadata such
as `false`) and `decodeExtent` (every numeric size) test for a String
first.
- fix_format.sh passes dart format's page width and trailing comma style, so
it no longer reads analysis_options.yaml and warns about unresolved
`package:` includes when no `pub get` ran.
- Example app: a dark theme; cards have no opaque background, captions use
the host's text color, and errors use it too next to a red icon, since no
one red reaches 4.5:1 on both themes; badge colors darkened to 4.5:1. A
test checks every text (4.5:1, 3:1 when large) and icon (3:1) in both
themes, for every flight status.
cd52aa6 to
66269f4
Compare
From writing the examples and the getting-started guide:
labelabovethe field (label style, disabled color, tapping it focuses the field; the
field keeps its semantics name, and its text and focus when the label
comes or goes). Select options may be
{label, value}pairs. The exampleapp and the docs drop their
Textworkarounds.(
emptyCatalogReason: no template library yet, not a template library, noexported declaration, none available): to
onEmpty, else toFlutterError.reportError and a failing assertion in debug builds.
empty or ends with one, are at column 1 instead of 0; specified in
craft_text_format, conformance cases pin the columns.
(open nit from feat(craft): add the template registry #28), switch keys included, and gives back a new copy of it
with
templateLibrary, so source positions set on what is read back don'tmove the positions it reports;
templateLibraryNameslists them.process.getBuiltinModule); CI runs the engine tests compiled to Wasm, and
the building blocks tests compiled to Wasm in Chrome. No engine case is
skipped there; web_skips.dart would list one, with its reason.
Object?matches stringconstants against a value of another type:
primitiveArg(metadata suchas
false) anddecodeExtent(every numeric size) test for a Stringfirst.
it no longer reads analysis_options.yaml and warns about unresolved
package:includes when nopub getran.the host's text color, and errors use it too next to a red icon, since no
one red reaches 4.5:1 on both themes; badge colors darkened to 4.5:1. A
test checks every text (4.5:1, 3:1 when large) and icon (3:1) in both
themes, for every flight status.