Using microformats

  • Revision slug: Using_microformats
  • Revision title: Using microformats
  • Revision id: 57401
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment /* count() */ remove parameter that's not actually there

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");

Methods

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.

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>
<h3 name="Loading_the_Microformats_API">Loading the Microformats API</h3>
<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>
<h3 name="Methods">Methods</h3>
<h4 name="count.28.29">count()</h4>
<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">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>
<h4 name="get.28.29">get()</h4>
<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_2">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_2">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>
<h4 name="getNamesFromNode.28.29">getNamesFromNode()</h4>
<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_3">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_3">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>
<h4 name="getParent.28.29">getParent()</h4>
<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_4">Parameters</h6>
<dl><dt><tt>node</tt>
</dt><dd>The node whose parent you wish to retrieve.
</dd></dl>
<h6 name="Return_value_4">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>
<h4 name="isMicroformat.28.29">isMicroformat()</h4>
<p>Determines whether or not the specified DOM node is a microformat.
</p>
<pre class="eval">flag = Microformats.isMicroformat(node);
</pre>
<h6 name="Parameters_5">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_5">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