@webqit/oohtml 2.1.6 → 2.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bindings-api.js +2 -0
- package/dist/bindings-api.js.map +7 -0
- package/dist/context-api.js +1 -1
- package/dist/context-api.js.map +2 -2
- package/dist/html-imports.js +1 -1
- package/dist/html-imports.js.map +2 -2
- package/dist/html-modules.js +1 -1
- package/dist/html-modules.js.map +2 -2
- package/dist/main.js +13 -13
- package/dist/main.js.map +3 -3
- package/dist/namespaced-html.js +1 -1
- package/dist/namespaced-html.js.map +2 -2
- package/dist/scoped-js.js +14 -14
- package/dist/scoped-js.js.map +3 -3
- package/dist/state-api.js +1 -1
- package/dist/state-api.js.map +2 -2
- package/package.json +5 -5
- package/src/bindings-api/index.js +85 -0
- package/src/index.js +2 -2
- package/src/scoped-js/Compiler.js +294 -0
- package/src/scoped-js/index.js +22 -311
- package/test/bindings-api.test.js +42 -0
- package/src/state-api/index.js +0 -59
- package/test/state-api.test.js +0 -34
- /package/src/{state-api → bindings-api}/targets.browser.js +0 -0
package/src/scoped-js/index.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { resolveParams } from '@webqit/subscript/src/params.js';
|
|
6
6
|
import SubscriptFunction from '@webqit/subscript/src/SubscriptFunctionLite.js';
|
|
7
7
|
import Observer from '@webqit/observer';
|
|
8
|
+
import Compiler from './Compiler.js';
|
|
8
9
|
import wqDom from '@webqit/dom';
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -41,50 +42,6 @@ export {
|
|
|
41
42
|
Observer,
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
/**
|
|
45
|
-
* Performs realtime capture of elements and builds their relationships.
|
|
46
|
-
*
|
|
47
|
-
* @param Object params
|
|
48
|
-
*
|
|
49
|
-
* @return Void
|
|
50
|
-
*/
|
|
51
|
-
function realtime( params ) {
|
|
52
|
-
const window = this, { dom } = window.wq;
|
|
53
|
-
const handled = () => {};
|
|
54
|
-
dom.realtime( window.document ).observe( params.scriptSelector, record => {
|
|
55
|
-
record.entrants.forEach( script => {
|
|
56
|
-
if ( 'contract' in script ) return handled( script );
|
|
57
|
-
Object.defineProperty( script, 'contract', { value: script.hasAttribute( 'contract' ) } );
|
|
58
|
-
if ( 'scoped' in script ) return handled( script );
|
|
59
|
-
Object.defineProperty( script, 'scoped', { value: script.hasAttribute( 'scoped' ) } );
|
|
60
|
-
// ---
|
|
61
|
-
const thisContext = script.scoped ? record.target : ( script.type === 'module' ? undefined : window );
|
|
62
|
-
compile.call( this, script, thisContext, params );
|
|
63
|
-
} );
|
|
64
|
-
}, { subtree: true, timing: 'intercept', generation: 'entrants' } );
|
|
65
|
-
// ---
|
|
66
|
-
window.wq.oohtml.Script.run = ( execHash ) => {
|
|
67
|
-
const exec = fromHash( execHash );
|
|
68
|
-
if ( !exec ) throw new Error( `Argument must be a valid exec hash.` );
|
|
69
|
-
const { script, compiledScript, thisContext } = exec;
|
|
70
|
-
if ( thisContext instanceof window.Element && script.scoped ) {
|
|
71
|
-
if ( !thisContext.scripts ) { Object.defineProperty( thisContext, 'scripts', { value: new Set } ); }
|
|
72
|
-
thisContext.scripts.add( script );
|
|
73
|
-
}
|
|
74
|
-
switch ( params.script.retention ) {
|
|
75
|
-
case 'dispose':
|
|
76
|
-
script.remove();
|
|
77
|
-
break;
|
|
78
|
-
case 'hidden':
|
|
79
|
-
script.textContent = `"source hidden"`;
|
|
80
|
-
break;
|
|
81
|
-
default:
|
|
82
|
-
script.textContent = compiledScript.function.originalSource;
|
|
83
|
-
}
|
|
84
|
-
return execute.call( this, compiledScript, thisContext, script );
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
45
|
// ------------------
|
|
89
46
|
// Script runner
|
|
90
47
|
export function execute( compiledScript, thisContext, script ) {
|
|
@@ -122,274 +79,28 @@ export function execute( compiledScript, thisContext, script ) {
|
|
|
122
79
|
return script;
|
|
123
80
|
}
|
|
124
81
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
// Let's obtain a material functions
|
|
146
|
-
let _Function, { parserParams, compilerParams, runtimeParams } = params.config;
|
|
147
|
-
if ( script.contract ) {
|
|
148
|
-
parserParams = { ...parserParams, allowAwaitOutsideFunction: script.type === 'module' };
|
|
149
|
-
runtimeParams = { ...runtimeParams, async: script.type === 'module' };
|
|
150
|
-
_Function = SubscriptFunction( source, { compilerParams, parserParams, runtimeParams, } );
|
|
151
|
-
Object.defineProperty( script, 'properties', { configurable: true, value: SubscriptFunction.inspect( _Function, 'properties' ) } );
|
|
152
|
-
} else {
|
|
153
|
-
const isAsync = script.type === 'module'//meta.topLevelAwait || imports.length;
|
|
154
|
-
const _FunctionConstructor = isAsync ? Object.getPrototypeOf( async function() {} ).constructor : Function;
|
|
155
|
-
_Function = runtimeParams?.compileFunction
|
|
156
|
-
? runtimeParams.compileFunction( source )
|
|
157
|
-
: new _FunctionConstructor( source );
|
|
158
|
-
Object.defineProperty( _Function, 'originalSource', { configurable: true, value: script.textContent } );
|
|
159
|
-
}
|
|
160
|
-
// Save material function to compile cache
|
|
161
|
-
compiledScript = Object.defineProperty( script.cloneNode(), 'function', { value: _Function } );
|
|
162
|
-
script.scoped && Object.defineProperty( compiledScript, 'scoped', Object.getOwnPropertyDescriptor( script, 'scoped') );
|
|
163
|
-
script.contract && Object.defineProperty( compiledScript, 'contract', Object.getOwnPropertyDescriptor( script, 'contract') );
|
|
164
|
-
cache.set( sourceHash, compiledScript );
|
|
165
|
-
}
|
|
166
|
-
const execHash = toHash( { script, compiledScript, thisContext } );
|
|
167
|
-
script.textContent = `wq.oohtml.Script.run('${ execHash }');`;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// ------------------
|
|
171
|
-
// Match import statements
|
|
172
|
-
// and detect top-level await
|
|
173
|
-
export function parse( source ) {
|
|
174
|
-
const [ tokens, meta ] = tokenize( source, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
175
|
-
|
|
176
|
-
if ( event === 'start-enclosure' && char === '{' && !meta.openAsync?.type && meta.openEnclosures.length === meta.openAsync?.scopeId ) {
|
|
177
|
-
meta.openAsync.type = 'block';
|
|
178
|
-
} else if ( event === 'end-enclosure' && char === '}' && meta.openAsync?.type === 'block' && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
179
|
-
meta.openAsync = null;
|
|
180
|
-
} else if ( event === 'start-quote' && !meta.openEnclosures.length && [ 'starting', 'from' ].includes( meta.openImport ) ) {
|
|
181
|
-
meta.openImport = 'url';
|
|
182
|
-
} else if ( event === 'end-quote' && meta.openImport === 'url' ) {
|
|
183
|
-
meta.openImport = 'closing';
|
|
184
|
-
} else if ( event === 'char' ) {
|
|
185
|
-
|
|
186
|
-
if ( meta.openImport === 'closing' && (
|
|
187
|
-
char === ';'/* explicit */ || ![ ' ', `\n` ].includes( char )/* implicit */ || isLastChar
|
|
188
|
-
) ) {
|
|
189
|
-
if ( char === ';' || isLastChar ) {
|
|
190
|
-
$tokens[ 0 ] += char;
|
|
191
|
-
$tokens.unshift( '' );
|
|
192
|
-
} else { $tokens.unshift( char ); }
|
|
193
|
-
meta.openImport = null;
|
|
194
|
-
return false;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
let remainder = source.substring( i - 1 );
|
|
198
|
-
|
|
199
|
-
if ( !meta.openImport && /^[\W]?import[ ]*[^\(]/.test( remainder ) ) {
|
|
200
|
-
meta.openImport = 'starting';
|
|
201
|
-
$tokens.unshift( '' );
|
|
202
|
-
return 6;
|
|
203
|
-
}
|
|
204
|
-
if ( meta.openImport === 'starting' && /^[\W]?from /.test( remainder ) ) {
|
|
205
|
-
meta.openImport = 'from';
|
|
206
|
-
return 4;
|
|
207
|
-
}
|
|
208
|
-
if ( !meta.openAsync ) {
|
|
209
|
-
if ( /^[\W]?async /.test( remainder ) ) {
|
|
210
|
-
meta.openAsync = { scopeId: meta.openEnclosures.length };
|
|
211
|
-
return 5;
|
|
212
|
-
}
|
|
213
|
-
if ( /^[\W]?await /.test( remainder ) ) {
|
|
214
|
-
meta.topLevelAwait = true;
|
|
215
|
-
return 5;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if ( meta.openAsync ) {
|
|
219
|
-
if ( !meta.openAsync.type && /.?\=\>[ ]*?\{/.test( remainder ) ) {
|
|
220
|
-
meta.openAsync.type = 'inline-arrow';
|
|
221
|
-
} else if ( meta.openAsync.type === 'inline-arrow' && [ `\n`, ';' ].includes( char ) && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
222
|
-
meta.openAsync = null;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
} );
|
|
229
|
-
// Hoist all import statements
|
|
230
|
-
let imports = [], body = '', _str;
|
|
231
|
-
for ( const str of tokens.reverse() ) {
|
|
232
|
-
if ( ( _str = str.trim() ).startsWith( 'import ' ) ) {
|
|
233
|
-
imports.push( str );
|
|
234
|
-
} else if ( _str ) { body += str; }
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return [ imports, body, meta ];
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// ------------------
|
|
241
|
-
// Rewrite import statements
|
|
242
|
-
export function rewriteImportStmts( imports ) {
|
|
243
|
-
const importSpecs = [], importPromises = [];
|
|
244
|
-
imports.forEach( ( $import, i ) => {
|
|
245
|
-
$import = parseImportStmt( $import );
|
|
246
|
-
// Identify whole imports and individual imports
|
|
247
|
-
const [ wholeImport, individualImports ] = $import.items.reduce( ( [ whole, parts ], item ) => {
|
|
248
|
-
return item.id === '*' ? [ item.alias, parts ] : [ whole, parts.concat( item ) ];
|
|
249
|
-
}, [ null, [] ] );
|
|
250
|
-
if ( wholeImport ) {
|
|
251
|
-
// const main = await import("url");
|
|
252
|
-
importSpecs.push( `const ${ wholeImport } = __$imports$__[${ i }];` );
|
|
253
|
-
}
|
|
254
|
-
if ( individualImports.length ) {
|
|
255
|
-
// const { aa: bb, cc } = await import("url");
|
|
256
|
-
const individualImportsSpec = individualImports.map( item => `${ item.id }${ item.id !== item.alias ? `: ${ item.alias }` : '' }` ).join( ', ' );
|
|
257
|
-
importSpecs.push( `const { ${ individualImportsSpec } } = __$imports$__[${ i }];` );
|
|
258
|
-
}
|
|
259
|
-
importPromises.push( `import("${ $import.url }")` );
|
|
260
|
-
} );
|
|
261
|
-
return [
|
|
262
|
-
`\n\tconst __$imports$__ = await Promise.all([\n\t\t${ importPromises.join( `,\n\t\t` ) }\n\t]);`,
|
|
263
|
-
importSpecs.join( `\n\t` ),
|
|
264
|
-
];
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// ------------------
|
|
268
|
-
// Parse import statements
|
|
269
|
-
export function parseImportStmt( str ) {
|
|
270
|
-
const getUrl = str => {
|
|
271
|
-
let quo = /^[`'"]/.exec( str );
|
|
272
|
-
return quo && str.substring( 1, str.lastIndexOf( quo[ 0 ] ) );
|
|
273
|
-
}
|
|
274
|
-
let $import = { items: [ { id: '' } ] }, _str = str.replace( 'import', '' ).trim();
|
|
275
|
-
if ( !( $import.url = getUrl( _str ) ) ) {
|
|
276
|
-
tokenize( _str, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
277
|
-
if ( [ 'start-quote', 'ongoing-quote', 'end-quote', 'char' ].includes( event ) ) {
|
|
278
|
-
if ( $import.url ) return;
|
|
279
|
-
if ( !meta.openQuote ) {
|
|
280
|
-
let remainder = _str.substring( i );
|
|
281
|
-
if ( char === ',' ) {
|
|
282
|
-
$import.items.unshift( { id: '' } );
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
if ( remainder.startsWith( ' as ' ) ) {
|
|
286
|
-
$import.items[ 0 ].alias = '';
|
|
287
|
-
return 3;
|
|
288
|
-
}
|
|
289
|
-
if ( remainder.startsWith( ' from ' ) ) {
|
|
290
|
-
$import.url = getUrl( remainder.replace( 'from', '' ).trim() );
|
|
291
|
-
return remainder.length;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
if ( 'alias' in $import.items[ 0 ] ) {
|
|
295
|
-
$import.items[ 0 ].alias += char;
|
|
296
|
-
} else {
|
|
297
|
-
$import.items[ 0 ].id += char;
|
|
298
|
-
if ( meta.openEnclosures.length ) {
|
|
299
|
-
$import.items[ 0 ].enclosed = true;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
82
|
+
/**
|
|
83
|
+
* Performs realtime capture of elements and builds their relationships.
|
|
84
|
+
*
|
|
85
|
+
* @param Object params
|
|
86
|
+
*
|
|
87
|
+
* @return Void
|
|
88
|
+
*/
|
|
89
|
+
function realtime( params ) {
|
|
90
|
+
const window = this, { dom } = window.wq;
|
|
91
|
+
const compiler = new Compiler( window, params, execute ), handled = () => {};
|
|
92
|
+
dom.realtime( window.document ).observe( params.scriptSelector, record => {
|
|
93
|
+
record.entrants.forEach( script => {
|
|
94
|
+
if ( 'contract' in script ) return handled( script );
|
|
95
|
+
Object.defineProperty( script, 'contract', { value: script.hasAttribute( 'contract' ) } );
|
|
96
|
+
if ( 'scoped' in script ) return handled( script );
|
|
97
|
+
Object.defineProperty( script, 'scoped', { value: script.hasAttribute( 'scoped' ) } );
|
|
98
|
+
// ---
|
|
99
|
+
const thisContext = script.scoped ? record.target : ( script.type === 'module' ? undefined : window );
|
|
100
|
+
compiler.compile( script, thisContext );
|
|
303
101
|
} );
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
.map( item => ( {
|
|
307
|
-
id: item.id && !item.alias && !item.enclosed ? 'default' : item.id.trim(),
|
|
308
|
-
alias: item.alias ? item.alias.trim() : item.id.trim(),
|
|
309
|
-
} ) )
|
|
310
|
-
.filter( item => item.id )
|
|
311
|
-
.reverse();
|
|
312
|
-
return $import;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// ------------------
|
|
316
|
-
// Token JavaScript source
|
|
317
|
-
export function tokenize( source, _callback ) {
|
|
318
|
-
const lastI = source.length - 1;
|
|
319
|
-
return [ ...source ].reduce( ( [ $tokens, meta, skip ], char, i ) => {
|
|
320
|
-
|
|
321
|
-
if ( skip ) {
|
|
322
|
-
$tokens[ 0 ] += char;
|
|
323
|
-
return [ $tokens, meta, --skip ];
|
|
324
|
-
}
|
|
325
|
-
let callbackReturn;
|
|
326
|
-
|
|
327
|
-
if ( meta.openQuote || meta.openComment ) {
|
|
328
|
-
if ( char === meta.openQuote ) {
|
|
329
|
-
meta.openQuote = null;
|
|
330
|
-
callbackReturn = _callback( $tokens, 'end-quote', char, meta, i, i === lastI );
|
|
331
|
-
} else if ( meta.openQuote ) {
|
|
332
|
-
callbackReturn = _callback( $tokens, 'ongoing-quote', char, meta, i, i === lastI );
|
|
333
|
-
} else if ( meta.openComment ) {
|
|
334
|
-
if ( ( meta.openComment === '//' && char === `\n` ) || ( meta.openComment === '/*' && $tokens[ 0 ].substr( -1 ) === '*' && char === '/' ) ) {
|
|
335
|
-
meta.openComment = null;
|
|
336
|
-
callbackReturn = _callback( $tokens, 'end-comment', char, meta, i, i === lastI );
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
if ( callbackReturn !== false ) {
|
|
340
|
-
$tokens[ 0 ] += char;
|
|
341
|
-
}
|
|
342
|
-
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
let enclosure;
|
|
346
|
-
if ( enclosure = [ '()', '{}', '[]' ].filter( pair => char === pair[ 0 ] )[ 0 ] ) {
|
|
347
|
-
callbackReturn = _callback( $tokens, 'start-enclosure', char, meta, i, i === lastI );
|
|
348
|
-
meta.openEnclosures.unshift( enclosure );
|
|
349
|
-
} else if ( meta.openEnclosures.length && char === meta.openEnclosures[ 0 ][ 1 ] ) {
|
|
350
|
-
meta.openEnclosures.shift();
|
|
351
|
-
callbackReturn = _callback( $tokens, 'end-enclosure', char, meta, i, i === lastI );
|
|
352
|
-
} else if ( [ '"', "'", "`" ].includes( char ) ) {
|
|
353
|
-
callbackReturn = _callback( $tokens, 'start-quote', char, meta, i, i === lastI );
|
|
354
|
-
meta.openQuote = char;
|
|
355
|
-
} else if ( !meta.openComment && [ '/*', '//' ].includes( source.substr( i, 2 ) ) ) {
|
|
356
|
-
callbackReturn = _callback( $tokens, 'start-comment', char, meta, i, i === lastI );
|
|
357
|
-
meta.openComment = source.substr( i, 2 );
|
|
358
|
-
} else {
|
|
359
|
-
callbackReturn = _callback( $tokens, 'char', char, meta, i, i === lastI );
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if ( callbackReturn !== false ) {
|
|
363
|
-
$tokens[ 0 ] += char;
|
|
364
|
-
}
|
|
365
|
-
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
366
|
-
|
|
367
|
-
}, [ [ '' ], { openEnclosures: [], }, 0 ] );
|
|
102
|
+
}, { subtree: true, timing: 'intercept', generation: 'entrants' } );
|
|
103
|
+
// ---
|
|
368
104
|
}
|
|
369
105
|
|
|
370
|
-
// ------------------
|
|
371
|
-
// Unique ID generator
|
|
372
|
-
const hashTable = new Map;
|
|
373
|
-
const uniqId = () => (0|Math.random()*9e6).toString(36);
|
|
374
106
|
const _await = ( value, callback ) => value instanceof Promise ? value.then( callback ) : callback( value );
|
|
375
|
-
|
|
376
|
-
// ------------------
|
|
377
|
-
// Hash of anything generator
|
|
378
|
-
export function toHash( val ) {
|
|
379
|
-
let hash;
|
|
380
|
-
if ( !( hash = hashTable.get( val ) ) ) {
|
|
381
|
-
hash = uniqId();
|
|
382
|
-
hashTable.set( val, hash );
|
|
383
|
-
}
|
|
384
|
-
return hash;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// ------------------
|
|
388
|
-
// Value of any hash
|
|
389
|
-
export function fromHash( hash ) {
|
|
390
|
-
let val;
|
|
391
|
-
hashTable.forEach( ( _hash, _val ) => {
|
|
392
|
-
if ( _hash === hash ) val = _val;
|
|
393
|
-
} );
|
|
394
|
-
return val;
|
|
395
|
-
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @imports
|
|
4
|
+
*/
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import { createDocument } from './index.js';
|
|
7
|
+
import Observer from '@webqit/observer';
|
|
8
|
+
|
|
9
|
+
describe(`Bindings API`, function() {
|
|
10
|
+
|
|
11
|
+
describe( `Basic...`, function() {
|
|
12
|
+
|
|
13
|
+
const head = ``;
|
|
14
|
+
const body = ``;
|
|
15
|
+
const { document } = createDocument( head, body );
|
|
16
|
+
|
|
17
|
+
it ( `The document object and elements should expose a "bindings" property each...`, async function() {
|
|
18
|
+
expect( document ).to.have.property( 'bindings' );
|
|
19
|
+
expect( document.body ).to.have.property( 'bindings' );
|
|
20
|
+
} );
|
|
21
|
+
|
|
22
|
+
it ( `Bindings objects should be observable...`, async function() {
|
|
23
|
+
let idReceived = null;
|
|
24
|
+
Observer.observe( document.bindings, records => {
|
|
25
|
+
idReceived = records[ 0 ].key;
|
|
26
|
+
} );
|
|
27
|
+
document.bindings.someKey = 'someValue'; // new
|
|
28
|
+
expect( idReceived ).to.eq( 'someKey' );
|
|
29
|
+
// -------------
|
|
30
|
+
let changes = [];
|
|
31
|
+
Observer.observe( document.bindings, records => {
|
|
32
|
+
changes.push( ...records );
|
|
33
|
+
} );
|
|
34
|
+
document.bindings.someKey2 = 'someValue2'; // new
|
|
35
|
+
document.bind( { someKey: 'someValue'/* update */, someKey3: 'someValue3'/* new */ } );
|
|
36
|
+
expect( changes ).to.an( 'array' ).with.length( 4 ); // (1) sets: someKey2, (2) deletes: someKey2, (3) updates: someKey, (4) sets: someKey3;
|
|
37
|
+
} );
|
|
38
|
+
|
|
39
|
+
} );
|
|
40
|
+
|
|
41
|
+
} );
|
|
42
|
+
|
package/src/state-api/index.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @imports
|
|
4
|
-
*/
|
|
5
|
-
import wqDom from '@webqit/dom';
|
|
6
|
-
import Observer from '@webqit/observer';
|
|
7
|
-
import { _ } from '../util.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @init
|
|
11
|
-
*
|
|
12
|
-
* @param Object $params
|
|
13
|
-
*/
|
|
14
|
-
export default function init( $params = {} ) {
|
|
15
|
-
const window = this, dom = wqDom.call( window );
|
|
16
|
-
if ( !window.wq ) { window.wq = {}; }
|
|
17
|
-
window.wq.Observer = Observer;
|
|
18
|
-
const params = dom.meta( 'oohtml' ).copyWithDefaults( $params, {
|
|
19
|
-
api: { state: 'state', },
|
|
20
|
-
} );
|
|
21
|
-
exposeAPIs.call( this, params );
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { Observer }
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @Exports
|
|
28
|
-
*
|
|
29
|
-
* The internal state object
|
|
30
|
-
* within elements and the document object.
|
|
31
|
-
*/
|
|
32
|
-
function getStateObject( node ) {
|
|
33
|
-
if ( !_( node ).has( 'state' ) ) {
|
|
34
|
-
const stateObj = Object.create( null );
|
|
35
|
-
_( node ).set( 'state', stateObj );
|
|
36
|
-
}
|
|
37
|
-
return _( node ).get( 'state' );
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Exposes State with native APIs.
|
|
42
|
-
*
|
|
43
|
-
* @param Object params
|
|
44
|
-
*
|
|
45
|
-
* @return Void
|
|
46
|
-
*/
|
|
47
|
-
function exposeAPIs( params ) {
|
|
48
|
-
const window = this;
|
|
49
|
-
// Assertions
|
|
50
|
-
if ( params.api.state in window.document ) { throw new Error( `document already has a "${ params.api.state }" property!` ); }
|
|
51
|
-
if ( params.api.state in window.Element.prototype ) { throw new Error( `The "Element" class already has a "${ params.api.state }" property!` ); }
|
|
52
|
-
// Definitions
|
|
53
|
-
Object.defineProperty( window.document, params.api.state, { get: function() {
|
|
54
|
-
return Observer.proxy( getStateObject( window.document ) );
|
|
55
|
-
} });
|
|
56
|
-
Object.defineProperty( window.Element.prototype, params.api.state, { get: function() {
|
|
57
|
-
return Observer.proxy( getStateObject( this ) );
|
|
58
|
-
} } );
|
|
59
|
-
}
|
package/test/state-api.test.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @imports
|
|
4
|
-
*/
|
|
5
|
-
import { expect } from 'chai';
|
|
6
|
-
import { createDocument } from './index.js';
|
|
7
|
-
import Observer from '@webqit/observer';
|
|
8
|
-
|
|
9
|
-
describe(`State API`, function() {
|
|
10
|
-
|
|
11
|
-
describe( `Basic...`, function() {
|
|
12
|
-
|
|
13
|
-
const head = ``;
|
|
14
|
-
const body = ``;
|
|
15
|
-
const { document } = createDocument( head, body );
|
|
16
|
-
|
|
17
|
-
it ( `The document object and elements should expose a "state" property each...`, async function() {
|
|
18
|
-
expect( document ).to.have.property( 'state' );
|
|
19
|
-
expect( document.body ).to.have.property( 'state' );
|
|
20
|
-
} );
|
|
21
|
-
|
|
22
|
-
it ( `State objects should be observable...`, async function() {
|
|
23
|
-
let idReceived = null;
|
|
24
|
-
Observer.observe( document.state, records => {
|
|
25
|
-
idReceived = records[ 0 ].key;
|
|
26
|
-
} );
|
|
27
|
-
document.state.someKey = 'someValue';
|
|
28
|
-
expect( idReceived ).to.eq( 'someKey' );
|
|
29
|
-
} );
|
|
30
|
-
|
|
31
|
-
} );
|
|
32
|
-
|
|
33
|
-
} );
|
|
34
|
-
|
|
File without changes
|