+ if(from instanceof HTMLElement || from instanceof Text) {
+ return documentElement.DocumentElement.fromHTMLElement(from, this);
+ }
+ },
+ getCursor: function() {
+ return new Cursor(this);
+ },
+
+ list: {},
+
+
+
+ highlightElement: function(element) {
+ this.wrapper.find('.highlighted-element').removeClass('highlighted-element');
+ element.dom().addClass('highlighted-element');
+ },
+
+ dimElement: function(element) {
+ element.dom().removeClass('highlighted-element');
+ },
+
+ getCurrentNodeElement: function() {
+ return this.getDocumentElement(this.wrapper.find('.current-node-element')[0]);
+ },
+
+ getCurrentTextElement: function() {
+ return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
+ },
+
+
+
+ setCurrentElement: function(element, params) {
+ params = _.extend({caretTo: 'end'}, params);
+ var findFirstDirectTextChild = function(e, nodeToLand) {
+ var byBrowser = this.getCursor().getPosition().element;
+ if(byBrowser && byBrowser.parent().sameNode(nodeToLand))
+ return byBrowser;
+ var children = e.children();
+ for(var i = 0; i < children.length; i++) {
+ if(children[i] instanceof documentElement.DocumentTextElement)
+ return children[i];
+ }
+ return null;
+ }.bind(this);
+ var _markAsCurrent = function(element) {
+ if(element instanceof documentElement.DocumentTextElement) {
+ this.wrapper.find('.current-text-element').removeClass('current-text-element');
+ element.dom().addClass('current-text-element');
+ } else {
+ this.wrapper.find('.current-node-element').removeClass('current-node-element')
+ element.dom().addClass('current-node-element');
+ this.publisher('currentElementChanged', element);
+ }
+ }.bind(this);
+
+
+ var isTextElement = element instanceof documentElement.DocumentTextElement,
+ nodeElementToLand = isTextElement ? element.parent() : element,
+ textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
+ currentTextElement = this.getCurrentTextElement(),
+ currentNodeElement = this.getCurrentNodeElement();
+
+ if(currentTextElement && !(currentTextElement.sameNode(textElementToLand)))
+ this.wrapper.find('.current-text-element').removeClass('current-text-element');
+
+ if(textElementToLand) {
+ _markAsCurrent(textElementToLand);
+ if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
+ this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
+ if(!(textElementToLand.sameNode(currentTextElement)))
+ this.publisher('currentTextElementSet', textElementToLand);
+ } else {
+ document.getSelection().removeAllRanges();