mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fix TextSpan's operator==
We forgot to compare the lengths of the lists.
This commit is contained in:
parent
d1154c2573
commit
25219277f6
@ -8,6 +8,19 @@ import 'basic_types.dart';
|
||||
import 'text_editing.dart';
|
||||
import 'text_style.dart';
|
||||
|
||||
// TODO(abarth): Should this be somewhere more general?
|
||||
bool _deepEquals(List<Object> a, List<Object> b) {
|
||||
if (a == null)
|
||||
return b == null;
|
||||
if (b == null || a.length != b.length)
|
||||
return false;
|
||||
for (int i = 0; i < a.length; ++i) {
|
||||
if (a[i] != b[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// An immutable span of text.
|
||||
class TextSpan {
|
||||
const TextSpan({
|
||||
@ -74,19 +87,9 @@ class TextSpan {
|
||||
if (other is! TextSpan)
|
||||
return false;
|
||||
final TextSpan typedOther = other;
|
||||
if (typedOther.text != text)
|
||||
return false;
|
||||
if (typedOther.style != style)
|
||||
return false;
|
||||
if ((typedOther.children == null) != (children == null))
|
||||
return false;
|
||||
if (children != null) {
|
||||
for (int i = 0; i < children.length; ++i) {
|
||||
if (typedOther.children[i] != children[i])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return typedOther.text == text
|
||||
&& typedOther.style == style
|
||||
&& _deepEquals(typedOther.children, children);
|
||||
}
|
||||
int get hashCode => hashValues(style, text, hashList(children));
|
||||
}
|
||||
|
||||
30
packages/flutter/test/painting/text_span_test.dart
Normal file
30
packages/flutter/test/painting/text_span_test.dart
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/painting.dart';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test("TextSpan equals", () {
|
||||
TextSpan a1 = new TextSpan(text: 'a');
|
||||
TextSpan a2 = new TextSpan(text: 'a');
|
||||
TextSpan b1 = new TextSpan(children: <TextSpan>[ a1 ]);
|
||||
TextSpan b2 = new TextSpan(children: <TextSpan>[ a2 ]);
|
||||
TextSpan c1 = new TextSpan();
|
||||
TextSpan c2 = new TextSpan();
|
||||
|
||||
expect(a1 == a2, isTrue);
|
||||
expect(b1 == b2, isTrue);
|
||||
expect(c1 == c2, isTrue);
|
||||
|
||||
expect(a1 == b2, isFalse);
|
||||
expect(b1 == c2, isFalse);
|
||||
expect(c1 == a2, isFalse);
|
||||
|
||||
expect(a1 == c2, isFalse);
|
||||
expect(b1 == a2, isFalse);
|
||||
expect(c1 == b2, isFalse);
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user