2 'libs/jquery-1.9.1.min'
7 // DocumentElement represents a node from WLXML document rendered inside Canvas
8 var DocumentElement = function(htmlElement) {
9 if(arguments.length === 0)
11 this.$element = $(htmlElement);
12 this.wlxmlTag = this.$element.prop('tagName');
15 $.extend(DocumentElement.prototype, {
16 children: function() {
18 if(this instanceof DocumentTextElement)
22 var elementContent = this.$element.contents();
23 elementContent.each(function(idx) {
24 var element = documentElementFromHTMLElement(this);
25 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (element instanceof DocumentTextElement) && $.trim($(this).text()) === '')
27 if(idx > 0 && element instanceof DocumentTextElement) {
28 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
36 return documentElementFromHTMLElement(this.$element.parent()[0]);
39 sameNode: function(other) {
40 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
43 wrapWithNodeElement: function(wlxmlNode) {
44 this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
45 return documentElementFromHTMLElement(this.$element.parent().get(0));
48 childIndex: function(child) {
49 var children = this.children(),
51 children.forEach(function(c, idx) {
52 if(c.sameNode(child)) {
61 var DocumentNodeElement = function(htmlElement) {
62 DocumentElement.call(this, htmlElement);
65 var DocumentTextElement = function(htmlElement) {
66 DocumentElement.call(this, htmlElement);
69 DocumentNodeElement.prototype = new DocumentElement();
70 DocumentTextElement.prototype = new DocumentElement();
72 var manipulate = function(e, params, action) {
74 if(params instanceof DocumentElement) {
77 dom = DocumentNodeElement.createDOM(params);
79 e.$element[action](dom);
80 return documentElementFromHTMLElement(dom);
83 $.extend(DocumentNodeElement.prototype, {
84 append: function(params) {
85 manipulate(this, params, 'append');
87 before: function(params) {
88 manipulate(this, params, 'before');
91 after: function(params) {
92 manipulate(this, params, 'after');
96 DocumentNodeElement.createDOM = function(params) {
99 dom = $(document.createTextNode(params.text));
101 dom = $('<' + params.tag + '>');
103 dom.attr('class', params.klass);
108 $.extend(DocumentTextElement.prototype, {
109 setText: function(text) {
110 this.$element[0].data = text;
112 getText: function() {
113 return this.$element.text();
117 var documentElementFromHTMLElement = function(htmlElement) {
118 if(htmlElement.nodeType === Node.ELEMENT_NODE)
119 return new DocumentNodeElement(htmlElement);
120 if(htmlElement.nodeType === Node.TEXT_NODE)
121 return new DocumentTextElement(htmlElement);
125 wrap: function(htmlElement) {
126 return documentElementFromHTMLElement(htmlElement);
128 DocumentElement: DocumentElement,
129 DocumentNodeElement: DocumentNodeElement,
130 DocumentTextElement: DocumentTextElement