2 'libs/jquery-1.9.1.min'
8 // DocumentElement represents a node from WLXML document rendered inside Canvas
9 var DocumentElement = function(htmlElement, canvas) {
10 if(arguments.length === 0)
13 this.$element = $(htmlElement);
14 this.wlxmlTag = this.$element.prop('tagName');
17 $.extend(DocumentElement.prototype, {
21 children: function() {
23 if(this instanceof DocumentTextElement)
27 var elementContent = this.$element.contents();
29 elementContent.each(function(idx) {
30 var childElement = documentElementFromHTMLElement(this, element.canvas);
31 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
33 if(idx > 0 && childElement instanceof DocumentTextElement) {
34 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
37 toret.push(childElement);
42 return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
45 sameNode: function(other) {
46 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
49 wrapWithNodeElement: function(wlxmlNode) {
50 this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
51 return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
54 childIndex: function(child) {
55 var children = this.children(),
57 children.forEach(function(c, idx) {
58 if(c.sameNode(child)) {
67 this.$element.detach();
73 var DocumentNodeElement = function(htmlElement, canvas) {
74 DocumentElement.call(this, htmlElement, canvas);
77 var DocumentTextElement = function(htmlElement, canvas) {
78 DocumentElement.call(this, htmlElement, canvas);
81 DocumentNodeElement.prototype = new DocumentElement();
82 DocumentTextElement.prototype = new DocumentElement();
84 var manipulate = function(e, params, action) {
86 if(params instanceof DocumentElement) {
89 dom = DocumentNodeElement.createDOM(params);
91 e.$element[action](dom);
92 return documentElementFromHTMLElement(dom);
95 $.extend(DocumentNodeElement.prototype, {
96 append: function(params) {
97 manipulate(this, params, 'append');
99 before: function(params) {
100 manipulate(this, params, 'before');
103 after: function(params) {
104 manipulate(this, params, 'after');
108 DocumentNodeElement.createDOM = function(params) {
111 dom = $(document.createTextNode(params.text));
113 dom = $('<' + params.tag + '>');
115 dom.attr('class', params.klass);
121 DocumentNodeElement.create = function(params) {
122 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
126 $.extend(DocumentTextElement.prototype, {
127 setText: function(text) {
128 this.$element[0].data = text;
130 getText: function() {
131 return this.$element.text();
133 after: function(params) {
134 if(params instanceof DocumentTextElement || params.text)
137 if(params instanceof DocumentNodeElement) {
140 dom = DocumentNodeElement.createDOM(params);
142 this.$element.wrap('<div>');
143 this.$element.parent().after(dom[0]);
144 this.$element.unwrap();
145 return documentElementFromHTMLElement(dom[0]);
147 wrapWithNodeElement: function(wlxmlNode) {
148 if(wlxmlNode.start && wlxmlNode.end) {
149 return this.canvas.wrapText({
150 inside: this.parent(),
151 textNodeIdx: this.parent().childIndex(this),
152 offsetStart: wlxmlNode.start,
153 offsetEnd: wlxmlNode.end,
154 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
157 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
162 var documentElementFromHTMLElement = function(htmlElement, canvas) {
164 // throw 'no canvas specified';
165 if(htmlElement.nodeType === Node.ELEMENT_NODE)
166 return new DocumentNodeElement(htmlElement, canvas);
167 if(htmlElement.nodeType === Node.TEXT_NODE)
168 return new DocumentTextElement(htmlElement, canvas);
172 wrap: function(htmlElement, canvas) {
173 return documentElementFromHTMLElement(htmlElement, canvas);
175 DocumentElement: DocumentElement,
176 DocumentNodeElement: DocumentNodeElement,
177 DocumentTextElement: DocumentTextElement