Macro: how to have a field with content with new lines

Hi.

I wrote a small macro for spoilers. The only thing missing is that the spoiler content can’t have new lines. It’s plain text in the end. Wiki syntax is working.

I guess it’s a question of the parameter type. Currently I don’t have any type configured. It should look like a textarea.

Any hints? Regards, Simpel

Hi @Simpel,

Is your macro doing something close to https://extensions.xwiki.org/xwiki/bin/view/Extension/Spoiler%20Macro?

A bit. Ours looks like:
image

Its pure html <details> due to accessibility. I try to check the extension you mentioned.

Ok. I did test it. It’s not accessible by keyboard. So no, nothing for us.

The ShowHide macro lacks the same.

This is the code for our “spoiler” macro:

{{velocity}}
#set ($title = $xcontext.macro.params.title)
#set ($content = $xcontext.macro.params.content)
#if($title && $content)
  {{html clean="false" wiki="true"}}
  <style>
  .spoiler {
    margin-bottom: 10px;
  }
  details[open] > summary {
    margin-bottom: 10px;
  }
  summary {
    display: list-item;
    cursor: pointer;
  }
  summary:focus {
    box-shadow: none !important;
  }
  </style>
  <details class="spoiler">
    <summary>$title</summary>
    $content
  </details>
  {{/html}}
#end
{{/velocity}}

You should use {{wikimacrocontent/}} and {{wikimacroparameter name="title" /}} to display the content/title. This ensures that the content is rendered correctly and that it is not vulnerable to XWiki syntax injection and XSS (see our security guide on how to escape content). You could take the expand macro as an example, this should fulfil your requirements I think.

1 Like

Two problems with that solution.

First the list item marker of the summary isn’t inline any more. That could be fixed with:

summary > p {
    display: inline;
}

But second more important: I can’t use any styling for the content. Any suggestion?

Simpel

Maybe you didn’t configure the macro content type properly (i.e., as WIKI)?

Regarding the title, I think there is a bug in the wiki macro renderer that doesn’t remove the wrapping paragraph when then parameter doesn’t support XWiki syntax. It might better when you specify the parameter type as WIKI, but I’m also not sure if the “inline” detection works in that mix of HTML and XWiki syntax. If you don’t want to support formatting, you could also just use the same code as the expand macro, i.e., $services.rendering.escape($escapetool.xml("${wikimacro.parameters.title}"), 'xwiki/2.1').

Ha. I did set content type to WIKI but didn’t recognize, that in your proposal “content” isn’t a parameter any more. It’s working now.

The css for the summary is working so I don’t care anymore.
Thanks.

PS: Full code for others:

{{velocity}}
  {{html clean="false" wiki="true"}}
  <style>
  .spoiler {
    margin-bottom: 10px;
  }
  details[open] > summary {
    margin-bottom: 10px;
  }
  summary {
    display: list-item;
    cursor: pointer;
  }
  summary:focus {
    box-shadow: none !important;
  }
  summary > p {
    display: inline;
  }
  </style>
  <details class="spoiler">
    <summary>{{wikimacroparameter name="title" /}}</summary>
    {{wikimacrocontent/}}
  </details>
  {{/html}}
{{/velocity}}