2 'libs/jquery-1.9.1.min',
 
   9 // DocumentElement represents a node from WLXML document rendered inside Canvas
 
  10 var DocumentElement = function(htmlElement, canvas) {
 
  11     if(arguments.length === 0)
 
  14     this.$element = $(htmlElement);
 
  16     this.wlxmlTag = this.$element.attr('wlxml-tag');
 
  19 $.extend(DocumentElement.prototype, {
 
  23     children: function() {
 
  25         if(this instanceof DocumentTextElement)
 
  29         var elementContent = this.$element.contents();
 
  31         elementContent.each(function(idx) {
 
  32             var childElement = documentElementFromHTMLElement(this, element.canvas);
 
  33             if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
 
  35             if(idx > 0 && childElement instanceof DocumentTextElement) {
 
  36                 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
 
  39             toret.push(childElement);
 
  44         return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
 
  47     sameNode: function(other) {
 
  48         return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
 
  51     wrapWithNodeElement: function(wlxmlNode) {
 
  52         var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
 
  53         this.$element.replaceWith(wrapper.dom());
 
  58     childIndex: function(child) {
 
  59         var children = this.children(),
 
  61         children.forEach(function(c, idx) {
 
  62             if(c.sameNode(child)) {
 
  71         this.$element.detach();
 
  77 var DocumentNodeElement = function(htmlElement, canvas) {
 
  78     DocumentElement.call(this, htmlElement, canvas);
 
  81 var DocumentTextElement = function(htmlElement, canvas) {
 
  82     DocumentElement.call(this, htmlElement, canvas);
 
  85 DocumentNodeElement.prototype = new DocumentElement();
 
  86 DocumentTextElement.prototype = new DocumentElement();
 
  88 var manipulate = function(e, params, action) {
 
  90     if(params instanceof DocumentElement) {
 
  93         dom = DocumentNodeElement.createDOM(params);
 
  95     e.$element[action](dom);
 
  96     return documentElementFromHTMLElement(dom);
 
  99 $.extend(DocumentNodeElement.prototype, {
 
 100     append: function(params) {
 
 101         manipulate(this, params, 'append');
 
 103     before: function(params) {
 
 104         manipulate(this, params, 'before');
 
 107     after: function(params) {
 
 108         manipulate(this, params, 'after');
 
 110     getWlxmlTag: function() {
 
 111         return this.$element.attr('wlxml-tag');
 
 113     setWlxmlTag: function(tag) {
 
 114         this.$element.attr('wlxml-tag', tag);
 
 116     getWlxmlClass: function() {
 
 117         var klass = this.$element.attr('wlxml-class');
 
 119             return klass.replace('-', '.');
 
 122     setWlxmlClass: function(klass) {
 
 124             this.$element.attr('wlxml-class', klass);
 
 126             this.$element.removeAttr('wlxml-class');
 
 129         if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('wlxml-class')))
 
 135 DocumentNodeElement.createDOM = function(params) {
 
 138         dom = $(document.createTextNode(params.text));
 
 140         dom = $('<div>').attr('wlxml-tag', params.tag);
 
 142             dom.attr('wlxml-class', params.klass);
 
 148 DocumentNodeElement.create = function(params, canvas) {
 
 149     return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
 
 153 $.extend(DocumentTextElement.prototype, {
 
 154     setText: function(text) {
 
 155         this.$element[0].data = text;
 
 157     getText: function() {
 
 158         return this.$element.text();
 
 160     after: function(params) {
 
 161         if(params instanceof DocumentTextElement || params.text)
 
 164         if(params instanceof DocumentNodeElement) {
 
 167             dom = DocumentNodeElement.createDOM(params);
 
 169         this.$element.wrap('<div>');
 
 170         this.$element.parent().after(dom[0]);
 
 171         this.$element.unwrap();
 
 172         return documentElementFromHTMLElement(dom[0]);
 
 174     before: function(params) {
 
 175         if(params instanceof DocumentTextElement || params.text)
 
 178         if(params instanceof DocumentNodeElement) {
 
 181             dom = DocumentNodeElement.createDOM(params);
 
 183         this.$element.wrap('<div>');
 
 184         this.$element.parent().before(dom[0]);
 
 185         this.$element.unwrap();
 
 186         return documentElementFromHTMLElement(dom[0]);
 
 188     wrapWithNodeElement: function(wlxmlNode) {
 
 189         if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
 
 190             return this.canvas.wrapText({
 
 191                 inside: this.parent(),
 
 192                 textNodeIdx: this.parent().childIndex(this),
 
 193                 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
 
 194                 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
 
 195                 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
 
 198             return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
 
 202         if(this.parent().children().length === 1) {
 
 203             var parent = this.parent();
 
 208     split: function(params) {
 
 209         var parentElement = this.parent(),
 
 210             myIdx = parentElement.childIndex(this),
 
 211             myCanvas = this.canvas,
 
 213             succeedingChildren = [],
 
 215             prefix = this.getText().substr(0, params.offset),
 
 216             suffix = this.getText().substr(params.offset);
 
 218         parentElement.children().forEach(function(child) {
 
 220                 succeedingChildren.push(child);
 
 221             if(child.sameNode(thisElement))
 
 225         if(prefix.length > 0)
 
 226             this.setText(prefix);
 
 230         var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
 
 231         parentElement.after(newElement);
 
 233         if(suffix.length > 0)
 
 234             newElement.append({text: suffix});
 
 235         succeedingChildren.forEach(function(child) {
 
 236             newElement.append(child);
 
 241 var documentElementFromHTMLElement = function(htmlElement, canvas) {
 
 243     //    throw 'no canvas specified';
 
 244     if(htmlElement.nodeType === Node.ELEMENT_NODE)
 
 245         return new DocumentNodeElement(htmlElement, canvas);
 
 246     if(htmlElement.nodeType === Node.TEXT_NODE)
 
 247         return new DocumentTextElement(htmlElement, canvas);
 
 251     wrap: function(htmlElement, canvas) {
 
 252         return documentElementFromHTMLElement(htmlElement, canvas);
 
 254     DocumentElement: DocumentElement,
 
 255     DocumentNodeElement: DocumentNodeElement,
 
 256     DocumentTextElement: DocumentTextElement