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     var tagNameProp = this.$element.prop('tagName');
 
  17     this.wlxmlTag = tagNameProp ? tagNameProp.toLowerCase() : undefined;
 
  20 $.extend(DocumentElement.prototype, {
 
  24     children: function() {
 
  26         if(this instanceof DocumentTextElement)
 
  30         var elementContent = this.$element.contents();
 
  32         elementContent.each(function(idx) {
 
  33             var childElement = documentElementFromHTMLElement(this, element.canvas);
 
  34             if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
 
  36             if(idx > 0 && childElement instanceof DocumentTextElement) {
 
  37                 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
 
  40             toret.push(childElement);
 
  45         return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
 
  48     sameNode: function(other) {
 
  49         return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
 
  52     wrapWithNodeElement: function(wlxmlNode) {
 
  53         this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass.replace('.', '-') + '">')[0]);
 
  54         return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
 
  57     childIndex: function(child) {
 
  58         var children = this.children(),
 
  60         children.forEach(function(c, idx) {
 
  61             if(c.sameNode(child)) {
 
  70         this.$element.detach();
 
  76 var DocumentNodeElement = function(htmlElement, canvas) {
 
  77     DocumentElement.call(this, htmlElement, canvas);
 
  80 var DocumentTextElement = function(htmlElement, canvas) {
 
  81     DocumentElement.call(this, htmlElement, canvas);
 
  84 DocumentNodeElement.prototype = new DocumentElement();
 
  85 DocumentTextElement.prototype = new DocumentElement();
 
  87 var manipulate = function(e, params, action) {
 
  89     if(params instanceof DocumentElement) {
 
  92         dom = DocumentNodeElement.createDOM(params);
 
  94     e.$element[action](dom);
 
  95     return documentElementFromHTMLElement(dom);
 
  98 $.extend(DocumentNodeElement.prototype, {
 
  99     append: function(params) {
 
 100         manipulate(this, params, 'append');
 
 102     before: function(params) {
 
 103         manipulate(this, params, 'before');
 
 106     after: function(params) {
 
 107         manipulate(this, params, 'after');
 
 109     setWlxmlClass: function(klass) {
 
 110         this.$element.attr('class', klass);
 
 113         if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('class')))
 
 119 DocumentNodeElement.createDOM = function(params) {
 
 122         dom = $(document.createTextNode(params.text));
 
 124         dom = $('<' + params.tag + '>');
 
 126             dom.attr('class', params.klass);
 
 132 DocumentNodeElement.create = function(params, canvas) {
 
 133     return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
 
 137 $.extend(DocumentTextElement.prototype, {
 
 138     setText: function(text) {
 
 139         this.$element[0].data = text;
 
 141     getText: function() {
 
 142         return this.$element.text();
 
 144     after: function(params) {
 
 145         if(params instanceof DocumentTextElement || params.text)
 
 148         if(params instanceof DocumentNodeElement) {
 
 151             dom = DocumentNodeElement.createDOM(params);
 
 153         this.$element.wrap('<div>');
 
 154         this.$element.parent().after(dom[0]);
 
 155         this.$element.unwrap();
 
 156         return documentElementFromHTMLElement(dom[0]);
 
 158     before: function(params) {
 
 159         if(params instanceof DocumentTextElement || params.text)
 
 162         if(params instanceof DocumentNodeElement) {
 
 165             dom = DocumentNodeElement.createDOM(params);
 
 167         this.$element.wrap('<div>');
 
 168         this.$element.parent().before(dom[0]);
 
 169         this.$element.unwrap();
 
 170         return documentElementFromHTMLElement(dom[0]);
 
 172     wrapWithNodeElement: function(wlxmlNode) {
 
 173         if(wlxmlNode.start && wlxmlNode.end) {
 
 174             return this.canvas.wrapText({
 
 175                 inside: this.parent(),
 
 176                 textNodeIdx: this.parent().childIndex(this),
 
 177                 offsetStart: wlxmlNode.start,
 
 178                 offsetEnd: wlxmlNode.end,
 
 179                 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
 
 182             return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
 
 185     split: function(params) {
 
 186         var parentElement = this.parent(),
 
 187             myIdx = parentElement.childIndex(this),
 
 188             myCanvas = this.canvas,
 
 190             succeedingChildren = [],
 
 192             prefix = this.getText().substr(0, params.offset),
 
 193             suffix = this.getText().substr(params.offset);
 
 195         parentElement.children().forEach(function(child) {
 
 197                 succeedingChildren.push(child);
 
 198             if(child.sameNode(thisElement))
 
 202         if(prefix.length > 0)
 
 203             this.setText(prefix);
 
 207         var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
 
 208         parentElement.after(newElement);
 
 210         if(suffix.length > 0)
 
 211             newElement.append({text: suffix});
 
 212         succeedingChildren.forEach(function(child) {
 
 213             newElement.append(child);
 
 218 var documentElementFromHTMLElement = function(htmlElement, canvas) {
 
 220     //    throw 'no canvas specified';
 
 221     if(htmlElement.nodeType === Node.ELEMENT_NODE)
 
 222         return new DocumentNodeElement(htmlElement, canvas);
 
 223     if(htmlElement.nodeType === Node.TEXT_NODE)
 
 224         return new DocumentTextElement(htmlElement, canvas);
 
 228     wrap: function(htmlElement, canvas) {
 
 229         return documentElementFromHTMLElement(htmlElement, canvas);
 
 231     DocumentElement: DocumentElement,
 
 232     DocumentNodeElement: DocumentNodeElement,
 
 233     DocumentTextElement: DocumentTextElement