Skip to content

Commit b715a5c

Browse files
committed
Make sure that enum documentation contains unique IDs for animations
1 parent 5983455 commit b715a5c

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

lib/src/model.dart

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,13 +4452,17 @@ abstract class ModelElement extends Canonicalization
44524452
// Matches valid javascript identifiers.
44534453
final RegExp validIdRegExp = RegExp(r'^[a-zA-Z_]\w*$');
44544454

4455-
final Set<String> uniqueIds = Set<String>();
4455+
// Make sure we have a set to keep track of used IDs for this href.
4456+
package.usedAnimationIdsByHref[href] ??= {};
4457+
44564458
String getUniqueId(String base) {
4457-
int count = 1;
4458-
String id = '$base$count';
4459-
while (uniqueIds.contains(id)) {
4460-
count++;
4461-
id = '$base$count';
4459+
int animationIdCount = 1;
4460+
String id = '$base$animationIdCount';
4461+
// We check for duplicate IDs so that we make sure not to collide with
4462+
// user-supplied ids on the same page.
4463+
while (package.usedAnimationIdsByHref[href].contains(id)) {
4464+
animationIdCount++;
4465+
id = '$base$animationIdCount';
44624466
}
44634467
return id;
44644468
}
@@ -4476,7 +4480,7 @@ abstract class ModelElement extends Canonicalization
44764480
bool wasDeprecated = false;
44774481
if (positionalArgs.length == 4) {
44784482
// Supports the original form of the animation tag for backward
4479-
// compatibility.
4483+
// compatibility.`
44804484
uniqueId = positionalArgs.removeAt(0);
44814485
wasDeprecated = true;
44824486
} else if (positionalArgs.length == 3) {
@@ -4496,13 +4500,13 @@ abstract class ModelElement extends Canonicalization
44964500
'and must not begin with a number.');
44974501
return '';
44984502
}
4499-
if (uniqueIds.contains(uniqueId)) {
4503+
if (package.usedAnimationIdsByHref[href].contains(uniqueId)) {
45004504
warn(PackageWarning.invalidParameter,
45014505
message: 'An animation has a non-unique identifier, "$uniqueId". '
45024506
'Animation identifiers must be unique.');
45034507
return '';
45044508
}
4505-
uniqueIds.add(uniqueId);
4509+
package.usedAnimationIdsByHref[href].add(uniqueId);
45064510

45074511
int width;
45084512
try {
@@ -4552,7 +4556,8 @@ abstract class ModelElement extends Canonicalization
45524556
45534557
<div style="position: relative;">
45544558
<div id="${overlayId}"
4555-
onclick="if ($uniqueId.paused) {
4559+
onclick="var $uniqueId = document.getElementById('$uniqueId');
4560+
if ($uniqueId.paused) {
45564561
$uniqueId.play();
45574562
this.style.display = 'none';
45584563
} else {
@@ -4569,7 +4574,8 @@ abstract class ModelElement extends Canonicalization
45694574
</div>
45704575
<video id="$uniqueId"
45714576
style="width:${width}px; height:${height}px;"
4572-
onclick="if (this.paused) {
4577+
onclick="var $overlayId = document.getElementById('$overlayId');
4578+
if (this.paused) {
45734579
this.play();
45744580
$overlayId.style.display = 'none';
45754581
} else {
@@ -6294,6 +6300,10 @@ class Package extends LibraryContainer
62946300
/// Number of times we have invoked a tool for this package.
62956301
int toolInvocationIndex = 0;
62966302

6303+
// The animation IDs that have already been used, indexed by the [href] of the
6304+
// object that contains them.
6305+
Map<String, Set<String>> usedAnimationIdsByHref = {};
6306+
62976307
/// Pieces of the location split by [locationSplitter] (removing package: and
62986308
/// slashes).
62996309
@override

test/model_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,16 @@ void main() {
11761176
Method withAnimationInOneLineDoc;
11771177
Method withAnimationInline;
11781178
Method withAnimationOutOfOrder;
1179+
Enum enumWithAnimation;
1180+
EnumField enumValue1;
1181+
EnumField enumValue2;
11791182

11801183
setUpAll(() {
1184+
enumWithAnimation = exLibrary.enums.firstWhere((c) => c.name == 'EnumWithAnimation');
1185+
enumValue1 = enumWithAnimation.constants
1186+
.firstWhere((m) => m.name == 'value1');
1187+
enumValue2 = enumWithAnimation.constants
1188+
.firstWhere((m) => m.name == 'value2');
11811189
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
11821190
withAnimation =
11831191
dog.allInstanceMethods.firstWhere((m) => m.name == 'withAnimation');
@@ -1237,6 +1245,14 @@ void main() {
12371245
expect(withAnimationOutOfOrder.documentation,
12381246
contains('<video id="outOfOrder"'));
12391247
});
1248+
test("Enum field animation identifiers are unique.", () {
1249+
expect(enumValue1.documentationAsHtml, contains('<video id="animation_1"'));
1250+
expect(enumValue1.documentationAsHtml, contains('<video id="animation_2"'));
1251+
expect(enumValue2.documentationAsHtml, isNot(contains('<video id="animation_1"')));
1252+
expect(enumValue2.documentationAsHtml, isNot(contains('<video id="animation_2"')));
1253+
expect(enumValue2.documentationAsHtml, contains('<video id="animation_3"'));
1254+
expect(enumValue2.documentationAsHtml, contains('<video id="animation_4"'));
1255+
});
12401256
});
12411257

12421258
group('MultiplyInheritedExecutableElement handling', () {

testing/test_package/lib/example.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,23 @@ class Dog implements Cat, E {
452452
void abstractMethod() {}
453453
}
454454

455+
/// Animation in an enum
456+
enum EnumWithAnimation {
457+
/// Animation enum value1
458+
///
459+
/// {@animation 100 100 http://host/path/to/video1.mp4}
460+
/// {@animation 100 100 http://host/path/to/video2.mp4}
461+
/// More docs
462+
value1,
463+
464+
/// Animation enum value2
465+
///
466+
/// {@animation 100 100 http://host/path/to/video1.mp4}
467+
/// {@animation 100 100 http://host/path/to/video2.mp4}
468+
/// More docs
469+
value2,
470+
}
471+
455472
abstract class E {}
456473

457474
class F<T extends String> extends Dog with _PrivateAbstractClass {
@@ -678,4 +695,4 @@ extension on Object {
678695
/// This class has nothing to do with [_Shhh], [FancyList], or [AnExtension.call],
679696
/// but should not crash because we referenced them.
680697
/// We should be able to find [DocumentThisExtensionOnce], too.
681-
class ExtensionReferencer {}
698+
class ExtensionReferencer {}

0 commit comments

Comments
 (0)