Creating anchor links in rich text fields with SDL Tridion 2011 SP1

ぃ、小莉子 提交于 2019-12-29 07:47:14

问题


I am trying to use the anchor button in a RTF field of a Component, and getting unexpected behavior. Using the Chrome Browser from the design view, I highlight/select the heading (i.e. <h2>My Heading</h2>) I want to use as an anchor, and press the anchor button and enter the anchor name (i.e. my_place).

This results in the following code being displayed in my source tab:

<a name="my_place" id="myplace"/><h2>My Heading</h2>

This causes render problems when displaying the HTML in a browser due to the self closing <a/> tag.

I would have expected one of the following three HTML fragments being inserted into the HTML source:

<a name="my_place" id="myplace"><h2>My Heading</h2></a>

or

<h2><a name="my_place" id="myplace">My Heading</a></h2>

or

<a name="my_place" id="myplace"><a><h2>My Heading</h2>

Has anyone else experienced this? or know of a way to achieve what I had expected (without manually editing the HTML). Or is this a bug in the current version of the product.


回答1:


Attached is my sample XSLT template:

<template match="a[(@name) and (count(node()) = 0)]">
    <copy>
        <apply-templates select="@*"/>
        <xhtml:span xmlns:xhtml="http://www.w3.org/1999/xhtml" class="hidden"> </xhtml:span>
    </copy>
</template>

This adds a bit more than strictly needed, but handles some other issues we have due to XML manipulation on the Content Delivery side.

Essentially it matches all empty a tags with a name attribute, and add something between them in order to stop them self closing. In our case we post process all of the XML with XSLT, so we have challenges with empty tags getting closed all the time. So as a dirty hack, we are now inserting a hidden span tag between empty tags to prevent the issue.




回答2:


Thanks Chris, I've edited your solution to fit my requirements so wanted to share for anyone with this issue in the future.

Note: This moves the text inside the anchor and deletes the text outside. Fixes anchors that were intended to contain text only, not html. i.e My solution fixes this tag:

<p><a name="anchor1" id="anchor1"></a>Anchor text</p>

To

<p><a name="anchor1" id="anchor1">Anchor text</a></p>

But not this:

<p><a name="anchor1" id="anchor1"></a><h1>Anchor text</h1></p>

Here's my xsl. Hopefully it will help give you a base, I'm sure you could easily update it to look for a following tag (I don't require this for my solution).

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" method="html" cdata-section-elements="script"/>
    <xsl:template match="/ | node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <!-- fixes Tridion bug when using interface button to insert anchor in rich text field -->
    <!-- gets all empty anchor tags with an id and takes any following text and copies it inside anchor -->
    <xsl:template match="a[(@id) and (count(node()) = 0)]">
       <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:attribute name="{name(.)}">
                    <xsl:value-of select="."/>                    
                </xsl:attribute>
            </xsl:for-each>
            <xsl:value-of select="normalize-space(following-sibling::text())"/>
        </xsl:copy>
    </xsl:template>
    <!-- delete any text after an empty anchor (template above has already copied this text inside the anchor) -->
    <xsl:template match="text()[preceding-sibling::a[(@id) and (count(node()) = 0)]]" ></xsl:template>
</xsl:stylesheet>

Here's my test XML

<?xml version ="1.0"?>
<?xml-stylesheet type="text/xsl" href="tridionhtmlfield.xsl"?>
<html>
    <head></head>
    <body>
        <p><a id="anchorlink" name="anchorlink" title="Anchor link" href="#Anchor">Anchor link</a>Some text after</p>
        <p><a name="broken-with-html-name" id="broken-with-html-id"></a><h1>Anchor - broken with html</h1></p>
        <p><a name="broken-text-only-name" id="broken-text-only-id"></a>Anchor - broken text only</p>
        <p><a name="broken-notext-name" id="broken-notext-id"></a></p>
        <p><a name="correct-name" id="correct-id">Anchor - correctly rendered</a> Some text after</p>
    </body>
</html>

After transform:

<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
    <body>
        <p><a id="anchorlink" name="anchorlink" title="Anchor link" href="#Anchor">Anchor link</a>Some text after</p>
        <p><a name="broken-with-html-name" id="broken-with-html-id"></a><h1>Anchor - broken with html</h1></p>
        <p><a name="broken-text-only-name" id="broken-text-only-id">Anchor - broken text only</a></p>
        <p><a name="broken-notext-name" id="broken-notext-id"></a></p>
        <p><a name="correct-name" id="correct-id">Anchor - correctly rendered</a> Some text after</p>
    </body>
</html>

Hope this helps




回答3:


It looks like a bug to me Chris. I've just confirmed it on Chrome, Firefox and IE. It's completely counter-intuitive that the current text selection should be ignored. (On the plus side, once you fix it manually in the source tab, everything appears to behave perfectly.)

I suggest that you report this to Tridion, and perhaps work around it by altering your templating or filter XSLT.




回答4:


It is a bug in Tridion. One work-around that I suggest (and have implemented in our particular installation) is to do the following:

  1. Edit the FormatAreaStyles.css file (found in the Tridion CMS program files) — as well as your CSS file used by the website — to include a class like this:

.hiddenanchor { width:1px; height: 1px; display: block; text-indent:-50000px; }

  1. Publish out your CSS file (with the new class) so that it'll format your anchors properly.
  2. And then in the component where you are building out the anchors, you will have to:

    a. type a word or series of words in your component (where you want the target to be),

    b. select that text, and apply the anchor tag to it,

    c. then apply the new class you've created (.hiddenanchor) to the anchor.

In the end, your "invisible" anchor would look like this:

<a name="anchorname" id="anchorname" class="hiddenanchor">Anchor Name</a>

It's a crude work-around — fully acknowledged. But it works. You don't end up with the hyperlink/underline styling until the close of the next DOM object.

As an explanation to the CSS, the anchor technically has to be visible in the DOM for it to work and to be accessible by the anchor link. So "display: none" won't work. Alternatively to taking the text-indent approach, you could absolute or fixed position the text off the screen as well.



来源:https://stackoverflow.com/questions/10537656/creating-anchor-links-in-rich-text-fields-with-sdl-tridion-2011-sp1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!