@@ -631,88 +631,116 @@ protected String getPropertyPrefix() {
631631 */
632632 private Object [] lastRange ;
633633
634+ class Node {
635+ Node [] childNodes ;
636+ String tagName ;
637+ Node parentNode ;
638+ String innerText ;
639+ int length ;
640+ String textContent ;
641+ }
642+
634643 /**
635644 * Find the HTML node and offset for this Java caret position.
636645 *
637646 * @param node domNode or one of its descendants
638647 * @param off document offset to start of this node
639648 * @param pt target caret position
640- * @return range information or length: [textNode,charOffset] or [nontextNode,charNodesOffset] or [null, nlen]
649+ * @return range information or length: [textNode,charOffset] or
650+ * [nontextNode,charNodesOffset] or [null, nlen]
641651 */
642652 @ SuppressWarnings ("unused" )
643653 @ Override
644- protected Object [] getJSNodePt (DOMNode node , int pt , boolean isRoot ) {
654+ protected Object [] getJSNodePt (DOMNode dnode , int pt , Object [] lastRange , int level ) {
645655 // JavaScript below will call this method iteratively with off >= 0.
656+ Node node = /** @j2sNative dnode || */
657+ null ;
658+ boolean isRoot = (lastRange == null );
646659 pt = Math .max (0 , pt );
647- if (isRoot ) {
648- // System.out.println("JSEPUI getJSNodePt " + editor.getText().replace('\n', '.').replace('\t', '^'));
649- lastRange = null ;
650- }
651-
652- boolean isTAB = isJSTAB ( node );
653-
654- //System.out.println("getting JSNodePt for " + isTAB + " " + pt + " " + (/** @j2sNative node.data||node.outerHTML ||*/""));
655-
660+ // if (isRoot) {
661+ // System.out.println("JSEPUI getJSNodePt " + editor.getText().replace('\n', '.').replace('\t', '^'));
662+ // }
663+
664+ boolean isTAB = isJSTAB ( dnode );
665+
666+ // System.out.println("getting JSNodePt for " + isTAB + " " + pt + " " + (/**
667+ // @j2sNative node.data||node.outerHTML ||*/""));
668+
656669 // Must consider several cases for BR and DIV:
657670 // <br>
658671 // <div><br><div> where br counts as 1 character --> [div, 0] or [null, 1]
659- // <div>.....<br><div> where childNodes[i] is br, counts as 0 charactors --> [div, i] or [null, 0]
672+ // <div>.....<br><div> where childNodes[i] is br, counts as 0 charactors -->
673+ // [div, i] or [null, 0]
660674 // as well as "raw" text in the root domNode:
661- // text....<br>...text...<br>.... where br counts as 1 character --> [node.parentNode, i] or [null, 1]
675+ // text....<br>...text...<br>.... where br counts as 1 character -->
676+ // [node.parentNode, i] or [null, 1]
662677 //
663- // also note that range can point to a character position only if the node is #text
664- // otherwise, it must point to a childNodes index in the parent node. So <br> must
665- // be indicated this second way.
678+ // also note that range can point to a character position only if the node is
679+ // #text
680+ // otherwise, it must point to a childNode index in the parent node. So <br>
681+ // must be indicated this second way.
666682 //
667683 // TAB will be indicated as a JSTAB string (see above).
668-
669- /**
670- * @j2sNative
671- *
672-
673-
674- this.lastRange = [node, 0];
675- if (isTAB) {
676- return (pt == 0 ? this.lastRange : [null, pt - 1]);
677- }
678- var nodes = node.childNodes;
679- var tag = node.tagName;
680- var n = nodes.length;
684+ //
685+ Object [] r = /** @j2sNative 1?[]: */
686+ null ;
687+ try {
688+ lastRange = setNode (null , node , 0 );
689+ if (isTAB ) {
690+ return (pt == 0 ? r = lastRange : setNode (r , null , pt - 1 ));
691+ }
692+ Node [] nodes = node .childNodes ;
693+ String tag = node .tagName ;
694+ int n = nodes .length ;
681695 if (tag == "BR" || n == 1 && nodes [0 ].tagName == "BR" ) {
682- return (pt == 0 ? [ node, 0] : [ null, pt - 1] );
683- }
684- var nlen = 0;
685- var i1 = (tag == "DIV" || tag == "P" ? 1 : 0);
686- for (var i = 0; i < n; i++) {
696+ return (pt == 0 ? setNode ( r , node , 0 ) : setNode ( r , null , pt - 1 ) );
697+ }
698+ int nlen = 0 ;
699+ int i1 = (tag == "DIV" || tag == "P" ? 1 : 0 );
700+ for (int i = 0 ; i < n ; i ++) {
687701 node = nodes [i ];
688- if (node.innerText) {
689- var ret=this.getJSNodePt$swingjs_api_js_DOMNode$I$Z(node, pt, false);
690- if (ret[0] != null)
691- return ret;
692- pt = ret[1];
702+ if (node .innerText != null ) {
703+ r = getJSNodePt ((DOMNode ) (Object ) node , pt , lastRange , ++level );
704+ if (r [0 ] != null ) {
705+ return r ;
706+ }
707+ pt = /** @j2sNative 1?r[1] : */
708+ 0 ;
693709 } else if (node .tagName == "BR" ) {
694710 if (pt == 0 )
695- return [ node.parentNode, i] ;
711+ return setNode ( r , node .parentNode , i ) ;
696712 pt -= (isRoot ? 1 : 0 );
697713 } else {
698714 nlen = node .length ;
699715 if (nlen >= pt )
700- return this.lastRange = [ node, pt] ;
701- this. lastRange = [ node, nlen] ;
716+ return r = setNode ( lastRange , node , pt ) ;
717+ lastRange = setNode ( lastRange , node , nlen ) ;
702718 pt -= nlen ;
703719 }
704720 }
705721 if (!isRoot )
706- return [ null, Math.max(0, pt - i1)] ;
707- var r = this. lastRange;
708- this.lastRange = null;
709- return r;
710- */
711- {
712- return null ;
722+ return setNode ( r , null , Math .max (0 , pt - i1 )) ;
723+ return r = lastRange ;
724+ } finally {
725+ // System.out.println("level " + level
726+ // + " pt = " + r[1]
727+ // + " text=" + (r[0] == null ? "<null>" : (((Node) r[0]).parentNode == null ? "NULL" : ((Node) r[0]).parentNode.tagName) + " "
728+ // + ((Node) r[0]).textContent.replace('\n','.').replace('\t','^').replace(' ','_'))) ;
713729 }
714730 }
715731
732+ private Object [] setNode (Object [] r , Node node , int i ) {
733+ /**
734+ @j2sNative if (r) {
735+ r[0] = node;
736+ r[1] = i;
737+ return r;
738+ }
739+ r = [node, i];
740+ */
741+ return r ;
742+ }
743+
716744 @ Override
717745 public String getJSTextValue () {
718746 String s = getInnerTextSafely (domNode , false , null ).toString ().replaceAll ("\u00A0 " ," " ); //
@@ -777,7 +805,6 @@ protected void jsSelect(Object[] r1, Object[] r2, boolean andScroll) {
777805
778806 andScroll |= (jc .秘keyAction != null );
779807
780-
781808
782809// System.out.println("jsSelect " + r1 + r2 + " " + andScroll);
783810 // range index may be NaN
0 commit comments