3 ], function(smartxml) {
9 var isMetaAttribute = function(attrName) {
10 return attrName.substr(0, 5) === 'meta-';
15 var WLXMLElementNode = function(nativeNode, document) {
16 smartxml.ElementNode.call(this, nativeNode, document);
18 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
20 $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
21 getClass: function() {
22 return this.getAttr('class');
24 setClass: function(klass) {
25 return this.setAttr('class', klass);
27 getMetaAttributes: function() {
29 this.getAttrs().forEach(function(attr) {
30 if(isMetaAttribute(attr.name)) {
31 toret.push({name: attr.name.substr(5), value: attr.value});
36 getOtherAttributes: function() {
38 this.getAttrs().forEach(function(attr) {
39 if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
40 toret[attr.name] = attr.value;
46 _getXMLDOMToDump: function() {
47 var DOM = this._$.clone(true, true);
49 DOM.find('*').addBack().each(function() {
52 contents = parent.contents(),
53 idx = contents.index(el),
59 if(data[formatter_prefix+ 'orig_before']) {
60 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
61 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
62 txt.data = data[formatter_prefix+ 'orig_before_original'];
64 el.before(data[formatter_prefix+ 'orig_before']);
67 if(data[formatter_prefix+ 'orig_after']) {
68 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
69 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
70 txt.data = data[formatter_prefix+ 'orig_after_original'];
72 el.after(data[formatter_prefix+ 'orig_after']);
75 if(data[formatter_prefix+ 'orig_begin']) {
76 el.prepend(data[formatter_prefix+ 'orig_begin']);
78 if(data[formatter_prefix+ 'orig_end']) {
79 contents = el.contents();
80 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
81 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
82 txt.data = data[formatter_prefix+ 'orig_end_original'];
84 el.append(data[formatter_prefix+ 'orig_end']);
97 var WLXMLDocument = function(xml) {
98 smartxml.Document.call(this, xml);
100 $(this.dom).find(':not(iframe)').addBack().contents()
101 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
104 text = {original: el.text(), trimmed: $.trim(el.text())},
105 elParent = el.parent(),
106 hasSpanParent = elParent.prop('tagName') === 'SPAN',
107 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
108 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
111 var addInfo = function(toAdd, where, transformed, original) {
112 var parentContents = elParent.contents(),
113 idx = parentContents.index(el[0]),
114 prev = idx > 0 ? parentContents[idx-1] : null,
115 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
118 if(where === 'above') {
119 target = prev ? $(prev) : elParent;
120 key = prev ? 'orig_after' : 'orig_begin';
121 } else if(where === 'below') {
122 target = next ? $(next) : elParent;
123 key = next ? 'orig_before' : 'orig_end';
124 } else { throw new Object;}
126 target.data(formatter_prefix + key, toAdd);
127 if(transformed !== undefined) {
128 target.data(formatter_prefix + key + '_transformed', transformed);
130 if(original !== undefined) {
131 target.data(formatter_prefix + key + '_original', original);
135 text.transformed = text.trimmed;
137 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
138 var startSpace = /\s/g.test(text.original.substr(0,1)),
139 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
140 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
141 text.transformed += text.trimmed;
142 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
144 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
145 text.transformed = ' ';
149 if(!text.transformed) {
150 addInfo(text.original, 'below');
152 return true; // continue
155 if(text.transformed !== text.original) {
156 // if(!text.trimmed) {
157 // addInfo(text.original, 'below');
159 var startingMatch = text.original.match(/^\s+/g),
160 endingMatch = text.original.match(/\s+$/g),
161 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
162 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
164 if(endingWhiteSpace) {
165 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ')
166 endingWhiteSpace = endingWhiteSpace.substr(1);
167 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
170 if(startingWhiteSpace && text.trimmed) {
171 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ')
172 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
173 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
178 el.replaceWith(document.createTextNode(text.transformed));
182 var formatter_prefix = '_wlxml_formatter_';
184 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
185 $.extend(WLXMLDocument.prototype, {
186 ElementNodeFactory: WLXMLElementNode,
191 WLXMLDocumentFromXML: function(xml) {
192 return new WLXMLDocument(xml);
195 WLXMLElementNodeFromXML: function(xml) {
196 return this.WLXMLDocumentFromXML(xml).root;