1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #undef IMPL
  27 
  28 #include "config.h"
  29 
  30 #include "DOMException.h"
  31 #include <WebCore/DocumentFragment.h>
  32 #include <WebCore/Node.h>
  33 #include <WebCore/Range.h>
  34 #include <WebCore/JSMainThreadExecState.h>
  35 
  36 #include <wtf/RefPtr.h>
  37 #include <wtf/GetPtr.h>
  38 
  39 #include "JavaDOMUtils.h"
  40 #include <wtf/java/JavaEnv.h>
  41 
  42 using namespace WebCore;
  43 
  44 extern "C" {
  45 
  46 #define IMPL (static_cast<Range*>(jlong_to_ptr(peer)))
  47 
  48 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_dispose(JNIEnv*, jclass, jlong peer)
  49 {
  50     IMPL->deref();
  51 }
  52 
  53 
  54 // Attributes
  55 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_getStartContainerImpl(JNIEnv* env, jclass, jlong peer)
  56 {
  57     WebCore::JSMainThreadNullState state;
  58     return JavaReturn<Node>(env, WTF::getPtr(IMPL->startContainer()));
  59 }
  60 
  61 JNIEXPORT jint JNICALL Java_com_sun_webkit_dom_RangeImpl_getStartOffsetImpl(JNIEnv*, jclass, jlong peer)
  62 {
  63     WebCore::JSMainThreadNullState state;
  64     return IMPL->startOffset();
  65 }
  66 
  67 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_getEndContainerImpl(JNIEnv* env, jclass, jlong peer)
  68 {
  69     WebCore::JSMainThreadNullState state;
  70     return JavaReturn<Node>(env, WTF::getPtr(IMPL->endContainer()));
  71 }
  72 
  73 JNIEXPORT jint JNICALL Java_com_sun_webkit_dom_RangeImpl_getEndOffsetImpl(JNIEnv*, jclass, jlong peer)
  74 {
  75     WebCore::JSMainThreadNullState state;
  76     return IMPL->endOffset();
  77 }
  78 
  79 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_RangeImpl_getCollapsedImpl(JNIEnv*, jclass, jlong peer)
  80 {
  81     WebCore::JSMainThreadNullState state;
  82     return IMPL->collapsed();
  83 }
  84 
  85 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_getCommonAncestorContainerImpl(JNIEnv* env, jclass, jlong peer)
  86 {
  87     WebCore::JSMainThreadNullState state;
  88     return JavaReturn<Node>(env, WTF::getPtr(IMPL->commonAncestorContainer()));
  89 }
  90 
  91 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_RangeImpl_getTextImpl(JNIEnv* env, jclass, jlong peer)
  92 {
  93     WebCore::JSMainThreadNullState state;
  94     return JavaReturn<String>(env, IMPL->text());
  95 }
  96 
  97 
  98 // Functions
  99 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setStartImpl(JNIEnv* env, jclass, jlong peer
 100     , jlong refNode
 101     , jint offset)
 102 {
 103     WebCore::JSMainThreadNullState state;
 104     if (!refNode) {
 105         raiseTypeErrorException(env);
 106         return;
 107     }
 108     raiseOnDOMError(env, IMPL->setStart(*static_cast<Node*>(jlong_to_ptr(refNode)), offset));
 109 }
 110 
 111 
 112 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setEndImpl(JNIEnv* env, jclass, jlong peer
 113     , jlong refNode
 114     , jint offset)
 115 {
 116     WebCore::JSMainThreadNullState state;
 117     if (!refNode) {
 118         raiseTypeErrorException(env);
 119         return;
 120     }
 121     raiseOnDOMError(env, IMPL->setEnd(*static_cast<Node*>(jlong_to_ptr(refNode))
 122             , offset));
 123 }
 124 
 125 
 126 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setStartBeforeImpl(JNIEnv* env, jclass, jlong peer
 127     , jlong refNode)
 128 {
 129     WebCore::JSMainThreadNullState state;
 130     if (!refNode) {
 131         raiseTypeErrorException(env);
 132         return;
 133     }
 134     raiseOnDOMError(env, IMPL->setStartBefore(*static_cast<Node*>(jlong_to_ptr(refNode))));
 135 }
 136 
 137 
 138 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setStartAfterImpl(JNIEnv* env, jclass, jlong peer
 139     , jlong refNode)
 140 {
 141     WebCore::JSMainThreadNullState state;
 142     if (!refNode) {
 143         raiseTypeErrorException(env);
 144         return;
 145     }
 146     raiseOnDOMError(env, IMPL->setStartAfter(*static_cast<Node*>(jlong_to_ptr(refNode))));
 147 }
 148 
 149 
 150 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setEndBeforeImpl(JNIEnv* env, jclass, jlong peer
 151     , jlong refNode)
 152 {
 153     WebCore::JSMainThreadNullState state;
 154     if (!refNode) {
 155         raiseTypeErrorException(env);
 156         return;
 157     }
 158     raiseOnDOMError(env, IMPL->setEndBefore(*static_cast<Node*>(jlong_to_ptr(refNode))));
 159 }
 160 
 161 
 162 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_setEndAfterImpl(JNIEnv* env, jclass, jlong peer
 163     , jlong refNode)
 164 {
 165     WebCore::JSMainThreadNullState state;
 166     if (!refNode) {
 167         raiseTypeErrorException(env);
 168         return;
 169     }
 170     raiseOnDOMError(env, IMPL->setEndAfter(*static_cast<Node*>(jlong_to_ptr(refNode))));
 171 }
 172 
 173 
 174 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_collapseImpl(JNIEnv*, jclass, jlong peer
 175     , jboolean toStart)
 176 {
 177     WebCore::JSMainThreadNullState state;
 178     IMPL->collapse(toStart);
 179 }
 180 
 181 
 182 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_selectNodeImpl(JNIEnv* env, jclass, jlong peer
 183     , jlong refNode)
 184 {
 185     WebCore::JSMainThreadNullState state;
 186     if (!refNode) {
 187         raiseTypeErrorException(env);
 188         return;
 189     }
 190     raiseOnDOMError(env, IMPL->selectNode(*static_cast<Node*>(jlong_to_ptr(refNode))));
 191 }
 192 
 193 
 194 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_selectNodeContentsImpl(JNIEnv* env, jclass, jlong peer
 195     , jlong refNode)
 196 {
 197     WebCore::JSMainThreadNullState state;
 198     if (!refNode) {
 199         raiseTypeErrorException(env);
 200         return;
 201     }
 202     raiseOnDOMError(env, IMPL->selectNodeContents(*static_cast<Node*>(jlong_to_ptr(refNode))));
 203 }
 204 
 205 
 206 JNIEXPORT jshort JNICALL Java_com_sun_webkit_dom_RangeImpl_compareBoundaryPointsImpl(JNIEnv* env, jclass, jlong peer
 207     , jshort how
 208     , jlong sourceRange)
 209 {
 210     WebCore::JSMainThreadNullState state;
 211     if (!sourceRange) {
 212         raiseTypeErrorException(env);
 213         return 0;
 214     }
 215     return raiseOnDOMError(env, IMPL->compareBoundaryPoints(static_cast<Range::CompareHow>(how), *static_cast<Range*>(jlong_to_ptr(sourceRange))));
 216 }
 217 
 218 
 219 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_deleteContentsImpl(JNIEnv* env, jclass, jlong peer)
 220 {
 221     WebCore::JSMainThreadNullState state;
 222     raiseOnDOMError(env, IMPL->deleteContents());
 223 }
 224 
 225 
 226 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_extractContentsImpl(JNIEnv* env, jclass, jlong peer)
 227 {
 228     WebCore::JSMainThreadNullState state;
 229     return JavaReturn<DocumentFragment>(env, WTF::getPtr(raiseOnDOMError(env, IMPL->extractContents())));
 230 }
 231 
 232 
 233 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_cloneContentsImpl(JNIEnv* env, jclass, jlong peer)
 234 {
 235     WebCore::JSMainThreadNullState state;
 236     return JavaReturn<DocumentFragment>(env, WTF::getPtr(raiseOnDOMError(env, IMPL->cloneContents())));
 237 }
 238 
 239 
 240 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_insertNodeImpl(JNIEnv* env, jclass, jlong peer
 241     , jlong newNode)
 242 {
 243     WebCore::JSMainThreadNullState state;
 244     if (!newNode) {
 245         raiseTypeErrorException(env);
 246         return;
 247     }
 248     raiseOnDOMError(env, IMPL->insertNode(*static_cast<Node*>(jlong_to_ptr(newNode))));
 249 }
 250 
 251 
 252 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_surroundContentsImpl(JNIEnv* env, jclass, jlong peer
 253     , jlong newParent)
 254 {
 255     WebCore::JSMainThreadNullState state;
 256     if (!newParent) {
 257         raiseTypeErrorException(env);
 258         return;
 259     }
 260     raiseOnDOMError(env, IMPL->surroundContents(*static_cast<Node*>(jlong_to_ptr(newParent))));
 261 }
 262 
 263 
 264 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_cloneRangeImpl(JNIEnv* env, jclass, jlong peer)
 265 {
 266     WebCore::JSMainThreadNullState state;
 267     return JavaReturn<Range>(env, WTF::getPtr(IMPL->cloneRange()));
 268 }
 269 
 270 
 271 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_RangeImpl_toStringImpl(JNIEnv* env, jclass, jlong peer)
 272 {
 273     WebCore::JSMainThreadNullState state;
 274     return JavaReturn<String>(env, IMPL->toString());
 275 }
 276 
 277 
 278 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_detachImpl(JNIEnv*, jclass, jlong peer)
 279 {
 280     WebCore::JSMainThreadNullState state;
 281     IMPL->detach();
 282 }
 283 
 284 
 285 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_RangeImpl_createContextualFragmentImpl(JNIEnv* env, jclass, jlong peer
 286     , jstring html)
 287 {
 288     WebCore::JSMainThreadNullState state;
 289     return JavaReturn<DocumentFragment>(env, WTF::getPtr(raiseOnDOMError(env, IMPL->createContextualFragment(String(env, html)))));
 290 }
 291 
 292 
 293 JNIEXPORT jshort JNICALL Java_com_sun_webkit_dom_RangeImpl_compareNodeImpl(JNIEnv* env, jclass, jlong peer
 294     , jlong refNode)
 295 {
 296     WebCore::JSMainThreadNullState state;
 297     if (!refNode) {
 298         raiseTypeErrorException(env);
 299         return 0;
 300     }
 301     return raiseOnDOMError(env, IMPL->compareNode(*static_cast<Node*>(jlong_to_ptr(refNode))));
 302 }
 303 
 304 
 305 JNIEXPORT jshort JNICALL Java_com_sun_webkit_dom_RangeImpl_comparePointImpl(JNIEnv* env, jclass, jlong peer
 306     , jlong refNode
 307     , jint offset)
 308 {
 309     WebCore::JSMainThreadNullState state;
 310     if (!refNode) {
 311         raiseTypeErrorException(env);
 312         return 0;
 313     }
 314     return raiseOnDOMError(env, IMPL->comparePoint(*static_cast<Node*>(jlong_to_ptr(refNode))
 315             , offset));
 316 }
 317 
 318 
 319 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_RangeImpl_intersectsNodeImpl(JNIEnv* env, jclass, jlong peer
 320     , jlong refNode)
 321 {
 322     WebCore::JSMainThreadNullState state;
 323     if (!refNode) {
 324         raiseTypeErrorException(env);
 325         return JNI_FALSE;
 326     }
 327     return raiseOnDOMError(env, IMPL->intersectsNode(*static_cast<Node*>(jlong_to_ptr(refNode))));
 328 }
 329 
 330 
 331 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_RangeImpl_isPointInRangeImpl(JNIEnv* env, jclass, jlong peer
 332     , jlong refNode
 333     , jint offset)
 334 {
 335     WebCore::JSMainThreadNullState state;
 336     if (!refNode) {
 337         raiseTypeErrorException(env);
 338         return JNI_FALSE;
 339     }
 340     return raiseOnDOMError(env, IMPL->isPointInRange(*static_cast<Node*>(jlong_to_ptr(refNode))
 341             , offset));
 342 }
 343 
 344 
 345 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_RangeImpl_expandImpl(JNIEnv* env, jclass, jlong peer
 346     , jstring unit)
 347 {
 348     WebCore::JSMainThreadNullState state;
 349     raiseOnDOMError(env, IMPL->expand(String(env, unit)));
 350 }
 351 
 352 
 353 }