summaryrefslogtreecommitdiffstats
path: root/toolkit/components/microformats/test/lib/parser-implied.js
blob: 7f67a2ca1ca0e2a48288102d6e3af0db0b71d383 (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*!
	Parser implied
	All the functions that deal with microformats implied rules

	Copyright (C) 2010 - 2015 Glenn Jones. All Rights Reserved.
	MIT License: https://raw.github.com/glennjones/microformat-shiv/master/license.txt
	Dependencies  dates.js, domutils.js, html.js, isodate,js, text.js, utilities.js, url.js
*/

var Modules = (function (modules) {

	// check parser module is loaded
	if(modules.Parser){

		/**
		 * applies "implied rules" microformat output structure i.e. feed-title, name, photo, url and date
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf (microformat output structure)
		 * @param  {Object} parentClasses (classes structure)
		 * @param  {Boolean} impliedPropertiesByVersion
		 * @return {Object}
		 */
		 modules.Parser.prototype.impliedRules = function(node, uf, parentClasses) {
			var typeVersion = (uf.typeVersion)? uf.typeVersion: 'v2';

			// TEMP: override to allow v1 implied properties while spec changes
			if(this.options.impliedPropertiesByVersion === false){
				typeVersion = 'v2';
			}

			if(node && uf && uf.properties) {
				uf = this.impliedBackwardComp( node, uf, parentClasses );
				if(typeVersion === 'v2'){
					uf = this.impliedhFeedTitle( uf );
					uf = this.impliedName( node, uf );
					uf = this.impliedPhoto( node, uf );
					uf = this.impliedUrl( node, uf );
				}
				uf = this.impliedValue( node, uf, parentClasses );
				uf = this.impliedDate( uf );

				// TEMP: flagged while spec changes are put forward
				if(this.options.parseLatLonGeo === true){
					uf = this.impliedGeo( uf );
				}
			}

			return uf;
		};


		/**
		 * apply implied name rule
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedName = function(node, uf) {
			// implied name rule
			/*
				img.h-x[alt]										<img class="h-card" src="glenn.htm" alt="Glenn Jones"></a>
				area.h-x[alt] 										<area class="h-card" href="glenn.htm" alt="Glenn Jones"></area>
				abbr.h-x[title]										<abbr class="h-card" title="Glenn Jones"GJ</abbr>

				.h-x>img:only-child[alt]:not[.h-*]					<div class="h-card"><a src="glenn.htm" alt="Glenn Jones"></a></div>
				.h-x>area:only-child[alt]:not[.h-*] 				<div class="h-card"><area href="glenn.htm" alt="Glenn Jones"></area></div>
				.h-x>abbr:only-child[title] 						<div class="h-card"><abbr title="Glenn Jones">GJ</abbr></div>

				.h-x>:only-child>img:only-child[alt]:not[.h-*] 		<div class="h-card"><span><img src="jane.html" alt="Jane Doe"/></span></div>
				.h-x>:only-child>area:only-child[alt]:not[.h-*] 	<div class="h-card"><span><area href="jane.html" alt="Jane Doe"></area></span></div>
				.h-x>:only-child>abbr:only-child[title]				<div class="h-card"><span><abbr title="Jane Doe">JD</abbr></span></div>
			*/
			var name,
				value;

			if(!uf.properties.name) {
				value = this.getImpliedProperty(node, ['img', 'area', 'abbr'], this.getNameAttr);
				var textFormat = this.options.textFormat;
				// if no value for tags/properties use text
				if(!value) {
					name = [modules.text.parse(this.document, node, textFormat)];
				}else{
					name = [modules.text.parseText(this.document, value, textFormat)];
				}
				if(name && name[0] !== ''){
					uf.properties.name = name;
				}
			}

			return uf;
		};


		/**
		 * apply implied photo rule
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedPhoto = function(node, uf) {
			// implied photo rule
			/*
				img.h-x[src] 												<img class="h-card" alt="Jane Doe" src="jane.jpeg"/>
				object.h-x[data] 											<object class="h-card" data="jane.jpeg"/>Jane Doe</object>
				.h-x>img[src]:only-of-type:not[.h-*]						<div class="h-card"><img alt="Jane Doe" src="jane.jpeg"/></div>
				.h-x>object[data]:only-of-type:not[.h-*] 					<div class="h-card"><object data="jane.jpeg"/>Jane Doe</object></div>
				.h-x>:only-child>img[src]:only-of-type:not[.h-*] 			<div class="h-card"><span><img alt="Jane Doe" src="jane.jpeg"/></span></div>
				.h-x>:only-child>object[data]:only-of-type:not[.h-*] 		<div class="h-card"><span><object data="jane.jpeg"/>Jane Doe</object></span></div>
			*/
			var value;
			if(!uf.properties.photo) {
				value = this.getImpliedProperty(node, ['img', 'object'], this.getPhotoAttr);
				if(value) {
					// relative to absolute URL
					if(value && value !== '' && this.options.baseUrl !== '' && value.indexOf('://') === -1) {
						value = modules.url.resolve(value, this.options.baseUrl);
					}
					uf.properties.photo = [modules.utils.trim(value)];
				}
			}
			return uf;
		};


		/**
		 * apply implied URL rule
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedUrl = function(node, uf) {
			// implied URL rule
			/*
				a.h-x[href]  							<a class="h-card" href="glenn.html">Glenn</a>
				area.h-x[href]  						<area class="h-card" href="glenn.html">Glenn</area>
				.h-x>a[href]:only-of-type:not[.h-*]  	<div class="h-card" ><a href="glenn.html">Glenn</a><p>...</p></div>
				.h-x>area[href]:only-of-type:not[.h-*]  <div class="h-card" ><area href="glenn.html">Glenn</area><p>...</p></div>
			*/
			var value;
			if(!uf.properties.url) {
				value = this.getImpliedProperty(node, ['a', 'area'], this.getURLAttr);
				if(value) {
					// relative to absolute URL
					if(value && value !== '' && this.options.baseUrl !== '' && value.indexOf('://') === -1) {
						value = modules.url.resolve(value, this.options.baseUrl);
					}
					uf.properties.url = [modules.utils.trim(value)];
				}
			}
			return uf;
		};


		/**
		 * apply implied date rule - if there is a time only property try to concat it with any date property
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedDate = function(uf) {
			// implied date rule
			// http://microformats.org/wiki/value-class-pattern#microformats2_parsers
			// http://microformats.org/wiki/microformats2-parsing-issues#implied_date_for_dt_properties_both_mf2_and_backcompat
			var newDate;
			if(uf.times.length > 0 && uf.dates.length > 0) {
				newDate = modules.dates.dateTimeUnion(uf.dates[0][1], uf.times[0][1], this.options.dateFormat);
				uf.properties[this.removePropPrefix(uf.times[0][0])][0] = newDate.toString(this.options.dateFormat);
			}
			// clean-up object
			delete uf.times;
			delete uf.dates;
			return uf;
		};


		/**
		 * get an implied property value from pre-defined tag/attriubte combinations
		 *
		 * @param  {DOM Node} node
		 * @param  {String} tagList (Array of tags from which an implied value can be pulled)
		 * @param  {String} getAttrFunction (Function which can extract implied value)
		 * @return {String || null}
		 */
		modules.Parser.prototype.getImpliedProperty = function(node, tagList, getAttrFunction) {
			// i.e. img.h-card
			var value = getAttrFunction(node),
				descendant,
				child;

			if(!value) {
				// i.e. .h-card>img:only-of-type:not(.h-card)
				descendant = modules.domUtils.getSingleDescendantOfType( node, tagList);
				if(descendant && this.hasHClass(descendant) === false){
					value = getAttrFunction(descendant);
				}
				if(node.children.length > 0 ){
					// i.e.  .h-card>:only-child>img:only-of-type:not(.h-card)
					child = modules.domUtils.getSingleDescendant(node);
					if(child && this.hasHClass(child) === false){
						descendant = modules.domUtils.getSingleDescendantOfType(child, tagList);
						if(descendant && this.hasHClass(descendant) === false){
							value = getAttrFunction(descendant);
						}
					}
				}
			}

			return value;
		};


		/**
		 * get an implied name value from a node
		 *
		 * @param  {DOM Node} node
		 * @return {String || null}
		 */
		modules.Parser.prototype.getNameAttr = function(node) {
			var value = modules.domUtils.getAttrValFromTagList(node, ['img','area'], 'alt');
			if(!value) {
				value = modules.domUtils.getAttrValFromTagList(node, ['abbr'], 'title');
			}
			return value;
		};


		/**
		 * get an implied photo value from a node
		 *
		 * @param  {DOM Node} node
		 * @return {String || null}
		 */
		modules.Parser.prototype.getPhotoAttr = function(node) {
			var value = modules.domUtils.getAttrValFromTagList(node, ['img'], 'src');
			if(!value && modules.domUtils.hasAttributeValue(node, 'class', 'include') === false) {
				value = modules.domUtils.getAttrValFromTagList(node, ['object'], 'data');
			}
			return value;
		};


		/**
		 * get an implied photo value from a node
		 *
		 * @param  {DOM Node} node
		 * @return {String || null}
		 */
		modules.Parser.prototype.getURLAttr = function(node) {
			var value = null;
			if(modules.domUtils.hasAttributeValue(node, 'class', 'include') === false){

				value = modules.domUtils.getAttrValFromTagList(node, ['a'], 'href');
				if(!value) {
					value = modules.domUtils.getAttrValFromTagList(node, ['area'], 'href');
				}

			}
			return value;
		};


		/**
		 *
		 *
		 * @param  {DOM Node} node
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedValue = function(node, uf, parentClasses){

			// intersection of implied name and implied value rules
			if(uf.properties.name) {
				if(uf.value && parentClasses.root.length > 0 && parentClasses.properties.length === 1){
					uf = this.getAltValue(uf, parentClasses.properties[0][0], 'p-name', uf.properties.name[0]);
				}
			}

			// intersection of implied URL and implied value rules
			if(uf.properties.url) {
				if(parentClasses && parentClasses.root.length === 1 && parentClasses.properties.length === 1){
					uf = this.getAltValue(uf, parentClasses.properties[0][0], 'u-url', uf.properties.url[0]);
				}
			}

			// apply alt value
			if(uf.altValue !== null){
				uf.value = uf.altValue.value;
			}
			delete uf.altValue;


			return uf;
		};


		/**
		 * get alt value based on rules about parent property prefix
		 *
		 * @param  {Object} uf
		 * @param  {String} parentPropertyName
		 * @param  {String} propertyName
		 * @param  {String} value
		 * @return {Object}
		 */
		modules.Parser.prototype.getAltValue = function(uf, parentPropertyName, propertyName, value){
			if(uf.value && !uf.altValue){
				// first p-name of the h-* child
				if(modules.utils.startWith(parentPropertyName,'p-') && propertyName === 'p-name'){
					uf.altValue = {name: propertyName, value: value};
				}
				// if it's an e-* property element
				if(modules.utils.startWith(parentPropertyName,'e-') && modules.utils.startWith(propertyName,'e-')){
					uf.altValue = {name: propertyName, value: value};
				}
				// if it's an u-* property element
				if(modules.utils.startWith(parentPropertyName,'u-') && propertyName === 'u-url'){
					uf.altValue = {name: propertyName, value: value};
				}
			}
			return uf;
		};


		/**
		 * if a h-feed does not have a title use the title tag of a page
		 *
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedhFeedTitle = function( uf ){
			if(uf.type && uf.type.indexOf('h-feed') > -1){
				// has no name property
				if(uf.properties.name === undefined || uf.properties.name[0] === '' ){
					// use the text from the title tag
					var title = modules.domUtils.querySelector(this.document, 'title');
					if(title){
						uf.properties.name = [modules.domUtils.textContent(title)];
					}
				}
			}
			return uf;
		};



	    /**
		 * implied Geo from pattern <abbr class="p-geo" title="37.386013;-122.082932">
		 *
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedGeo = function( uf ){
			var geoPair,
				parts,
				longitude,
				latitude,
				valid = true;

			if(uf.type && uf.type.indexOf('h-geo') > -1){

				// has no latitude or longitude property
				if(uf.properties.latitude === undefined || uf.properties.longitude === undefined ){

					geoPair = (uf.properties.name)? uf.properties.name[0] : null;
					geoPair = (!geoPair && uf.properties.value)? uf.properties.value : geoPair;

					if(geoPair){
						// allow for the use of a ';' as in microformats and also ',' as in Geo URL
						geoPair = geoPair.replace(';',',');

						// has sep char
						if(geoPair.indexOf(',') > -1 ){
							parts = geoPair.split(',');

							// only correct if we have two or more parts
							if(parts.length > 1){

								// latitude no value outside the range -90 or 90
								latitude = parseFloat( parts[0] );
								if(modules.utils.isNumber(latitude) && latitude > 90 || latitude < -90){
									valid = false;
								}

								// longitude no value outside the range -180 to 180
								longitude = parseFloat( parts[1] );
								if(modules.utils.isNumber(longitude) && longitude > 180 || longitude < -180){
									valid = false;
								}

								if(valid){
									uf.properties.latitude = [latitude];
									uf.properties.longitude  = [longitude];
								}
							}

						}
					}
				}
			}
			return uf;
		};


		/**
		 * if a backwards compat built structure has no properties add name through this.impliedName
		 *
		 * @param  {Object} uf
		 * @return {Object}
		 */
		modules.Parser.prototype.impliedBackwardComp = function(node, uf, parentClasses){

			// look for pattern in parent classes like "p-geo h-geo"
			// these are structures built from backwards compat parsing of geo
			if(parentClasses.root.length === 1 && parentClasses.properties.length === 1) {
				if(parentClasses.root[0].replace('h-','') === this.removePropPrefix(parentClasses.properties[0][0])) {

					// if microformat has no properties apply the impliedName rule to get value from containing node
					// this will get value from html such as <abbr class="geo" title="30.267991;-97.739568">Brighton</abbr>
					if( modules.utils.hasProperties(uf.properties) === false ){
						uf = this.impliedName( node, uf );
					}
				}
			}

			return uf;
		};



	}

	return modules;

} (Modules || {}));