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

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.

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.

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

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.

It is a bug in Tridion. One work-around that I suggest (and have implemented in our particular installation) is to do the following:
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;
}
Publish out your CSS file (with the new class) so that it'll format your anchors properly.
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.

Related

How to convert a xml element into url using css?

I have an xml file which has an element like this
<video><title>XXXX</title><url>VIDEO ID OF YOUTUBE></url></video>
Is there any way to use CSS to display the elements as
XXXX
CSS is for Styling, if you wish to read XML and produce HTML Content you should use Javascript. Or any server side language if you want it to be done before sending it to the client browser.
As Nicklas points out, many think of Css to be intended to be for styling and nothing more. More generally, though, it's intended for the presentation of your information. This is a fringe case that's difficult for me to say whether it's going too far or within the scope of CSS: is this simply changing the presentation, or is it doing something more?
I'm sure many, like Nicklas, would argue that what you want to do goes beyond the intended purpose of CSS. And I'd probably agree with Nicklas in that for most cases I'd find this to be a less-than-ideal way to go about things.
With that said, it is possible
#url:before {
display: inline-block;
content: "<a href=\"http://www.youtube.com/";
}
#url:after {
display: inline-block;
content: "\">";
}
#text:after {
display: inline-block;
content: "</a>";
}
Note: I used Html in this example for the sake of making a JsFiddle, but the same strategy should work for an Xml file
I do not know of a way to use CSS for this. But you can do this using a XSLT Stylesheet. Is that what you want? Then a XSLT Stylesheet similar to this can help you:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/" >
<body>
<xsl:apply-templates select ="/items/video"/>
</body>
</xsl:template>
<xsl:template match="video" >
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="./url" disable-output-escaping="yes" />
</xsl:attribute>
<xsl:value-of select="./title"/>
</a>
</li>
</xsl:template>
<xsl:template match=
"*[not(#*|*|comment()|processing-instruction())
and normalize-space()=''
]"/>
<xsl:template match="text()"/>
</xsl:stylesheet>
This assumes that the XML input file looks as follows:
<?xml version="1.0" encoding="utf-8" ?>
<items>
<video>
<title>XXXX</title>
<url>VIDEO ID OF YOUTUBE></url>
</video>
<video>
<title>XXXX</title>
<url>VIDEO ID OF YOUTUBE></url>
</video>
</items>
Exactly how you should do the transform is hard to say when I don't know about your requirements. A XSLT transform can be done in a number of ways. If the XML input file is static one can let the webbrowser do the transformation. If the XML file is on a server you can write a transform in a awebpage and sen the HTML to the webbbrowser. It all depens on your environment.

Deleting all text from a text box leaves behind a <br> tag

I am using Tridion 2011 SP1 and Firefox 16.0.2, Internet Explorer 8, Internet Explorer 9 and Chrome 23.0.1271.95.
When I open a component in any of the browsers and delete all text in the Design view of a rich text field (pressing delete and backspace multiple times) I am left with a variety of tags in the Source view. These vary by browser.
Firefox : <br />
Chrome : <p><br /></p>
IE8 : <p></p>
IE9 : <p></p>
I have checked the list of hot fixes and there is nothing relevant.
Any ideas?
This sounds like a bug which should be submitted to SDL Customer Support. Does the behavior differ with IE, Chrome and FF?
Unless support can provide a hot fix, I think your only option is to filter out the lone BR (as Frank suggested) using the filtering XSLT. The default XSLT filter used when creating a new rich text field has this filtering included as shown below (last <template> node).
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></output>
<template match="/ | node() | #*">
<copy>
<apply-templates select="node() | #*"></apply-templates>
</copy>
</template>
<template match="*[ (self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(#*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or #* or * or node()]) and not(following::node()[not( (self::text() or self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(#*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or #* or * or node()]) )]) ]">
<!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks -->
</template>
<template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]">
<!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space -->
<text> </text>
</template>
</stylesheet>

Add class to body tag using diazo with notheme

i'm displaying the content of a document in an overlay using
plone/document?ajax_load=True&ajax_include_head=True as the src for an iframe.
in development mode appending &diazo.off=1 did the trick.
on the production server this sadly does not work, so i added the
ajax_load parameter as suggested in the documentation of plone.app.theming
i wrapped all my directives in a <rules if-not="$ajax_load"> element to make sure they are not applied (see code below)
now i'd need to mark the body of the iframed page with a certain class to apply different styles (eg no background color for the body in overlays)
the solution proposed for a nearly similar question does only work if you are using a theme with a body element having a class attribute to operate on.
is there a way to add a class to the content without having a theme (using )?
or do i have to supply an empty html document (index2.html) as theme and apply lots of rules to copy over css/js etc twice?
<rules if="$ajax_load">
<!-- theme href="index.html" /-->
<notheme />
<!-- only works when using a theme -->
<before theme-children="/html/body"><xsl:attribute name="class"><xsl:value-of select="/html/body/#class"/> my class</xsl:attribute></before>
<!-- thought this could to the trick but does not work at all -->
<xsl:template match="html/body">
<xsl:attribute name="class"> foo</xsl:attribute>
</xsl:template>
</rules>
<rules if-not="$ajax_load">
<theme href="index.html" />
<replace content="/html/head/title" theme="/html/head/title" />
...
Obviously without a theme to operate on, any rule referencing a theme does not work in conjunction with <notheme/>. You can however <drop> and <replace> content (I have ideas of how to implement <before> and <after> content, but its difficult to implement. Maybe in a future version of Diazo.) However you can achieve the same thing with a small amount of xslt:
<replace content="/html/body/#class">
<xsl:attribute name="class"><xsl:value-of select="."/> newclass</xsl:attribute>
</replace>
If you are using plone.app.jquerytools for your overlays, then you can pass a class parameter:
$('a.myPopover').prepOverlay({
subtype: 'iframe',
cssclass: 'my-popover'
});
In some of our project using plone.app.tiles we used a specific theme template for the overlays, that was stripped of all unecessary parts and reduced to a single wrapper div and used it for the tile edit view like so:
<theme href="minimal.html" if-path="##edit-tile" />

xsl:attributes strips the "content"

I'm writing some rules in Diazo. I want, in case the user browses the "viewer" section (a browser view, not a real plone folder), to drop the "selected" class for the "home" tab in the globalnav and put "selected" class for the "viewer" tab.
<replace css:content="#portal-globalnav" css:theme="#portal-globalnav" />
<drop if-path="viewer/"
css:content="#portaltab-index_html"
attributes="class" />
<xsl:template if-path="viewer/"
match="//li[#id='portaltab-viewer']/">
<xsl:attribute name="class">selected</xsl:attribute>
</xsl:template>
But the result it's a right li portaltab-viewer with the "selected" class, but without any content inside! I obtain an empty "li" tag in the portal-globalnav O.O
What's wrong?
Vito
You need to recurse into the content of the element with xsl:apply-templates. Try:
<replace if-path="/viewer" css:content-children="li#portaltab-viewer"><xsl:attribute name="class">selected</xsl:attribute><xsl:apply-templates select="node()"/></replace>
The lack of whitespace before the xsl:attribute is necessary as I don't think I ever got around to making Diazo ignore whitespace around xsl:* elements.

Question related to layout and margin of text in XSL

My other question about layout the text correctly on the webpage. I would like to write RSS code and then use XSL to format the RSS into HTML
My question:
Why the text of the ITEM DESCRIPTION run out of the margin of the webpage? HOw to force that piece of text maintain inside the margin?
Run out of margin http://i40.photobucket.com/albums/e236/ngquochung86/somemargin.jpg
My XML CODE:
<channel>
.......
<description>
CHANNEL DESCRIPTION: Website of New York Times
</description>
<item>
<title>
ITEM TITLE:YouTube and Universal to Create a Hub for Music
</title>
<link>
<a href="http://www.nytimes.com/2009/04/10/technology/internet/10google.html?ref=technology"/>
</link>
<description>
CHANNEL DESCRIPTION: The agreement is an effort by YouTube, which is owned by Google, to put more professionally produced content in front of its huge audience, and in turn, earn more money from advertising.
</description>
</item>
My XSL code:
<span class="channelDescription"> <xsl:value-of select="channel/description"/></span>
<xsl:for-each select="channel/item">
<span class="itemTitle"><xsl:value-of select="title"/></span>
<span class="itemLink"><xsl:value-of select="link"/></span>
<span class="itemDescription"><xsl:value-of select="description"/></span>
</xsl:for-each>
Would you please help me? Thank you
Like AnthonyWJones said, this seems like a CSS/HTML question. Once you use XSLT to transform the RSS feed into HTML, it is simply treated (and thus rendered) as a standard HTML document.
My best guess at this point is that you need to put a container (a block level element such as <DIV>) around the description (or the whole document) and specify a width for it that is within the dimensions of the display using CSS.
So something like this should work:
<style type="text/css">
#container {
width: 90%;
}
</style>
<div id="container">
// Your XSL code
</div>

Resources