Describing microformats in JavaScript

Microformats are described in JavaScript by using a standardized structure format that has several standard members that describe the object.

Microformat definition format

The microformat definition must contain the following entries:

mfVersion
Specifies the version number of the microformat API to which the definition was written. For Firefox 3, this should be set to 0.8.
mfObject
The JavaScript object that implements the microformat.
className
A string specifying the name of the microformat's class as referenced in HTML (class="className").
required
An array indicating the names of any properties that must be specified. This may be left out if all properties are optional.
properties
A structure describing the properties of the microformat.

Property specifications

Each property in the properties structure is specified by its name, and may include additional attributes if the property so requires. Standard attributes are:

plural
A boolean value indicating that, if true indicates that the property can have multiple values. If a property is plural, it is returned as an array.
virtual
A boolean value indicating whether or not the property is virtual. If it's virtual, the virtualGetter() method will be called to attempt to create the property if it doesn't exist.
virtualGetter
A function that is called to get a virtual property's value. This is only used if virtual is true.
value
The property's default value.
subproperties
It's possible for a property to itself include more properties; to do so, include them in a subproperties structure within the property.
dataType
The type of data contained by the property. Possible values are:
dateTime
An ISO date
anyURI
A URI
email
An email address
tel
A telephone number
HTML
HTML including tags
float
A floating-point number
microformat
A microformat
microformat_property
A specific property within the microformat specified by microformat.
custom
Custom data

A simple example

The hCard microformat for defining contact information makes use of the adr microformat to define an address. The adr microformat is defined as follows:

var adr_definition = {
  mfVersion: 0.8,
  mfObject: adr,
  className: "adr",
  properties: {
    "type" : {
      plural: true,
      types: ["work", "home", "pref", "postal", "dom", "intl", "parcel"]
    },
    "post-office-box" : {
    },
    "street-address" : {
      plural: true
    },
    "extended-address" : {
    },
    "locality" : {
    },
    "region" : {
    },
    "postal-code" : {
    },
    "country-name" : {
    }
  }
};

The properties are quite simple here. The type property indicates the type of address represented by the object (work, home, and so forth). Since the plural property is true, multiple types can be specified. This allows an address to be marked as being, for example, a work address for receiving parcels.

The street-address property is plural as well. This allows multiple lines of address information to be contained in the street-address array.

The microformat is registered with the microformat API by calling Microformats.add(), like this:

Microformats.add("adr", adr_definition);

Note: To be clear: since the adr microformat is included by default in Firefox 3 and later, you don't need to add it yourself if you wish to make use of it.

See also

Using microformats, Parsing microformats in JavaScript