Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Preventing Self Closing <a> Tags


Go to End


4 Posts   2182 Views

Avatar
david.brunelle

Community Member, 5 Posts

30 September 2012 at 7:13am

Hoping there is an easy solution...

Using SilverStripe 3.0.2, and I'm trying to insert "Anchors" in to an HTMLEditorField. Something like <a name="anchor"></a>. However, when I save my page the tag ends up being converted to a self-closing <a> tag like this: <a name="anchor" />. This results in browsers thinking there's an unclosed tag on the page and causing some general bugginess.

Does anyone know what's happening here or how to prevent it? I think it's SilverStripe making this change and NOT TinyMCE (viewing the source in TinyMCE still shows separate opening and closing tags).

Avatar
Tim Snadden

Community Member, 32 Posts

30 September 2012 at 8:13am

You may want to consider asking why you are doing this as there are likely better solutions. If you want to create the ability to link to parts of the page I would recommend adding ids (not names) to the elements you want to link to. I'm digging into my memory banks here but I seem to remember that a LONG time ago (maybe even as far as Netscape 3?) there were browsers that couldn't do internal page links to ids on elements and required and empty <a> element. Now you can just add an id to an element and link to it no problem.

Avatar
david.brunelle

Community Member, 5 Posts

1 October 2012 at 2:59am

Thanks Tim. I'm using anchors because that's what TinyMCE does and what my clients want. There's not an easy way to assign IDs to various headings using a WYSIWYG editor.

So, regardless of what's best... I still need to figure out how to prevent SS from making tags self-closing.

-David

Avatar
mtils

Community Member, 3 Posts

8 November 2012 at 8:00pm

Hi,

this issue is solved in Silverstripe 3.0.3.

see: https://github.com/silverstripe/sapphire/pull/793

However if you need a quick fix:

Change this in HTMLValue.php Line 33: (HTMLValue::getContent())

return preg_replace (
array (
'/^\s*<body[^>]*>/i',
'/<\/body[^>]*>\s*$/i',
'/<\s*a(.*)\/>/i'
),
array(
',
'',
'<a $1></a>'
),
$this->getDocument()->saveXML($this->getDocument()->documentElement->lastChild)
);