Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

range.IntersectWith() - exception on 'li' #2990

Open
1 task done
hdijkema opened this issue Nov 13, 2024 · 0 comments
Open
1 task done

range.IntersectWith() - exception on 'li' #2990

hdijkema opened this issue Nov 13, 2024 · 0 comments
Assignees

Comments

@hdijkema
Copy link

hdijkema commented Nov 13, 2024

This issue is unique.

  • I have used the search tool and did not find an issue describing my bug.

Operating System of DocumentServer

Docker

Version information

8.2.1

Expected Behavior

A new range on intersection or 'null' when nothing intersects.

Actual Behavior

An exception:

TypeError: a is undefined
    Li https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:10461
    nsg https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14003
    partOf https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function line 3 > eval:15
    <anonymous> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function line 3 > eval:81
    <anonymous> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function line 3 > eval:97
    anonymous https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function:3
    EDc https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js:1230
    Mhf https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14984
    Qzh https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14983
    h https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14956
    EventListener.handleEvent* https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14985
    <anonymous> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14993
    <anonymous> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:15149
sdk-all-min.js line 1229 > Function line 3 > eval:87:17
    <anoniem> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function line 3 > eval:87
    <anoniem> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function line 3 > eval:97
    anonymous https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js line 1229 > Function:3
    EDc https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all-min.js:1230
    Mhf https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14984
    Qzh https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14983
    h https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14956
    (Async: EventListener.handleEvent)
    <anoniem> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14985
    <anoniem> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:14993
    <anoniem> https://example.com/8.2.1-0ace3c53c3b52a07b1bf4ca239d5162e/sdkjs/word/sdk-all.js:15149

Reproduction Steps

This is called from within a plugin.

Create a function and run it with callCommand. See code below. This issue is related to issue #2920.

let runCommand = function(func, callback, args = '') {
  win.Asc.scope.args = JSON.stringify(args);
  //console.log('running command width args=', win.Asc.scope.args);
  win.Asc.plugin.callCommand(func, false, true, callback);
};
let indexOfBookmark = function(callback, at_cursor = false) {
   runCommand(
      function() {
         let parId = function(par) {  return par.Ga.Wa;  };
         let parText = function(par) {
            let raw_txt = par.GetText();
            let txt = raw_txt.replace(/[\n\r\t]/g, '').replace(/[\\]u[0-9a-f][0-9a-f][0-9a-f][0-9][a-f]/g, '').replace(/[^\x20-\x7E]/g, '').replace(/\s/g, '');
            return txt.trim();
            };
         let partOf = function(bookmark, sel, range) {
            //console.log(bookmark, range.GetText(), sel.GetText());
            let nrange = range.IntersectWith(sel);
            // If we have an intersection, the selection intersects with the bookmark and 
            // we should know that this bookmark is selected and no new bookmark must be created.
            // preferably it should also be possible to see if the current caret position is in a bookmark. 
            // however. IntersectWith is not reliable (version 8.1.3) so we need to do some extra work
            // to see if we are indeed an intersection. 
            //console.log("nrange intersected");
            if (nrange !== null) {
               //console.log('nrange !== null');
               let range_txt = parText(range);
               let sel_txt = parText(sel);
               let sel_pars = sel.GetAllParagraphs();
               let range_pars = range.GetAllParagraphs();
               let sel_par_cnt = sel_pars.map(function(par) { return { txt: parText(par), id: [ parId(par) ] }; });
               let range_par_cnt = range_pars.map(function(par) { return { txt: parText(par), id: [ parId(par) ] }; });
               let sel_par_tot_txt = '';
               let sel_par_ids = [];
               sel_par_cnt.forEach(function(obj) { sel_par_tot_txt += obj.txt; sel_par_ids.push(obj.id[0]); });
               sel_par_cnt.push({ txt: sel_par_tot_txt, id: sel_par_ids });
               let range_par_tot_txt = '';
               let range_par_ids = [];
               range_par_cnt.forEach(function(obj) { range_par_tot_txt += obj.txt; range_par_ids.push(obj.id[0]); });
               range_par_cnt.push({ txt: range_par_tot_txt, id: range_par_ids });
               //console.log('sel_par_cnt=', sel_par_cnt);
               //console.log('range_par_cnt=', range_par_cnt);
               let sel_par_filtered = sel_par_cnt.filter(function(obj) { return sel_txt.indexOf(obj.txt) >= 0 || obj.txt.indexOf(sel_txt) >= 0; });
               let range_par_filtered = range_par_cnt.filter(function(obj) { return range_txt.indexOf(obj.txt) >= 0 || obj.txt.indexOf(range_txt) >= 0; });
               //console.log('sel_par_filtered=', sel_par_filtered);
               //console.log('range_par_filtered=', range_par_filtered);
               let sel_set = new Set();
               sel_par_filtered.forEach(function(obj) { obj.id.forEach(function(id) { sel_set.add(id); }); });
               let range_set = new Set();
               range_par_filtered.forEach(function(obj) { obj.id.forEach(function(id) { range_set.add(id); }); });
               //console.log('sel_set=', sel_set);
               //console.log('range_set=', range_set);
               let intersection = sel_set.intersection(range_set);
               return intersection.size > 0;
            } else {
               // check if the paragraphs still intersect.
               //console.log('nrange === null');
               let sel_pars = sel.GetAllParagraphs();
               let range_pars = range.GetAllParagraphs();
               let sel_par_cnt = sel_pars.map(function(par) { return parId(par); });
               let range_par_cnt = range_pars.map(function(par) { return  parId(par); });
               let sel_set = new Set(sel_par_cnt);
               let range_set = new Set(range_par_cnt);
               let intersection = sel_set.intersection(range_set);
               return intersection.size > 0
            }
         };

         try {
            let args = JSON.parse(Asc.scope.args);
            let doc = Api.GetDocument();

            //let at_cursor = args.at_cursor;
            let doc = Api.GetDocument();
            //if (at_cursor) {
            //   There's no way to get the range of the cursor position (caret), so we try to select
            //   the current word. 
            //	   doc.SelectCurrentWord();
            //}

            let sel = doc.GetRangeBySelect();
            let bookmark_names = doc.GetAllBookmarksNames();
            let found = 0;
            let idx = -1;
            let bookmark_name = '';
            let i;
            //console.log(bookmark_names.length);
            for(i = 0; i < bookmark_names.length; i++) {
               let bookmark = bookmark_names[i];
               let range = doc.GetBookmarkRange(bookmark);
               try {
                  if (partOf(bookmark, sel, range)) {
                     found += 1;
                     idx = i;
                     bookmark_name = bookmark;
                  }
               } catch (e) { 
                  console.error(e);
	       }
           }

          console.log('found=', found, 'idx=', idx, 'bookmark=', bookmark_name);
          let obj = { found_count: found, idx: idx, bookmark: bookmark_name };
             return JSON.stringify(obj);
          } catch (e) {
             console.error('indexOfBookmark error:', e);
             return null;
          }
      },
      function(result) {
          if (result !== null) {
              let obj = JSON.parse(result);
              callback(obj);
          }
      },
      { at_cursor: at_cursor }
   );
};

Additional information

image

So it breaks on the bullet list.

(Also the parId() function does not work anymore with 8.2.1, it will give an exception: par.Ga is undefined).

Also, the search function for if a bookmark is selected would be much simpler if the intersectwith function would work correctly:

let indexOfBookmark = function(callback, at_cursor = false) {
	runCommand(
		function() {
			let partOf = function(bookmark, sel, range) {
				let nrange = range.IntersectWith(sel);
				if (nrange !== null) {
					return true;
				} else {
					return false;
				}
			};

			try {
				let args = JSON.parse(Asc.scope.args);
				//let at_cursor = args.at_cursor;

				let doc = Api.GetDocument();
				//if (at_cursor) {
				//	doc.SelectCurrentWord();
				//}

				let sel = doc.GetRangeBySelect();
				let bookmark_names = doc.GetAllBookmarksNames();
				let found = 0;
				let idx = -1;
				let bookmark_name = '';
				let i;
				for(i = 0; i < bookmark_names.length; i++) {
					let bookmark = bookmark_names[i];
					let range = doc.GetBookmarkRange(bookmark);
					try {
						if (partOf(bookmark, sel, range)) {
							found += 1;
							idx = i;
							bookmark_name = bookmark;
						}
					} catch (e) { 
						console.error(e);
					}
				}
				//console.log('found=', found, 'idx=', idx, 'bookmark=', bookmark_name);
				let obj = { found_count: found, idx: idx, bookmark: bookmark_name };
				return JSON.stringify(obj);
			} catch (e) {
				console.error('indexOfBookmark error:', e);
				return null;
			}
		},
		function(result) {
			if (result !== null) {
				let obj = JSON.parse(result);
				callback(obj);
			}
		},
		{ at_cursor: at_cursor }
	);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants