-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
299 lines (276 loc) · 8.93 KB
/
index.js
File metadata and controls
299 lines (276 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const XHTML = "http://www.w3.org/1999/xhtml"
, AFNS = "http://apifusion.com/ui/vc/1.0"
, DEFAULT_XML = '<?xml version="1.0" encoding="UTF-8"?><r/>';
var mod =
{ load : function load( name, req, onLoad /*, config*/ ) // AMD plugin API
{
return getXml( req.toUrl( name ) ).then( onLoad, onLoad );
}
, getXml : getXml
, onSetHeader : function(xhr){}
, createXml : createXml
, fromXmlStr : fromXmlStr
, transform : transform // ( xml, xsl, el )
, XPath_node : function XPath_node( xPath, node )
{
var d = node.ownerDocument || node
, nsResolver = d.createNSResolver && d.createNSResolver(d.documentElement);
if( d.evaluate )
return (node.ownerDocument || node)
.evaluate(xPath, node, nsResolver, 9, null)
.singleNodeValue;
d.setProperty('SelectionLanguage', 'XPath');
d.setProperty('SelectionNamespaces', 'xmlns:xsl="http://www.w3.org/1999/XSL/Transform"');
return node.selectSingleNode( xPath );//,nsmgr )
}
, XPath_nl : XPath_nl
, $ : XPath_nl
, o2xml : object2Xml // ( o, tag=<root>, node=CrateElement() )
, toXmlString : toXmlString
, createElement : createElement
, cleanElement : cleanElement
, nodeText : function nodeText( node ){ return node && (node.textContent || node.text) }// IE compatibility
, DEFAULT_XML : DEFAULT_XML
, promise : CreatePromise
};
if( 'ActiveXObject' in window ) // IE shim
{ mod.onSetHeader = function(xhr){ try { xhr.responseType = "msxml-document"; }catch(err){} }; // Helping IE11
mod.transform = function transform( xml, xsl, el )
{
xsl.setProperty( "AllowXsltScript", true );
var txt = xml.transformNode(xsl);
if( el )
el.innerHTML = txt;
return txt;
};
mod.fromXmlStr = function fromXmlStr( xmlString )
{
var doc = new ActiveXObject( "Msxml2.DOMDocument.6.0" );
doc.loadXML( xmlString );
return doc;
};
mod.createXml = function createXml()
{
var doc = new ActiveXObject( "Msxml2.DOMDocument.6.0" );
doc.loadXML( DEFAULT_XML );
return doc;
};
}
export default mod;
function
CreatePromise( executor )
{
return new Promise( executor )
}
function
XPath_nl( /* string | Array */ xPath, node )
{
var d = ( node || xPath[0] ).ownerDocument || node
, nl = [];
nl.ownerDocument = d;
if( "string" == typeof xPath )
nl = xpath2arr(xPath, node);
else
nl.push.apply(nl,xPath);
// WindJetQuery inlined
var o = nl[0] || createElement('b',d);
forEachProp( o, function(v,name)
{
nl[name] = function
invokeElementMethod()
{
var args = arguments
, i = 0
, max = this.length;
this._ret = [];
for( ; i<max ; i++ )
{ var el = this[i]
, v = el[name];
if( "function" === typeof v )
v = v.apply( el, args );
else if( args.length ) // setter
v = el[name] = args[ i % args.length ];
this._ret[i] = v;
}
return this;
}
}, nl );
nl.attr = function( k,v )
{ return arguments.length>1
? nl.setAttribute(k,v)
: nl[0] && nl[0].getAttribute(k);
};
nl.val = function(){ return this[0] && this[0].value; };
nl.createChild = createChild;
nl.$ = function(xp)
{ var ret = [];
this.forEach( function( el )
{
ret.push.apply( ret, xpath2arr(xp,el) );
});
return XPath_nl( ret, d );
};
nl.$ret = function(){ return XPath_nl( this._ret, d ); };
return nl;
function
xpath2arr(xPath, node)
{
var nl =[]
, e, xr
, nsResolver = d.createNSResolver && d.createNSResolver(d.documentElement);
if( d.evaluate )
{ xr = (node.ownerDocument || node).evaluate(xPath, node, nsResolver, 0, null);
while (e = xr.iterateNext())
nl.push(e);
}else
{
d.setProperty('SelectionLanguage', 'XPath');
d.setProperty('SelectionNamespaces', 'xmlns:xsl="http://www.w3.org/1999/XSL/Transform"');
var i = 0, l = node.selectNodes( xPath );//, nsmgr );
for( ;i<l.length; i++ )
nl.push(l[i]);
}
return nl;
}
}
function
createChild( name, attrs )
{ this.forEach( function(n,i)
{ var c = this._ret[i] = n.ownerDocument.createElementNS( AFNS, name );
for( var a in attrs )
c.setAttribute( a, attrs[a] );
n.appendChild( c );
}, this);
return this;
}
function /* @returns { Promise<XMLDocument>, options:{headers:{}, method:'GET',async:true,responseHeaders:{}} }
The promise that the result will load. */
getXml ( url
, options /* XHR properties or headers hashmap */
)
{
if(!options )
options = {};
options.method = options.method || "GET";
var xhr = new XMLHttpRequest(); // new ActiveXObject("Msxml2.XMLHTTP") // IE
forEachProp( options, function( v, k ){ xhr[k] = v; });
var p = mod.promise( function xhrExecutor( resolve, reject )
{
if( 'onerror' in xhr )
xhr.onerror = reject;
xhr.onreadystatechange = function ()
{
if( 4 !== xhr.readyState )
return;
options.responseHeaders = xhr.getAllResponseHeaders();
options.requestUrl = url;
if( 200 !== xhr.status )
return reject( new Error( xhr.status + " " + xhr.statusText +" @ " + url ), xhr );
// todo 300+ redirect
try
{ if( xhr.responseXML )
resolve( xhr.responseXML );
else
resolve( new DOMParser().parseFromString( xhr.responseText, "application/xml" ) );
}catch( ex )
{ reject( ex ); }
};
xhr.open( options.method, url, true );
xhr.setRequestHeader && xhr.setRequestHeader("Accept", "application/xml, text/xml, application/xhtml+xml, text/xsl, text/html, text/plain");
xhr.setRequestHeader && forEachProp( options.headers ||{}, function( v, k ){ xhr.setRequestHeader(k,v); });
mod.onSetHeader(xhr);
xhr.send();
});
p.xhr = xhr;
return p;
}
function
transform( xml, xsl, el )
{
var p = new XSLTProcessor();
p.importStylesheet(xsl);
if( el )
{ cleanElement(el);
el.appendChild( p.transformToFragment( xml, el.ownerDocument ) );
return el;
}else
return p.transformToDocument(xml);
// doc && ( doc.documentElement.outerHTML || doc.documentElement.outerXML );
}
function
createXml()
{
return new DOMParser().parseFromString( DEFAULT_XML, "application/xml" );
}
function
fromXmlStr( xmlStr )
{
return new DOMParser().parseFromString( xmlStr, "application/xml" );
}
// todo xml2Object, tests
function
object2Xml( o, tag, node )
{
// object2Xml( { aa:[1,2], b:{a:'asd'},c:'qwe'},'root')
// returns <root><aa><r>1</r><r>1</r></aa><c>qwe</c></root>
node = node || mod.createXml().documentElement;
var n = createEl( tag || 'root');
if( o instanceof Array )
o.forEach( function( el )
{ object2Xml( el, 'r', n ); });
else if( o instanceof Object )
forEachProp( o, function( v, k )
{ object2Xml( v, k, n ); });
else
{ var t = n.ownerDocument.createTextNode( '' + o );
n.appendChild(t);
}
return n;
function createEl(k){ var e = mod.createElement( k, node.ownerDocument || node ); node.appendChild(e); return e; }
}
function
toXmlString( xml )
{
if( isXmlDom( xml ) )
return xml.xml || xml.outerHTML;
if( typeof xml === 'string' )
{ if( xml.charAt(0) === '<' )// if started with '<', broken or not, return same string
return xml;
return '<r><![CDATA['+xml.replace( /\\</g ,'<' )+']]></r>'
}
var x = object2Xml(xml);
return x.xml || x.outerHTML;
}
function
isXmlDom( xml )
{
return xml && ( xml instanceof Node || (typeof xml ==='object' && 'transformNode' in xml ) );
}
function
createElement( name, document, nsUrl )
{
return nsUrl && document.createElementNS ? document.createElementNS( nsUrl, name ) : document.createElement(name);
}
function
cleanElement( el )
{
while( el && el.lastChild)
el.removeChild(el.lastChild);
}
function
forEachProp( o, onProp, scope )
{
if( !o )
return;
var n;
if( scope )
if( 'string' === typeof onProp )
for( n in o )
scope[onProp].call( scope, o[n], n, o );
else
for( n in o )
onProp.call( scope, o[n], n, o );
else
for( n in o )
onProp( o[n], n, o );
}