Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Preventing Self Closing <a> Tags

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

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

Page: 1
Go to End
Author Topic: 736 Views
  • david.brunelle
    Avatar
    Community Member
    5 Posts

    Preventing Self Closing <a> Tags Link to this post

    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).

  • Tim Snadden
    Avatar
    Community Member
    32 Posts

    Re: Preventing Self Closing <a> Tags Link to this post

    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.

  • david.brunelle
    Avatar
    Community Member
    5 Posts

    Re: Preventing Self Closing <a> Tags Link to this post

    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

  • mtils
    Avatar
    Community Member
    3 Posts

    Re: Preventing Self Closing <a> Tags Link to this post

    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)
    );

    736 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.