1
1
/*
2
- microformat-node - v1.0 .0
3
- Built: 2015-09-08 09 :09 -
2
+ microformat-node - v1.1 .0
3
+ Built: 2015-09-14 03 :09 -
4
4
Copyright (c) 2015 Glenn Jones
5
5
Licensed MIT
6
6
*/
@@ -11,8 +11,8 @@ var cheerio = require('cheerio'),
11
11
Promise = require ( 'bluebird' ) ;
12
12
13
13
var Modules = ( function ( modules ) {
14
- modules . version = '1.0 .0' ;
15
- modules . livingStandard = '2015-08-20T13:50:36Z ' ;
14
+ modules . version = '1.1 .0' ;
15
+ modules . livingStandard = '2015-09-09T14:47:13Z ' ;
16
16
17
17
/**
18
18
* constructor
@@ -39,9 +39,9 @@ var Modules = (function (modules) {
39
39
'baseUrl' : '' ,
40
40
'filters' : [ ] ,
41
41
'textFormat' : 'whitespacetrimmed' ,
42
- 'dateFormat' : 'auto' ,
43
- 'overlappingVersions' : true ,
44
- 'impliedPropertiesByVersion' : false ,
42
+ 'dateFormat' : 'auto' , // html5 for testing
43
+ 'overlappingVersions' : false ,
44
+ 'impliedPropertiesByVersion' : true ,
45
45
'parseLatLonGeo' : false
46
46
} ;
47
47
this . rootID = 0 ;
@@ -131,6 +131,139 @@ var Modules = (function (modules) {
131
131
} ,
132
132
133
133
134
+ /**
135
+ * get the count of microformats
136
+ *
137
+ * @param {DOM Node } rootNode
138
+ * @return {Int }
139
+ */
140
+ count : function ( options ) {
141
+ var out = { } ,
142
+ items ,
143
+ classItems ,
144
+ x ,
145
+ i ;
146
+
147
+ this . init ( ) ;
148
+ options = ( options ) ? options : { } ;
149
+ this . getDOMContext ( options ) ;
150
+
151
+ // if we do not have any context create error
152
+ if ( ! this . rootNode || ! this . document ) {
153
+ return { 'errors' : [ this . noContentErr ] } ;
154
+ } else {
155
+
156
+ items = this . findRootNodes ( this . rootNode , true ) ;
157
+ i = items . length ;
158
+ while ( i -- ) {
159
+ classItems = modules . domUtils . getAttributeList ( items [ i ] , 'class' ) ;
160
+ x = classItems . length ;
161
+ while ( x -- ) {
162
+ // find v2 names
163
+ if ( modules . utils . startWith ( classItems [ x ] , 'h-' ) ) {
164
+ this . appendCount ( classItems [ x ] , 1 , out ) ;
165
+ }
166
+ // find v1 names
167
+ for ( var key in modules . maps ) {
168
+ // has v1 root but not also a v2 root so we dont double count
169
+ if ( modules . maps [ key ] . root === classItems [ x ] && classItems . indexOf ( key ) === - 1 ) {
170
+ this . appendCount ( key , 1 , out ) ;
171
+ }
172
+ }
173
+ }
174
+ }
175
+ var relCount = this . countRels ( this . rootNode ) ;
176
+ if ( relCount > 0 ) {
177
+ out . rels = relCount ;
178
+ }
179
+
180
+ return out ;
181
+ }
182
+ } ,
183
+
184
+
185
+ /**
186
+ * does a node have a class that marks it as a microformats root
187
+ *
188
+ * @param {DOM Node } node
189
+ * @param {Objecte } options
190
+ * @return {Boolean }
191
+ */
192
+ isMicroformat : function ( node , options ) {
193
+ var classes ,
194
+ i ;
195
+
196
+ if ( ! node ) {
197
+ return false ;
198
+ }
199
+
200
+ // if documemt get topmost node
201
+ node = modules . domUtils . getTopMostNode ( node ) ;
202
+
203
+ // look for h-* microformats
204
+ classes = this . getUfClassNames ( node ) ;
205
+ if ( options && options . filters && modules . utils . isArray ( options . filters ) ) {
206
+ i = options . filters . length ;
207
+ while ( i -- ) {
208
+ if ( classes . root . indexOf ( options . filters [ i ] ) > - 1 ) {
209
+ return true ;
210
+ }
211
+ }
212
+ return false ;
213
+ } else {
214
+ return ( classes . root . length > 0 ) ;
215
+ }
216
+ } ,
217
+
218
+
219
+ /**
220
+ * does a node or its children have microformats
221
+ *
222
+ * @param {DOM Node } node
223
+ * @param {Objecte } options
224
+ * @return {Boolean }
225
+ */
226
+ hasMicroformats : function ( node , options ) {
227
+ var items ,
228
+ i ;
229
+
230
+ if ( ! node ) {
231
+ return false ;
232
+ }
233
+
234
+ // if browser based documemt get topmost node
235
+ node = modules . domUtils . getTopMostNode ( node ) ;
236
+
237
+ // returns all microformats roots
238
+ items = this . findRootNodes ( node , true ) ;
239
+ if ( options && options . filters && modules . utils . isArray ( options . filters ) ) {
240
+ i = items . length ;
241
+ while ( i -- ) {
242
+ if ( this . isMicroformat ( items [ i ] , options ) ) {
243
+ return true ;
244
+ }
245
+ }
246
+ return false ;
247
+ } else {
248
+ return ( items . length > 0 ) ;
249
+ }
250
+ } ,
251
+
252
+
253
+ /**
254
+ * add a new V1 mapping object to parser
255
+ *
256
+ * @param {Array } maps
257
+ */
258
+ add : function ( maps ) {
259
+ maps . forEach ( function ( map ) {
260
+ if ( map && map . root && map . name && map . properties ) {
261
+ modules . maps [ map . name ] = JSON . parse ( JSON . stringify ( map ) ) ;
262
+ }
263
+ } ) ;
264
+ } ,
265
+
266
+
134
267
/**
135
268
* internal parse to get parent microformat by walking up the tree
136
269
*
@@ -253,20 +386,6 @@ var Modules = (function (modules) {
253
386
} ,
254
387
255
388
256
- /**
257
- * add a new V1 mapping object to parser
258
- *
259
- * @param {Array } maps
260
- */
261
- add : function ( maps ) {
262
- maps . forEach ( function ( map ) {
263
- if ( map && map . root && map . name && map . properties ) {
264
- modules . maps [ map . name ] = JSON . parse ( JSON . stringify ( map ) ) ;
265
- }
266
- } ) ;
267
- } ,
268
-
269
-
270
389
// find uf's of a given type and return a dom and node structure of just that type of ufs
271
390
findFilterNodes : function ( rootNode , filters ) {
272
391
var newRootNode = modules . domUtils . createNode ( 'div' ) ,
@@ -303,57 +422,6 @@ var Modules = (function (modules) {
303
422
} ,
304
423
305
424
306
- /**
307
- * get the count of microformats
308
- *
309
- * @param {DOM Node } rootNode
310
- * @return {Int }
311
- */
312
- count : function ( options ) {
313
- var out = { } ,
314
- items ,
315
- classItems ,
316
- x ,
317
- i ;
318
-
319
- this . init ( ) ;
320
- options = ( options ) ? options : { } ;
321
- this . getDOMContext ( options ) ;
322
-
323
- // if we do not have any context create error
324
- if ( ! this . rootNode || ! this . document ) {
325
- return { 'errors' : [ this . noContentErr ] } ;
326
- } else {
327
-
328
- items = this . findRootNodes ( this . rootNode , true ) ;
329
- i = items . length ;
330
- while ( i -- ) {
331
- classItems = modules . domUtils . getAttributeList ( items [ i ] , 'class' ) ;
332
- x = classItems . length ;
333
- while ( x -- ) {
334
- // find v2 names
335
- if ( modules . utils . startWith ( classItems [ x ] , 'h-' ) ) {
336
- this . appendCount ( classItems [ x ] , 1 , out ) ;
337
- }
338
- // find v1 names
339
- for ( var key in modules . maps ) {
340
- // has v1 root but not also a v2 root so we dont double count
341
- if ( modules . maps [ key ] . root === classItems [ x ] && classItems . indexOf ( key ) === - 1 ) {
342
- this . appendCount ( key , 1 , out ) ;
343
- }
344
- }
345
- }
346
- }
347
- var relCount = this . countRels ( this . rootNode ) ;
348
- if ( relCount > 0 ) {
349
- out . rels = relCount ;
350
- }
351
-
352
- return out ;
353
- }
354
- } ,
355
-
356
-
357
425
/**
358
426
* appends data to output object for count
359
427
*
@@ -368,84 +436,8 @@ var Modules = (function (modules) {
368
436
out [ name ] = count ;
369
437
}
370
438
} ,
371
-
372
-
373
-
374
- /**
375
- * does a node have a class that marks it as a microformats root
376
- *
377
- * @param {DOM Node } node
378
- * @param {Objecte } options
379
- * @return {Boolean }
380
- */
381
- isMicroformat : function ( node , options ) {
382
- var classes ,
383
- i ;
384
-
385
- if ( ! node ) {
386
- return false ;
387
- }
388
-
389
- // if documemt get topmost node
390
- node = modules . domUtils . getTopMostNode ( node ) ;
391
- //if(node.nodeType && node.nodeType === 9){
392
- // node = modules.domUtils.querySelector(node, 'html');
393
- //}
394
-
395
- // look for h-* microformats
396
- classes = this . getUfClassNames ( node ) ;
397
- if ( options && options . filters && modules . utils . isArray ( options . filters ) ) {
398
- i = options . filters . length ;
399
- while ( i -- ) {
400
- if ( classes . root . indexOf ( options . filters [ i ] ) > - 1 ) {
401
- return true ;
402
- }
403
- }
404
- return false ;
405
- } else {
406
- return ( classes . root . length > 0 ) ;
407
- }
408
- } ,
409
-
410
-
411
- /**
412
- * does a node or its children have microformats
413
- *
414
- * @param {DOM Node } node
415
- * @param {Objecte } options
416
- * @return {Boolean }
417
- */
418
- hasMicroformats : function ( node , options ) {
419
- var items ,
420
- i ;
421
-
422
- if ( ! node ) {
423
- return false ;
424
- }
425
439
426
440
427
- // if browser based documemt get topmost node
428
- node = modules . domUtils . getTopMostNode ( node ) ;
429
- //if(node.nodeType && node.nodeType === 9){
430
- // node = modules.domUtils.querySelector(node, 'html');
431
- //}
432
-
433
- // returns all microformats roots
434
- items = this . findRootNodes ( node , true ) ;
435
- if ( options && options . filters && modules . utils . isArray ( options . filters ) ) {
436
- i = items . length ;
437
- while ( i -- ) {
438
- if ( this . isMicroformat ( items [ i ] , options ) ) {
439
- return true ;
440
- }
441
- }
442
- return false ;
443
- } else {
444
- return ( items . length > 0 ) ;
445
- }
446
- } ,
447
-
448
-
449
441
/**
450
442
* is the microformats type in the filter list
451
443
*
0 commit comments