Using microformats

  • Revision slug: Using_microformats
  • Revision title: Using microformats
  • Revision id: 57406
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment add link to Parsing microformats in JavaScript article

Revision Content

{{template.Fx_minversion_header(3)}} {{template.Draft()}} Microformats allow web sites to provide semantic data to the browser in order to make it possible to present summaries of the information on a page without having to know how to parse the document itself. Firefox 3 implements a global Microformats object that provides access to microformats. This object and its API make finding and reading microformats easy to do.

Loading the microformats API

The Microformats object is created using the new JavaScript script loader added to Firefox 3. To use the API, you need to first load the object:

Components.utils.import("resource://gre/modules/Microformats.js");

Once you've loaded the microformats API, you can manage microformats using the methods listed here; for information about parsing microformats, see Parsing microformats in JavaScript

Methods

add()

Adds a new microformat to the microformat module.

{{template.Note("If a microformat by the specified name already exists, it is replaced by the new one.")}}

add(name, definition);
Parameters
<tt>name</tt>
The name of the microformat to add to the microformat module.
<tt>definition</tt>
A JavaScript structure describing the microformat. See Describing microformats in JavaScript for details.

count()

Counts the number of microformats in a document that match specified criteria.

numMicroformats = Microformats.count(name, rootElement, recurseFrames);
Parameters
<tt>name</tt>
The name of the microformat to count.
<tt>rootElement</tt>
Optional. The DOM element at which to begin the search. Defaults to content.document (in other words, the entire document).
<tt>recurseFrames</tt>
Optional. If true, child frames are included in the search. The default is true.
Return value

An integer value indicating the number of microformats that match the specified criteria.

debug()

Returns a string that describes a microformat object.

{{template.Note("You can simply call <code>debug()</code> on a microformat object: <code>microformatObject.debug()</code> instead of using this method if you prefer.")}}

dumpString = dumpObject(microformatObject)
Parameters
<tt>microformatObject</tt>
The microformat object to dump.
Return value

A string that describes the contents of the specified microformat object.

get()

Returns an array of microformat objects corresponding to the microformats found that match specified criteria.

microformatsArray = Microformats.get(name, rootElement, recurseFrames, microformats);
Parameters
<tt>name</tt>
The name of the microformat to find.
<tt>rootElement</tt>
The DOM element at which to begin the search.
<tt>recurseFrames</tt>
Optional. If true, child frames are included in the search. The default is true.
<tt>microformats</tt>
Optional. If provided, this is an array of microformat objects to which the search results are added.
Return value

A new array of microformat objects matching the search criteria, or the array specified by <tt>microformats</tt> with the newly found microformat objects added.

getNamesFromNode()

Returns a space-delineated list of microformat names that correspond to the specified microformat node.

nameList = Microformats.getNamesFromNode(node);
Parameters
<tt>node</tt>
The node for which to retrieve a list of microformat names.
Return value

If the specified <tt>node</tt> is a microformat, the result is a space-delineated string listing all the microformat names that correspond to the node. If the node isn't a microformat, nothing is returned.

getParent()

Returns the parent node of the specified microformat or child of a microformat.

parentNode = Microformats.getParent(node);
Parameters
<tt>node</tt>
The node whose parent you wish to retrieve.
Return value

The parent of the specified node. Returns nothing if the specified <tt>node</tt> isn't a microformat or the child of a microformat.

isMicroformat()

Determines whether or not the specified DOM node is a microformat.

flag = Microformats.isMicroformat(node);
Parameters
<tt>node</tt>
The DOM node to check to see if it's a microformat.
Return value

true if the node is a microformat, otherwise false.

{{template.Note("This method does <i>not</i> return <code>true</code> if the node is a child of a microformat.")}}

Revision Source

<p>{{template.Fx_minversion_header(3)}}
{{template.Draft()}}
Microformats allow web sites to provide semantic data to the browser in order to make it possible to present summaries of the information on a page without having to know how to parse the document itself.  Firefox 3 implements a global <code>Microformats</code> object that provides access to microformats.  This object and its API make finding and reading microformats easy to do.
</p>
<h2 name="Loading_the_microformats_API">Loading the microformats API</h2>
<p>The <code>Microformats</code> object is created using the new JavaScript script loader added to Firefox 3.  To use the API, you need to first load the object:
</p>
<pre class="eval"><span class="plain">Components.utils.import("resource://gre/modules/Microformats.js");</span>
</pre>
<p>Once you've loaded the microformats API, you can manage microformats using the methods listed here; for information about parsing microformats, see <a href="en/Parsing_microformats_in_JavaScript">Parsing microformats in JavaScript</a>
</p>
<h2 name="Methods">Methods</h2>
<h3 name="add.28.29">add()</h3>
<p>Adds a new microformat to the microformat module.
</p><p>{{template.Note("If a microformat by the specified name already exists, it is replaced by the new one.")}}
</p>
<pre class="eval">add(name, definition);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl><dt><tt>name</tt>
</dt><dd>The name of the microformat to add to the microformat module.
</dd><dt><tt>definition</tt>
</dt><dd>A JavaScript structure describing the microformat.  See <a href="en/Describing_microformats_in_JavaScript">Describing microformats in JavaScript</a> for details.
</dd></dl>
<h3 name="count.28.29">count()</h3>
<p>Counts the number of microformats in a document that match specified criteria.
</p>
<pre class="eval">numMicroformats = Microformats.count(name, rootElement, recurseFrames);
</pre>
<h6 name="Parameters_2">Parameters</h6>
<dl><dt><tt>name</tt>
</dt><dd>The name of the microformat to count.
</dd><dt><tt>rootElement</tt>
</dt><dd>Optional.  The DOM element at which to begin the search.  Defaults to <code>content.document</code> (in other words, the entire document).
</dd><dt><tt>recurseFrames</tt>
</dt><dd>Optional.  If <code>true</code>, child frames are included in the search.  The default is <code>true</code>.
</dd></dl>
<h6 name="Return_value">Return value</h6>
<p>An integer value indicating the number of microformats that match the specified criteria.
</p>
<h3 name="debug.28.29">debug()</h3>
<p>Returns a string that describes a microformat object.
</p><p>{{template.Note("You can simply call &lt;code&gt;debug()&lt;/code&gt; on a microformat object: &lt;code&gt;microformatObject.debug()&lt;/code&gt; instead of using this method if you prefer.")}}
</p>
<pre class="eval">dumpString = dumpObject(microformatObject)
</pre>
<h6 name="Parameters_3">Parameters</h6>
<dl><dt><tt>microformatObject</tt>
</dt><dd>The microformat object to dump.
</dd></dl>
<h6 name="Return_value_2">Return value</h6>
<p>A string that describes the contents of the specified microformat object.
</p>
<h3 name="get.28.29">get()</h3>
<p>Returns an array of microformat objects corresponding to the microformats found that match specified criteria.
</p>
<pre class="eval">microformatsArray = Microformats.get(name, rootElement, recurseFrames, microformats);
</pre>
<h6 name="Parameters_4">Parameters</h6>
<dl><dt><tt>name</tt>
</dt><dd>The name of the microformat to find.
</dd><dt><tt>rootElement</tt>
</dt><dd>The DOM element at which to begin the search.
</dd><dt><tt>recurseFrames</tt>
</dt><dd>Optional.  If <code>true</code>, child frames are included in the search.  The default is <code>true</code>.
</dd><dt><tt>microformats</tt>
</dt><dd>Optional.  If provided, this is an array of microformat objects to which the search results are added.
</dd></dl>
<h6 name="Return_value_3">Return value</h6>
<p>A new array of microformat objects matching the search criteria, or the array specified by <tt>microformats</tt> with the newly found microformat objects added.
</p>
<h3 name="getNamesFromNode.28.29">getNamesFromNode()</h3>
<p>Returns a space-delineated list of microformat names that correspond to the specified microformat node.
</p>
<pre class="eval">nameList = Microformats.getNamesFromNode(node);
</pre>
<h6 name="Parameters_5">Parameters</h6>
<dl><dt><tt>node</tt>
</dt><dd>The node for which to retrieve a list of microformat names.
</dd></dl>
<h6 name="Return_value_4">Return value</h6>
<p>If the specified <tt>node</tt> is a microformat, the result is a space-delineated string listing all the microformat names that correspond to the node.  If the node isn't a microformat, nothing is returned.
</p>
<h3 name="getParent.28.29">getParent()</h3>
<p>Returns the parent node of the specified microformat or child of a microformat.
</p>
<pre class="eval">parentNode = Microformats.getParent(node);
</pre>
<h6 name="Parameters_6">Parameters</h6>
<dl><dt><tt>node</tt>
</dt><dd>The node whose parent you wish to retrieve.
</dd></dl>
<h6 name="Return_value_5">Return value</h6>
<p>The parent of the specified node.  Returns nothing if the specified <tt>node</tt> isn't a microformat or the child of a microformat.
</p>
<h3 name="isMicroformat.28.29">isMicroformat()</h3>
<p>Determines whether or not the specified DOM node is a microformat.
</p>
<pre class="eval">flag = Microformats.isMicroformat(node);
</pre>
<h6 name="Parameters_7">Parameters</h6>
<dl><dt><tt>node</tt>
</dt><dd>The DOM node to check to see if it's a microformat.
</dd></dl>
<h6 name="Return_value_6">Return value</h6>
<p><code>true</code> if the node is a microformat, otherwise <code>false</code>.
</p><p>{{template.Note("This method does &lt;i&gt;not&lt;/i&gt; return &lt;code&gt;true&lt;/code&gt; if the node is a child of a microformat.")}}
</p>
Revert to this revision