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);
101 var formatter_prefix = '_wlxml_formatter_';
103 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
104 $.extend(WLXMLDocument.prototype, {
105 ElementNodeFactory: WLXMLElementNode,
107 loadXML: function(xml) {
108 smartxml.Document.prototype.loadXML.call(this, xml);
109 $(this.dom).find(':not(iframe)').addBack().contents()
110 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
113 text = {original: el.text(), trimmed: $.trim(el.text())},
114 elParent = el.parent(),
115 hasSpanParent = elParent.prop('tagName') === 'SPAN',
116 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
117 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
120 var addInfo = function(toAdd, where, transformed, original) {
121 var parentContents = elParent.contents(),
122 idx = parentContents.index(el[0]),
123 prev = idx > 0 ? parentContents[idx-1] : null,
124 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
127 if(where === 'above') {
128 target = prev ? $(prev) : elParent;
129 key = prev ? 'orig_after' : 'orig_begin';
130 } else if(where === 'below') {
131 target = next ? $(next) : elParent;
132 key = next ? 'orig_before' : 'orig_end';
133 } else { throw new Object;}
135 target.data(formatter_prefix + key, toAdd);
136 if(transformed !== undefined) {
137 target.data(formatter_prefix + key + '_transformed', transformed);
139 if(original !== undefined) {
140 target.data(formatter_prefix + key + '_original', original);
144 text.transformed = text.trimmed;
146 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
147 var startSpace = /\s/g.test(text.original.substr(0,1)),
148 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
149 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
150 text.transformed += text.trimmed;
151 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
153 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
154 text.transformed = ' ';
158 if(!text.transformed) {
159 addInfo(text.original, 'below');
161 return true; // continue
164 if(text.transformed !== text.original) {
165 // if(!text.trimmed) {
166 // addInfo(text.original, 'below');
168 var startingMatch = text.original.match(/^\s+/g),
169 endingMatch = text.original.match(/\s+$/g),
170 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
171 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
173 if(endingWhiteSpace) {
174 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ')
175 endingWhiteSpace = endingWhiteSpace.substr(1);
176 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
179 if(startingWhiteSpace && text.trimmed) {
180 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ')
181 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
182 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
187 el.replaceWith(document.createTextNode(text.transformed));
189 this.trigger('contentSet');
196 WLXMLDocumentFromXML: function(xml) {
197 return new WLXMLDocument(xml);
200 WLXMLElementNodeFromXML: function(xml) {
201 return this.WLXMLDocumentFromXML(xml).root;