From 153d48c19c8bc2219c6ad4db4a09ea2c5051693a Mon Sep 17 00:00:00 2001 From: iska Date: Tue, 6 Aug 2019 21:26:21 +0200 Subject: [PATCH] Set iterator and range detach operations to noop Leave the housekeeping to the NSHashTable itself, which should purge null-references automatically (not necessarily right away) Also update tests accordingly: Use `allObjects.count` instead of simply `count` on the NSHashTable, since `allObjects.count` returns non-null references. This should fix #36 --- Sources/HTMLDocument.m | 4 ++-- Tests/HTMLKitTests/HTMLNodeIteratorTests.m | 12 +++++++----- Tests/HTMLKitTests/HTMLRangeTests.m | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/HTMLDocument.m b/Sources/HTMLDocument.m index 33e84d4..e41f539 100644 --- a/Sources/HTMLDocument.m +++ b/Sources/HTMLDocument.m @@ -141,7 +141,7 @@ - (void)detachNodeIterator:(HTMLNodeIterator *)iterator { - [_nodeIterators removeObject:iterator]; + // NOOP } #pragma mark - Ranges @@ -153,7 +153,7 @@ - (void)detachRange:(HTMLRange *)range { - [_ranges removeObject:range]; + // NOOP } - (void)didRemoveCharacterDataInNode:(HTMLCharacterData *)node atOffset:(NSUInteger)offset withLength:(NSUInteger)length diff --git a/Tests/HTMLKitTests/HTMLNodeIteratorTests.m b/Tests/HTMLKitTests/HTMLNodeIteratorTests.m index fe51ef4..91bd4ac 100644 --- a/Tests/HTMLKitTests/HTMLNodeIteratorTests.m +++ b/Tests/HTMLKitTests/HTMLNodeIteratorTests.m @@ -580,19 +580,20 @@ static HTMLNode * (^ LastDescendant)(HTMLNode *) = ^ HTMLNode * (HTMLNode *node) XCTAssertNotNil(body); // iterator should be deallocated and detached at this point - XCTAssertEqual(0, nodeIterators.count); + XCTAssertEqual(0, nodeIterators.allObjects.count); // iterator should be autoreleased, deallocated and detached after autoreleasepool @autoreleasepool { HTMLNodeIterator *iterator = [[HTMLNodeIterator alloc] initWithNode:body]; [iterator nextNode]; - XCTAssertEqual(1, nodeIterators.count); + XCTAssertEqual(1, nodeIterators.allObjects.count); } - XCTAssertEqual(0, nodeIterators.count); + XCTAssertEqual(0, nodeIterators.allObjects.count); } -- (void)testBugFix_Issue_22 { +- (void)testBugFix_Issue_22 +{ // The issue is applicable only for devices. On simulator the test is passed. HTMLDocument *document = [HTMLDocument documentWithString:@"
"]; @@ -608,7 +609,8 @@ static HTMLNode * (^ LastDescendant)(HTMLNode *) = ^ HTMLNode * (HTMLNode *node) XCTAssertTrue([element.elementId isEqualToString:divId]); } -- (void)testBugFix_Issue_28 { +- (void)testBugFix_Issue_28 +{ HTMLDocument *document = self.document; HTMLNodeIterator *iterator = document.body.nodeIterator; diff --git a/Tests/HTMLKitTests/HTMLRangeTests.m b/Tests/HTMLKitTests/HTMLRangeTests.m index c6b1c0e..3a62ae5 100644 --- a/Tests/HTMLKitTests/HTMLRangeTests.m +++ b/Tests/HTMLKitTests/HTMLRangeTests.m @@ -2112,10 +2112,10 @@ @autoreleasepool { HTMLRange *range = [[HTMLRange alloc] initWithDocument:document]; [range cloneContents]; - XCTAssertEqual(1, ranges.count); + XCTAssertEqual(1, ranges.allObjects.count); } - XCTAssertEqual(0, ranges.count); + XCTAssertEqual(0, ranges.allObjects.count); } @end