Skip to content

HTMLHelperProperty addLink() issue with type=canonical #83

@mfran

Description

@mfran

When using the addLink() method in the HTMLHelperProperty, passing in type=canonical, the output will always return something of the following variety:

<link rel="canonical" type="http://www.foo.com/bar" />

The spec on the tag suggests that the format should include a rel attribute but no type attribute, a la:

<link rel="canonical" href="http://www.foo.com/bar" />

I've created a fix for myself locally by updating the addLink() method with the code below. Unsure if this is the best approach but it works for me.

I'm on
Version: 1.9.0.2823 (Integrity)
Built-on: 2011-07-08 02:11:51
Using Railo 3.3.4.003 final

For reference:

http://en.wikipedia.org/wiki/Canonical_link_element
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394

<cffunction name="addLink" access="public" returntype="string" output="false"
hint="Adds code for a link tag for inline use or in the HTML head.">
<cfargument name="type" type="string" required="true"
hint="The type of link. Supports type shortcuts 'icon', 'rss', 'atom', 'html' and 'canonical', otherwise a complete MIME type is required." />
<cfargument name="href" type="string" required="true"
hint="The href path to a web accessible location of the link file." />
<cfargument name="attributes" type="any" required="false" default="#StructNew()#"
hint="A struct or string (param1=value1|param2=value2) of attributes." />
<cfargument name="outputType" type="string" required="false" default="head"
hint="Indicates to output type for the generated HTML code ('head', 'body', 'inline'). Link tags must be in the HTML head section according to W3C specification. Use the value of inline with caution." />

&lt;cfset var mimeTypeData = resolveMimeTypeAndGetData(arguments.type) /&gt;
&lt;cfset var code = "" /&gt;
&lt;cfset var key = "" /&gt;

&lt;cfif arguments.type EQ "icon"&gt;
    &lt;cfset code = '&lt;link href="' & computeAssetPath("img", arguments.href) & '"' /&gt;
&lt;cfelse&gt;
    &lt;cfset code = '&lt;link href="' & arguments.href & '"' /&gt;
&lt;/cfif&gt;

&lt;cfif arguments.type NEQ "canonical" &gt;
    &lt;cfset arguments.attributes = getUtils().parseAttributesIntoStruct(arguments.attributes) /&gt;
    &lt;cfset StructAppend(arguments.attributes, mimeTypeData, false) /&gt;
    &lt;cfloop collection="#arguments.attributes#" item="key"&gt;
        &lt;cfset code = code & ' ' & LCase(key) & '="' & getUtils().escapeHtml(arguments.attributes[key]) & '"' /&gt;
    &lt;/cfloop&gt;
&lt;cfelse&gt;
    &lt;cfset code = '&lt;link rel="canonical" href="' & arguments.href & '"' /&gt;
&lt;/cfif&gt;

&lt;cfset code = code & ' /&gt;' & Chr(13) /&gt;

&lt;cfif arguments.outputType EQ "inline"&gt;
    &lt;cfreturn code /&gt;
&lt;cfelse&gt;
    &lt;cfset appendToHtmlArea(arguments.outputType, code) /&gt;
    &lt;cfreturn "" /&gt;
&lt;/cfif&gt;

</cffunction>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions