@@ -1164,11 +1164,12 @@ - (BOOL)runAdoptionAgencyAlgorithmForTagName:(NSString *)tagName
1164
1164
[self addParseError:@"Adoption agency formatting element not current"];
1165
1165
}
1166
1166
HTMLElement *furthestBlock;
1167
- for (NSUInteger i = [_stackOfOpenElements indexOfObject:formattingElement] + 1;
1168
- i < _stackOfOpenElements.count ; i++)
1167
+ for (NSUInteger i = [_stackOfOpenElements indexOfObject:formattingElement] + 1, end = _stackOfOpenElements.count ;
1168
+ i < end ; i++)
1169
1169
{
1170
- if (IsSpecialElement([_stackOfOpenElements objectAtIndex:i])) {
1171
- furthestBlock = [_stackOfOpenElements objectAtIndex:i];
1170
+ HTMLElement *element = [_stackOfOpenElements objectAtIndex:i];
1171
+ if (IsSpecialElement(element)) {
1172
+ furthestBlock = element;
1172
1173
break;
1173
1174
}
1174
1175
}
@@ -1181,13 +1182,17 @@ - (BOOL)runAdoptionAgencyAlgorithmForTagName:(NSString *)tagName
1181
1182
return YES;
1182
1183
}
1183
1184
HTMLElement *commonAncestor = [_stackOfOpenElements objectAtIndex:[_stackOfOpenElements indexOfObject:formattingElement] - 1];
1184
- NSUInteger bookmark = [_activeFormattingElements indexOfObject:formattingElement];
1185
+ HTMLElement *beforeBookmark, *afterBookmark; {
1186
+ NSUInteger bookmark = [_activeFormattingElements indexOfObject:formattingElement];
1187
+ if (bookmark > 0) beforeBookmark = [_activeFormattingElements objectAtIndex:bookmark - 1];
1188
+ if ((bookmark + 1) < _activeFormattingElements.count) afterBookmark = [_activeFormattingElements objectAtIndex:bookmark + 1];
1189
+ }
1185
1190
HTMLElement *node = furthestBlock, *lastNode = furthestBlock;
1186
1191
NSUInteger nodeIndex = [_stackOfOpenElements indexOfObject:node];
1187
1192
NSInteger innerLoopCounter = 0;
1188
1193
while (YES) {
1189
1194
innerLoopCounter += 1;
1190
-
1195
+
1191
1196
nodeIndex -= 1;
1192
1197
node = [_stackOfOpenElements objectAtIndex:nodeIndex];
1193
1198
@@ -1201,16 +1206,23 @@ - (BOOL)runAdoptionAgencyAlgorithmForTagName:(NSString *)tagName
1201
1206
[_stackOfOpenElements removeObject:node];
1202
1207
continue;
1203
1208
}
1204
-
1205
- HTMLElement *clone = [node copy];
1206
- [_activeFormattingElements replaceObjectAtIndex:[_activeFormattingElements indexOfObject:node]
1207
- withObject:clone];
1208
- [_stackOfOpenElements replaceObjectAtIndex:[_stackOfOpenElements indexOfObject:node]
1209
- withObject:clone];
1210
- node = clone;
1211
-
1209
+
1210
+ {
1211
+ HTMLElement *clone = [node copy];
1212
+ [_activeFormattingElements replaceObjectAtIndex:[_activeFormattingElements indexOfObject:node]
1213
+ withObject:clone];
1214
+ if (beforeBookmark == node) beforeBookmark = clone;
1215
+ if (afterBookmark == node) afterBookmark = clone;
1216
+ [_stackOfOpenElements replaceObjectAtIndex:[_stackOfOpenElements indexOfObject:node]
1217
+ withObject:clone];
1218
+ node = clone;
1219
+ }
1220
+
1212
1221
if ([lastNode isEqual:furthestBlock]) {
1213
- bookmark = [_activeFormattingElements indexOfObject:node];
1222
+ // "move the aforementioned bookmark to be immediately after the new node" -> new node is the before-bookmark
1223
+ beforeBookmark = node;
1224
+ NSUInteger bookmark = [_activeFormattingElements indexOfObject:node];
1225
+ afterBookmark = ((bookmark + 1) < _activeFormattingElements.count) ? [_activeFormattingElements objectAtIndex:bookmark + 1] : nil;
1214
1226
}
1215
1227
1216
1228
[[node mutableChildren] addObject:lastNode];
@@ -1225,14 +1237,27 @@ - (BOOL)runAdoptionAgencyAlgorithmForTagName:(NSString *)tagName
1225
1237
[formattingClone.mutableChildren addObjectsFromArray:furthestBlock.children.array];
1226
1238
1227
1239
[furthestBlock.mutableChildren addObject:formattingClone];
1228
-
1229
- // TODO: Explain why this is necessary.
1230
- if ([_activeFormattingElements indexOfObject:formattingElement] < bookmark) {
1231
- bookmark--;
1232
- }
1233
-
1240
+
1234
1241
[self removeElementFromListOfActiveFormattingElements:formattingElement];
1235
- [_activeFormattingElements insertObject:formattingClone atIndex:bookmark];
1242
+ NSUInteger proposedBookmark = NSNotFound;
1243
+ if (_activeFormattingElements.count == 0) {
1244
+ proposedBookmark = 0;
1245
+ }
1246
+ if (proposedBookmark == NSNotFound) {
1247
+ NSUInteger beforeIndex = beforeBookmark ? [_activeFormattingElements indexOfObject:beforeBookmark] : NSNotFound;
1248
+ if (beforeIndex != NSNotFound) {
1249
+ proposedBookmark = beforeIndex + 1;
1250
+ }
1251
+ }
1252
+ if (proposedBookmark == NSNotFound) {
1253
+ NSUInteger afterIndex = afterBookmark ? [_activeFormattingElements indexOfObject:afterBookmark] : NSNotFound;
1254
+ if (afterIndex != NSNotFound) {
1255
+ proposedBookmark = afterIndex;
1256
+ }
1257
+ }
1258
+ NSAssert(proposedBookmark != NSNotFound, @"Adoption agency algorithm bookmark not found in the list of active formatting elements");
1259
+ [_activeFormattingElements insertObject:formattingClone
1260
+ atIndex:proposedBookmark];
1236
1261
1237
1262
[_stackOfOpenElements removeObject:formattingElement];
1238
1263
[_stackOfOpenElements insertObject:formattingClone
@@ -2614,6 +2639,7 @@ - (void)processToken:(id)token usingRulesForInsertionMode:(HTMLInsertionMode)ins
2614
2639
return [self inBodyInsertionModeHandleStartTagToken:token];
2615
2640
} else {
2616
2641
NSAssert(NO, @"invalid %@ in in body insertion mode", [token class]);
2642
+ break;
2617
2643
}
2618
2644
2619
2645
case HTMLTextInsertionMode:
@@ -2625,6 +2651,7 @@ - (void)processToken:(id)token usingRulesForInsertionMode:(HTMLInsertionMode)ins
2625
2651
return [self textInsertionModeHandleEOFToken:token];
2626
2652
} else {
2627
2653
NSAssert(NO, @"invalid %@ in text insertion mode", [token class]);
2654
+ break;
2628
2655
}
2629
2656
2630
2657
case HTMLInTableInsertionMode:
@@ -2824,6 +2851,7 @@ - (void)processToken:(id)token usingRulesForInsertionMode:(HTMLInsertionMode)ins
2824
2851
return [self foreignContentInsertionModeHandleStartTagToken:token];
2825
2852
} else {
2826
2853
NSAssert(NO, @"invalid %@ in foreign content insertion mode", [token class]);
2854
+ break;
2827
2855
}
2828
2856
2829
2857
default:
0 commit comments