X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6435513192b5be363719dcb1559c93b52944800d..1d8d7b5fad1547bc2356f2e69e18ca597c969d9b:/modules/documentCanvas/canvas/documentElement.js diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 50e680d..9befd7e 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -22,16 +22,27 @@ $.extend(DocumentElement.prototype, { var elementContent = this.$element.contents(); elementContent.each(function(idx) { var element = documentElementFromHTMLElement(this); - if( - (toret.length === 0 && (element instanceof DocumentNodeElement)) || - (toret.length > 0 && (toret[toret.length -1] instanceof DocumentNodeElement) && (element instanceof DocumentNodeElement)) - ) - toret.push(documentElementFromHTMLElement(document.createTextNode())); + if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (element instanceof DocumentTextElement) && $.trim($(this).text()) === '') + return true; + if(idx > 0 && element instanceof DocumentTextElement) { + if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') + return true; + } toret.push(element); - if((idx === elementContent.length - 1) && (element instanceof DocumentNodeElement)) - toret.push(documentElementFromHTMLElement(document.createTextNode())); }); return toret; + }, + parent: function() { + return documentElementFromHTMLElement(this.$element.parent()[0]); + }, + + sameNode: function(other) { + return other && (typeof other === typeof this) && other.$element[0] === this.$element[0]; + }, + + wrapWithNodeElement: function(wlxmlNode) { + this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]); + return documentElementFromHTMLElement(this.$element.parent().get(0)); } }); @@ -46,6 +57,16 @@ var DocumentTextElement = function(htmlElement) { DocumentNodeElement.prototype = new DocumentElement(); DocumentTextElement.prototype = new DocumentElement(); +$.extend(DocumentNodeElement.prototype, { + append: function(params) { + var to_append = $('<' + params.tag + '>'); + if(params.klass) + to_append.attr('class', params.klass); + this.$element.append(to_append); + return documentElementFromHTMLElement(to_append); + } +}) + var documentElementFromHTMLElement = function(htmlElement) { if(htmlElement.nodeType === Node.ELEMENT_NODE) return new DocumentNodeElement(htmlElement);