Parsing microformats in JavaScript

Our volunteers haven't translated this article into 正體中文 (繁體) yet. Join us and help get the job done!

Firefox 3 introduces a new API for managing and parsing microformats. This article examines the generic microformat parsing API, which handles the heavy lifting of pulling data out of a microformat. This API is primarily intended to be used when implementing new microformats.

Methods

dateTimeGetter()

Specifically retrieves a date from a microformat node. After getting the text, it is normalized into an ISO 8601 date.

normalizedDate = Microformats.parser.dateTimeGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

A string containing the normalized date.

defaultGetter()

Uses the microformat patterns to decide what the correct text for a given microformat property is. This includes looking at thing such as abbr, img and alt, area and alt, and value excerpting.

propertyValue = Microformats.parser.defaultGetter(propnode, parentnode, datatype);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
datatype
"HTML" if the search should be done using innerHTML(), or "text" to use innerText(). The default is "text".
Return value

A string containing the property's value.

emailGetter()

Specifically retrieves an email address from a microformat node. This removes the subject if one is specified, as well as the mailto: prefix.

email = Microformats.parser.emailGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

A string containing the email address.

HTMLGetter()

Retrieves all the HTML from a particular DOM node.

html = Microformats.parser.HTMLGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

An object containing a function you can call to get the string and the HTML.

Note: This doesn't return the HTML as a string, but an object with a few functions you can call to retrieve the HTML and do other tasks.

The functions you can call on the returned object are:

string = html.toString();

Returns a string using innerText().

string = html.toHTML();

Returns the node's HTML using innerHTML().

string = html.replace(a, b);

Returns a string in which all occurrences of a in the HTML are replaced with b.

string = html.match(a);

Performs the specified matching operation on the HTML and returns the result.

iso8601FromDate

Converts a JavaScript date object into an ISO 8601 formatted date.

isoDate = Microformats.parser.iso8601FromDate(date, punctuation)
Parameters
date
The JavaScript Date object to convert.
punctuation
true if the date should have "-" and "/" in it.
Return value

A string containing the ISO 8601 formatted date.

textGetter()

Retrieves all the text from a particular DOM node, including all tags. This calls defaultGetter() internally.

text = Microformats.parser.textGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

A string containing all the text from the specified microformat node, including the tags.

telGetter()

Specifically retrieves a telephone number from a microformat node. This handles the fact that telephone numbers use "value" as the name of one of their subproperties, but "value" is also used for value excerpting.

tel = Microformats.parser.telGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

A string containing the telephone number.

uriGetter()

Specifically retrieves a URI from a microformat node. This is done by looking at an href, img, object, or area to get the fully-qualified URI.

uri = Microformats.parser.uriGetter(propnode, parentnode);
Parameters
propnode
The DOM node to check.
parentnode
The property's parent node. If it is a subproperty, this is the parent property node. Otherwise this is the microformat node.
Return value

A string containing the fully-qualified URI.

See also

Document Tags and Contributors

Contributors to this page: Sheppy, Mgjbot, ethertank, Kohei
最近更新: ethertank,