My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
hCardToJSON  
JAVASCRIPT: Extract hCard elements from a "vcard" element.
Updated Feb 21, 2008 by john.fle...@gmail.com

Requires getElementByClassName found in JavascriptSnippets.

Source

var Microformats = function() {
	return {
		getNodeValue: function(node) {
			if (node && node.hasChildNodes()) {
				return node.firstChild.nodeValue;
			}
			return null;
		},
		getNodeAttribute: function(node, attr) {
			if (node && node.attributes) {
				return node.getAttribute(attr) || null;
			}
			return null;
		},
		hCardToJSON: function(card) {
			var vCard = (card instanceof Object) ? card : document.getElementById(card);
			var n = Microformats.getNodeValue(getElementsByClassName('n', vCard)[0]);
			var fn = Microformats.getNodeValue(getElementsByClassName('fn', vCard)[0]);
			var url = Microformats.getNodeAttribute(getElementsByClassName('url', vCard)[0], 'href');
			var org = Microformats.getNodeValue(getElementsByClassName('org', vCard)[0]);
			var photo = Microformats.getNodeAttribute(getElementsByClassName('photo', vCard)[0], 'src');
			var adr = getElementsByClassName('adr', vCard)[0] || null;
			var adrStreetAddress, adrLocality, adrRegion, adrPostalCode, geo, geoLatitude, geoLongitude;
			if (adr) {
				adrStreetAddress = Microformats.getNodeValue(getElementsByClassName('street-address', adr)[0]);
				adrLocality = Microformats.getNodeValue(getElementsByClassName('locality', adr)[0]);
				adrRegion = Microformats.getNodeValue(getElementsByClassName('region', adr)[0]);
				adrPostalCode = Microformats.getNodeValue(getElementsByClassName('postal-code', adr)[0]);
				geo = getElementsByClassName('geo', adr)[0] || null;
				if (geo) {
					geoLatitude = Microformats.getNodeValue(getElementsByClassName('latitude', geo)[0]);
					geoLongitude = Microformats.getNodeValue(getElementsByClassName('longitude', geo)[0]);
				}
			}
			return {"n":n, "fn":fn, "url":url, "photo":photo, "org":org, "adr":{"street-address":adrStreetAddress, "locality":adrLocality, "region":adrRegion, "postal-code":adrPostalCode, "geo":{"latitude":geoLatitude, "longitude":geoLongitude}}, "vCard":vCard}; // "vCard" is a reference back to the DOM element (not part of the hCard standard).
		}
	};
}();
Powered by Google Project Hosting