Jelajahi Sumber

ZF-9743 Add support for RDFa meta property in HeadMeta view helper; also clarifies documentation for RDFa Doctype view helper

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23610 44c647ce-9c0f-0410-b52a-842ac1e357ba
mjh_ca 15 tahun lalu
induk
melakukan
edecaa2577

+ 23 - 31
documentation/manual/en/module_specs/Zend_View-Helpers-Doctype.xml

@@ -101,8 +101,8 @@ if ($view->doctype()->isHtml5()) {
         <title>Choosing a Doctype to Use with the Open Graph Protocol</title>
 
         <para>
-            If you would like to implement the <ulink url="http://opengraphprotocol.org/">
-            Open Graph Protocol</ulink>, it is best to specify the XHTML1_RDFA doctype.
+            To implement the <ulink url="http://opengraphprotocol.org/">
+            Open Graph Protocol</ulink>, you may specify the XHTML1_RDFA doctype.
             This doctype allows a developer to use the <ulink url="http://www.w3.org/TR/xhtml-rdfa-primer/">
             Resource Description Framework</ulink> within an <acronym>XHTML</acronym> document.
         </para>
@@ -113,48 +113,40 @@ $doctypeHelper->doctype('XHTML1_RDFA');
 ]]></programlisting>
 
         <para>
-        It is not required to use the XHTML1_RDFA doctype to enable the Open Graph Protocol, but it is required to enable the namespaces inside the html tag:
+        The RDFa doctype allows XHTML to validate when the 'property'
+        meta tag attribute is used per the Open Graph Protocol spec.  Example
+        within a view script:
         </para>
 
         <programlisting language="html"><![CDATA[
-<?php echo $this->doctype() ?>
-
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
-]]></programlisting>
-
-        <para>
-            In the above example, we specifed two namespaces in the html tag. One for the Open Graph and another for <ulink url="http://developers.facebook.com/docs/opengraph">
-            Facebook Open Graph</ulink>.
-        </para>
-        
-        <para>
-            Here is how to specify a meta tag with a property:
-        </para>
-        
-        <programlisting language="html"><![CDATA[
-<meta property="og:type" content="musician" />
+<?php echo $this->doctype('XHTML1_RDFA'); ?>
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:og="http://opengraphprotocol.org/schema/">
+<head>
+   <meta property="og:type" content="musician" />
 ]]></programlisting>
 
         <para>
-            We set the property to og:type. The og references the Open Graph namespace we specified in the html tag. The content identifies the page as being about a musician.
+            In the previous example, we set the property to og:type. The og references
+            the Open Graph namespace we specified in the html tag.
+            The content identifies the page as being about a musician.  See
+            the <ulink url="http://opengraphprotocol.org/">Open Graph Protocol documentation</ulink>
+            for supported properties. The <link linkend="zend.view.helpers.initial.headmeta">HeadMeta helper</link>
+            may be used to programmatically set these Open Graph Protocol meta tags.
         </para>
 
         <para>
-            Here is who you check if the doctype includes <acronym>RFDa</acronym> in the document.
+            Here is how you check if the doctype is set to XHTML1_RDFA:
         </para>
 
         <programlisting language="php"><![CDATA[
 <?php echo $this->doctype() ?>
-
-<?php if ($view->doctype()->isRdfa()): ?>
-
-    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
-
-<?php else: ?>
-
-    <html xmlns="http://www.w3.org/1999/xhtml">
-
-<?php endif; ?>
+<html xmlns="http://www.w3.org/1999/xhtml" 
+      <?php if ($view->doctype()->isRdfa()): ?>
+      xmlns:og="http://opengraphprotocol.org/schema/"
+      xmlns:fb="http://www.facebook.com/2008/fbml"
+      <?php endif; ?>
+>
 ]]></programlisting>
         
     </example>

+ 59 - 1
documentation/manual/en/module_specs/Zend_View-Helpers-HeadMeta.xml

@@ -74,6 +74,37 @@
     </itemizedlist>
 
     <para>
+        The following methods are also supported with XHTML1_RDFA doctype
+        set with the <link linkend="zend.view.helpers.initial.doctype">Doctype helper</link>:
+    </para>
+
+    <itemizedlist>
+        <listitem>
+            <para>
+                <command>appendProperty($property, $content, $modifiers)</command>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <command>offsetSetProperty($index, $property, $content, $modifiers)</command>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <command>prependProperty($property, $content, $modifiers)</command>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <command>setProperty($property, $content, $modifiers)</command>
+            </para>
+        </listitem>
+    </itemizedlist>
+
+    <para>
         The <varname>$keyValue</varname> item is used to define a value for the 'name'
         or 'http-equiv' key; <varname>$content</varname> is the value for the
         'content' key, and <varname>$modifiers</varname> is an optional associative
@@ -86,7 +117,8 @@
             $keyValue, $keyType = 'name', $modifiers = array(), $placement =
             'APPEND')</command>. <varname>$keyValue</varname> is the content for the key
         specified in <varname>$keyType</varname>, which should be either 'name' or
-        'http-equiv'. <varname>$placement</varname> can be either 'SET' (overwrites
+        'http-equiv'. <varname>$keyType</varname> may also be specified as 'property' if the
+        doctype has been set to XHTML1_RDFA. <varname>$placement</varname> can be 'SET' (overwrites
         all previously stored values), 'APPEND' (added to end of stack), or
         'PREPEND' (added to top of stack).
     </para>
@@ -179,6 +211,32 @@ $this->headMeta()->appendHttpEquiv('Refresh',
 <?php echo $this->headMeta() ?>
 ]]></programlisting>
     </example>
+
+    <example id="zend.view.helpers.initial.headmeta.property">
+        <title>HeadMeta Usage with XHTML1_RDFA doctype</title>
+
+        <para>
+            Enabling the RDFa doctype with the <link linkend="zend.view.helpers.initial.doctype">Doctype helper</link>
+            enables the use of the 'property' attribute (in addition to the standard 'name' and 'http-equiv') with HeadMeta.
+            This is commonly used with the Facebook <ulink url="http://opengraphprotocol.org/">Open Graph Protocol</ulink>.
+        </para>
+
+        <para>
+            For instance, you may specify an open graph page title and type as follows:
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$this->doctype(Zend_View_Helper_Doctype::XHTML_RDFA);
+$this->headMeta()->setProperty('og:title', 'my article title');
+$this->headMeta()->setProperty('og:type', 'article');
+echo $this->headMeta();
+
+// output is:
+//   <meta property="og:title" content="my article title" />
+//   <meta property="og:type" content="article" />
+]]></programlisting>
+
+    </example>
 </sect3>
 <!--
 vim:se ts=4 sw=4 et:

+ 14 - 2
library/Zend/View/Helper/HeadMeta.php

@@ -39,7 +39,7 @@ class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_S
      * Types of attributes
      * @var array
      */
-    protected $_typeKeys     = array('name', 'http-equiv', 'charset');
+    protected $_typeKeys     = array('name', 'http-equiv', 'charset', 'property');
     protected $_requiredKeys = array('content');
     protected $_modifierKeys = array('lang', 'scheme');
 
@@ -98,6 +98,8 @@ class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_S
                 return 'name';
             case 'HttpEquiv':
                 return 'http-equiv';
+            case 'Property':
+                return 'property';
             default:
                 require_once 'Zend/View/Exception.php';
                 $e = new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type));
@@ -118,6 +120,10 @@ class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_S
      * - offsetGetHttpEquiv($index, $keyValue, $content, $modifers = array())
      * - prependHttpEquiv($keyValue, $content, $modifiers = array())
      * - setHttpEquiv($keyValue, $content, $modifiers = array())
+     * - appendProperty($keyValue, $content, $modifiers = array())
+     * - offsetGetProperty($index, $keyValue, $content, $modifiers = array())
+     * - prependProperty($keyValue, $content, $modifiers = array())
+     * - setProperty($keyValue, $content, $modifiers = array())
      *
      * @param  string $method
      * @param  array $args
@@ -125,7 +131,7 @@ class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_S
      */
     public function __call($method, $args)
     {
-        if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/', $method, $matches)) {
+        if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
             $action = $matches['action'];
             $type   = $this->_normalizeType($matches['type']);
             $argc   = count($args);
@@ -202,6 +208,12 @@ class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_S
             return false;
         }
 
+        // <meta property= ... /> is only supported with doctype RDFa
+        if (!$this->view->doctype()->isRdfa()
+            && $item->type === 'property') {
+            return false;
+        }
+
         return true;
     }
 

+ 55 - 0
tests/Zend/View/Helper/HeadMetaTest.php

@@ -363,6 +363,61 @@ class Zend_View_Helper_HeadMetaTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-9743
+     */
+    public function testPropertyIsSupportedWithRdfaDoctype()
+    {
+        $this->view->doctype('XHTML1_RDFA');
+        $this->helper->headMeta('foo', 'og:title', 'property');
+        $this->assertEquals('<meta property="og:title" content="foo" />',
+                            $this->helper->toString()
+                           );
+    }
+
+    /**
+     * @group ZF-9743
+     */
+    public function testPropertyIsNotSupportedByDefaultDoctype()
+    {
+        try {
+            $this->helper->headMeta('foo', 'og:title', 'property');
+            $this->fail('meta property attribute should not be supported on default doctype');
+        } catch (Zend_View_Exception $e) {
+            $this->assertContains('Invalid value passed', $e->getMessage());
+        }
+    }
+
+    /**
+     * @group ZF-9743
+     * @depends testPropertyIsSupportedWithRdfaDoctype
+     */
+    public function testOverloadingAppendPropertyAppendsMetaTagToStack()
+    {
+        $this->view->doctype('XHTML1_RDFA');
+        $this->_testOverloadAppend('property');
+    }
+
+    /**
+     * @group ZF-9743
+     * @depends testPropertyIsSupportedWithRdfaDoctype
+     */
+    public function testOverloadingPrependPropertyPrependsMetaTagToStack()
+    {
+        $this->view->doctype('XHTML1_RDFA');
+        $this->_testOverloadPrepend('property');
+    }
+
+    /**
+     * @group ZF-9743
+     * @depends testPropertyIsSupportedWithRdfaDoctype
+     */
+    public function testOverloadingSetPropertyOverwritesMetaTagStack()
+    {
+        $this->view->doctype('XHTML1_RDFA');
+        $this->_testOverloadSet('property');
+    }
+
+    /**
      * @issue ZF-2663
      */
     public function testSetNameDoesntClobber()