| /* | |
| * Copyright (C) 2004-2016 Apple Inc. All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions | |
| * are met: | |
| * 1. Redistributions of source code must retain the above copyright | |
| * notice, this list of conditions and the following disclaimer. | |
| * 2. Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| #import "DOMRangeInternal.h" | |
| #import "DOMDocumentFragmentInternal.h" | |
| #import "DOMInternal.h" | |
| #import "DOMNodeInternal.h" | |
| #import <WebCore/DocumentFragment.h> | |
| #import "ExceptionHandlers.h" | |
| #import <WebCore/BoundaryPointInlines.h> | |
| #import <WebCore/JSExecState.h> | |
| #import <WebCore/Range.h> | |
| #import <WebCore/SimpleRange.h> | |
| #import <WebCore/TextIterator.h> | |
| #import <WebCore/ThreadCheck.h> | |
| #import <WebCore/WebCoreObjCExtras.h> | |
| #import <WebCore/WebScriptObjectPrivate.h> | |
| #import <wtf/GetPtr.h> | |
| #import <wtf/URL.h> | |
| #define IMPL protect(reinterpret_cast<WebCore::Range*>(_internal)) | |
| @implementation DOMRange | |
| - (void)dealloc | |
| { | |
| if (WebCoreObjCScheduleDeallocateOnMainThread([DOMRange class], self)) | |
| return; | |
| if (_internal) | |
| IMPL->deref(); | |
| [super dealloc]; | |
| } | |
| - (DOMNode *)startContainer | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(WTF::getPtr(IMPL->startContainer())); | |
| } | |
| - (int)startOffset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return IMPL->startOffset(); | |
| } | |
| - (DOMNode *)endContainer | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(WTF::getPtr(IMPL->endContainer())); | |
| } | |
| - (int)endOffset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return IMPL->endOffset(); | |
| } | |
| - (BOOL)collapsed | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return IMPL->collapsed(); | |
| } | |
| - (DOMNode *)commonAncestorContainer | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(WTF::getPtr(IMPL->commonAncestorContainer())); | |
| } | |
| - (NSString *)text | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| auto range = makeSimpleRange(*IMPL); | |
| protect(range.start.document())->updateLayout(); | |
| return plainText(range).createNSString().autorelease(); | |
| } | |
| - (void)setStart:(DOMNode *)refNode offset:(int)offset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setStart(*core(refNode), offset)); | |
| } | |
| - (void)setEnd:(DOMNode *)refNode offset:(int)offset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setEnd(*core(refNode), offset)); | |
| } | |
| - (void)setStartBefore:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setStartBefore(protect(*core(refNode)))); | |
| } | |
| - (void)setStartAfter:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setStartAfter(protect(*core(refNode)))); | |
| } | |
| - (void)setEndBefore:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setEndBefore(protect(*core(refNode)))); | |
| } | |
| - (void)setEndAfter:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->setEndAfter(protect(*core(refNode)))); | |
| } | |
| - (void)collapse:(BOOL)toStart | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| IMPL->collapse(toStart); | |
| } | |
| - (void)selectNode:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->selectNode(protect(*core(refNode)))); | |
| } | |
| - (void)selectNodeContents:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->selectNodeContents(protect(*core(refNode)))); | |
| } | |
| - (short)compareBoundaryPoints:(unsigned short)how sourceRange:(DOMRange *)sourceRange | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!sourceRange) | |
| raiseTypeErrorException(); | |
| return raiseOnDOMError(IMPL->compareBoundaryPoints(how, protect(*core(sourceRange)))); | |
| } | |
| - (void)deleteContents | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| raiseOnDOMError(IMPL->deleteContents()); | |
| } | |
| - (DOMDocumentFragment *)extractContents | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(raiseOnDOMError(IMPL->extractContents()).ptr()); | |
| } | |
| - (DOMDocumentFragment *)cloneContents | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(raiseOnDOMError(IMPL->cloneContents()).ptr()); | |
| } | |
| - (void)insertNode:(DOMNode *)newNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!newNode) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->insertNode(*core(newNode))); | |
| } | |
| - (void)surroundContents:(DOMNode *)newParent | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!newParent) | |
| raiseTypeErrorException(); | |
| raiseOnDOMError(IMPL->surroundContents(protect(*core(newParent)))); | |
| } | |
| - (DOMRange *)cloneRange | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(WTF::getPtr(IMPL->cloneRange())); | |
| } | |
| - (NSString *)toString | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return IMPL->toString().createNSString().autorelease(); | |
| } | |
| - (void)detach | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| IMPL->detach(); | |
| } | |
| - (DOMDocumentFragment *)createContextualFragment:(NSString *)html | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| return kit(raiseOnDOMError(IMPL->createContextualFragment(html)).ptr()); | |
| } | |
| - (short)compareNode:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| return raiseOnDOMError(IMPL->compareNode(protect(*core(refNode)))); | |
| } | |
| - (BOOL)intersectsNode:(DOMNode *)refNode | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| return IMPL->intersectsNode(protect(*core(refNode))); | |
| } | |
| - (short)comparePoint:(DOMNode *)refNode offset:(int)offset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| return raiseOnDOMError(IMPL->comparePoint(protect(*core(refNode)), offset)); | |
| } | |
| - (BOOL)isPointInRange:(DOMNode *)refNode offset:(int)offset | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| if (!refNode) | |
| raiseTypeErrorException(); | |
| return raiseOnDOMError(IMPL->isPointInRange(protect(*core(refNode)), offset)); | |
| } | |
| - (void)expand:(NSString *)unit | |
| { | |
| WebCore::JSMainThreadNullState state; | |
| raiseOnDOMError(IMPL->expand(unit)); | |
| } | |
| @end | |
| @implementation DOMRange (DOMRangeDeprecated) | |
| - (void)setStart:(DOMNode *)refNode :(int)offset | |
| { | |
| [self setStart:refNode offset:offset]; | |
| } | |
| - (void)setEnd:(DOMNode *)refNode :(int)offset | |
| { | |
| [self setEnd:refNode offset:offset]; | |
| } | |
| - (short)compareBoundaryPoints:(unsigned short)how :(DOMRange *)sourceRange | |
| { | |
| return [self compareBoundaryPoints:how sourceRange:sourceRange]; | |
| } | |
| @end | |
| WebCore::Range* core(DOMRange *wrapper) | |
| { | |
| return wrapper ? reinterpret_cast<WebCore::Range*>(wrapper->_internal) : 0; | |
| } | |
| SUPPRESS_NODELETE DOMRange *kit(WebCore::Range* value) | |
| { | |
| WebCoreThreadViolationCheckRoundOne(); | |
| if (!value) | |
| return nil; | |
| if (RetainPtr wrapper = getDOMWrapper(value)) | |
| return wrapper.autorelease(); | |
| auto wrapper = adoptNS([[DOMRange alloc] _init]); | |
| wrapper->_internal = reinterpret_cast<DOMObjectInternal*>(value); | |
| value->ref(); | |
| addDOMWrapper(wrapper.get(), value); | |
| return wrapper.autorelease(); | |
| } | |
| DOMRange *kit(const std::optional<WebCore::SimpleRange>& value) | |
| { | |
| return kit(createLiveRange(value).get()); | |
| } | |
| #undef IMPL |