diff --git a/Sources/HTMLKitDOMExceptions.m b/Sources/HTMLKitDOMExceptions.m index 1a596a3..5ea8311 100644 --- a/Sources/HTMLKitDOMExceptions.m +++ b/Sources/HTMLKitDOMExceptions.m @@ -16,3 +16,4 @@ NSString * const HTMLKitSyntaxError = @"SyntaxError"; NSString * const HTMLKitInvalidCharacterError = @"InvalidCharacterError"; NSString * const HTMLKitInvalidNodeTypeError = @"InvalidNodeTypeError"; NSString * const HTMLKitIndexSizeError = @"IndexSizeError"; +NSString * const HTMLKitWrongDocumentError = @"WrongDocumentError"; diff --git a/Sources/HTMLRange.m b/Sources/HTMLRange.m index 847f9ef..ae90172 100644 --- a/Sources/HTMLRange.m +++ b/Sources/HTMLRange.m @@ -65,6 +65,15 @@ NS_INLINE void CheckValidBoundaryOffset(HTMLNode *node, NSUInteger offset, NSStr } } +NS_INLINE void CheckValidDocument(HTMLRange *lhs, HTMLRange *rhs, NSString *cmd) +{ + if (lhs.rootNode != rhs.rootNode) { + [NSException raise:HTMLKitWrongDocumentError + format:@"%@: Wrong Document Error, ranges %@ and %@ have different roots.", + cmd, lhs, rhs]; + } +} + NS_INLINE NSComparisonResult CompareBoundaries(HTMLNode *startNode, NSUInteger startOffset, HTMLNode *endNode, NSUInteger endOffset) { if (startNode == endNode) { @@ -173,6 +182,23 @@ NS_INLINE NSComparisonResult CompareBoundaries(HTMLNode *startNode, NSUInteger s [self setStartNode:_endContainer startOffset:_endOffset]; } +- (void)selectNode:(HTMLNode *)node +{ + HTMLNode *parent = node.parentNode; + CheckValidBoundaryNode(parent, NSStringFromSelector(_cmd)); + + [self setStartNode:parent startOffset:node.index]; + [self setEndNode:parent endOffset:node.index + 1]; +} + +- (void)selectNodeContents:(HTMLNode *)node +{ + CheckValidBoundaryNode(node, NSStringFromSelector(_cmd)); + + [self setStartNode:node startOffset:0]; + [self setEndNode:node endOffset:node.length]; +} + - (NSComparisonResult)compareBoundaryPoints:(HTMLRangeComparisonMethod)method sourceRange:(HTMLRange *)sourceRange { CheckValidDocument(self, sourceRange, NSStringFromSelector(_cmd)); diff --git a/Sources/include/HTMLKitDOMExceptions.h b/Sources/include/HTMLKitDOMExceptions.h index f11518c..5b13fae 100644 --- a/Sources/include/HTMLKitDOMExceptions.h +++ b/Sources/include/HTMLKitDOMExceptions.h @@ -17,3 +17,4 @@ extern NSString * const HTMLKitInvalidCharacterError; extern NSString * const HTMLKitInvalidNodeTypeError; extern NSString * const HTMLKitIndexSizeError; +extern NSString * const HTMLKitWrongDocumentError;