{"size": 1756, "ext": "js", "max_stars_repo_path": "js/location/set.js", "max_stars_repo_name": "OCEANOFANYTHING/writeurl", "max_stars_repo_stars_event_min_datetime": "2018-01-27T18:24:10.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-05T22:13:42.000Z", "max_issues_repo_path": "js/location/set.js", "max_issues_repo_name": "OCEANOFANYTHING/writeurl", "max_issues_repo_issues_event_min_datetime": "2020-02-20T13:28:53.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-06T21:24:13.000Z", "max_forks_repo_path": "js/location/set.js", "max_forks_repo_name": "OCEANOFANYTHING/writeurl", "max_forks_repo_forks_event_min_datetime": "2018-03-17T17:01:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-13T17:17:27.000Z"}
{"max_stars_count": 8, "max_issues_count": 3, "max_forks_count": 4, "avg_line_length": 25.8235294118, "max_line_length": 98, "alphanum_fraction": 0.7038724374}
'use strict';

nbe.location.set = function (editor, location) {
	var find_container_offset, container_offset_start, container_offset_end, range, selection;

	find_container_offset = function (id, offset) {
		var node, new_id, new_offset, container, new_node;

		node = editor.state.nodes[id];
		if (node) {
			if (node.type === 'img') {
				new_id = node.parent.id;
				new_offset = node.parent.children.indexOf(node) + 1;
			} else {
				new_id = id;
				new_offset = offset;
			}

			container = editor.dom[new_id];
			new_node = editor.state.nodes[new_id];

			if (new_node.type === 'text') {
				container = container.firstChild;
				if (new_offset > new_node.val.length) {
					new_offset = new_node.val.length;
				}
			}

			return {container : container, offset : new_offset};
		} else {
			return null;
		}
	};

	editor.mutation.disconnect();

	if (location) {
		container_offset_start = find_container_offset(location.start.container, location.start.offset);
		if (location.collapsed) {
			container_offset_end = container_offset_start;
		} else {
			container_offset_end = find_container_offset(location.end.container, location.end.offset);
		}

		if (container_offset_start && container_offset_end) {
			range = document.createRange();
			range.setStart(container_offset_start.container, container_offset_start.offset);
			range.setEnd(container_offset_end.container, container_offset_end.offset);
			selection = window.getSelection();
			selection.removeAllRanges();
			selection.addRange(range);
		} else {
			location = null;
		}
	}

	editor.location = location;
	if (location) {
		nbe.location.focus(editor);
		nbe.location.scroll(editor);
		editor.inputs.notify();
	} else {
		nbe.location.blur(editor);
	}

	editor.mutation.observe();
};
