code

Last modified by Lucas Charpentier on 2026/10/27 10:27

Audit

Macro ID
code
Source
XS
Type
Wrapper
Classification reason
Wrapper: most of the output is the code the user types, which the macro highlights. The advanced source parameter can take the code from a page, an attachment or a script variable instead, which is a generator use, but a rare one.
Status
Reviewed
Criteria version
15.20
XWiki version
18.8.0
Audit date
2026-10-14
Auditor
Compared macros
box html

Sources

Where to gather the information the criteria are checked against, see Gather the sources.

SourceWhat it givesUsed forWhere
DocumentationParameters, default values and examples, including the most common exampleL2, L3, and the criteria measured with the most common examplehttps://extensions.xwiki.org/xwiki/bin/view/Extension/Code%20Macro/
Source codeHow the macro declares its parameters and content: types, groups, features, advanced and hidden flags, constraints, content type, inline supportC4, C5, C7, C8, C9, C13, E4, E6https://github.com/xwiki/xwiki-platform/blob/xwiki-platform-18.8.0/xwiki-platform-core/xwiki-platform-rendering/xwiki-platform-rendering-macros/xwiki-platform-rendering-macro-code/src/main/java/org/xwiki/rendering/internal/macro/code/CodeMacro.java
Macro descriptorWhat the Edit Macro dialog is built from, on design.xwiki.orgD1, D2, C1 to C9, E4Descriptor, list of macros with their categories
TranslationsThe name, description and parameter names in each languageD1, D2, D4, C1l10n.xwiki.org
Uses in XWiki pagesHow XWiki itself uses the macro, a realistic example when the documentation has noneL3, C2, C3, C6, C8, C9Search the xwiki organization on GitHub
Compared macrosThe audits of the macros of the same typeL1, D3box, html
Other sources specific to this macro
CriterionResultNotes
D1 wordingOK
D2 summaryOK
D3 categoryKOminor: only in Formatting, like box, while a user may look for code under Development, where html is. A macro can have several categories since XWiki 14.6, so code could also be in Development.
D4 quick-actionKOQuick Actions finds the macro first for "code" and for the synonym "snippet", through its entry "Code Snippet". The synonyms "highlight", "syntax" and "programming" give no result. The French and German names are "Code", the same as the English one, so no language with a different name can be tested.
L1 consistencyOKcompared with box and html. Like html, the Quick Action opens the Edit Macro dialog and the content is typed in its text area; unlike html, the content is optional, since the source parameter can give it instead. The box parameters cssClass, image, title and width are inherited from box. The differences are by design.
L2 doc-parametersKOthe parameters are documented only on the legacy extensions.xwiki.org page, with no page in the new documentation. That page also documents only language, layout and source, and covers cssClass, image, title and width with "This macro also supports all Box Macro parameters".
L3 doc-exampleOKthe documentation example {{code language="html"}}<html>...</html>{{/code}} is used as the most common example. The first example of the page, {{code language=<providedlanguage>}} , holds a placeholder and cannot be copied as is.
I1 patternKOthe reference wrapper, warning, is inserted by its Quick Action without a dialog; the "Code Snippet" Quick Action opens the Edit Macro dialog, see I3.
I2 zero-configKOsubmitting the Edit Macro dialog with the default values inserts {{code}}{{/code}} , which renders an empty grey box in the editor and in view mode, and {{code/}} renders nothing: the user expects a code block and sees an empty frame.
I3 instantKOinserting from the Quick Actions menu with "/code" and Enter, entry "Code Snippet", opens the Edit Macro dialog with an empty Content text area; the macro is only inserted after submitting it.
I4 cursorKOafter submitting the dialog, the macro widget is selected and the cursor is not in its content: typed text goes nowhere, and the content cannot be typed in place, see E6.
I5 focusN/A
E1 wysiwygOK
E2 errorsKOinvalid values are accepted silently, with no message. For example, {{code language="nosuchlanguage"}}<b>x</b>{{/code}} renders the code without highlighting, {{code width="abc"}}x = 1{{/code}} renders with style="width:abc", and {{code image="missing.png"}}x{{/code}} renders a broken image. An invalid layout and an invalid source, on the other hand, give a message that names the problem, like "Unable to convert value [foo]. Allowed values are (case insensitive) "PLAIN" or "LINENUMBERS"." for layout="foo" .
E3 nestingOKin a list item and in a table cell the macro is inline, so the multi-line example becomes a span with a background per line instead of a code block, and in the editor the bullet of * {{code language="html"}}<html>...</html>{{/code}} is shown in front of the last line. Inline code is still usable, and saving keeps the source. A list, a table and a box inside its content are shown as text, as expected for code.
E4 inline-supportOKthe macro declares inline support and renders as a span inside a sentence, as in Run {{code language="bash"}}mvn clean install{{/code}} now. .
E5 emptyKOthe content is never editable in place. The empty macro stays visible as an empty grey box, but one click selects the widget and the cursor never enters it: the content can only be typed in the Edit Macro dialog.
E6 inplaceKOthe content is never edited in place: in the editor the code is a read-only widget, and changing it needs the Edit Macro dialog, where it is a plain text area with no code editing support. For example, the most common example {{code language="html"}}<html>...</html>{{/code}} cannot be changed by clicking in the displayed code. XWIKI-19978 asks for it.

Parameters

ParameterC1 clarityC2 mandatoryC3 defaultsC4 input-assistanceC5 groupsC6 orderC7 alternativesC8 advancedC9 technicalC13 constraintsNotes
$contentOKOKN/AOKN/AOKN/AN/AN/AN/A

C4: a plain text area in the Edit Macro dialog, counted under E6.

cssClassOKOKOKOKKOOKN/AKOOKN/A

C1: "A CSS class to add to the box element" uses technical terms, which fit a styling parameter: describing it another way would only be more confusing.

C5, minor: the 8 fields, content included, are all in the single group "Optional parameters", above 7. The same holds for every parameter.

C8: a styling parameter that is rarely used, and no common example sets it, but it is not marked as advanced.

imageKOOKOKKOKOOKN/AOKKON/A

C1: the description "the image which is to be displayed in the message box" calls the code block a message box, and does not say what value is expected, an attachment or a URL.

C4: a free text field, with no image picker, although the type is a resource reference; a reference to a missing attachment is accepted.

C9: inherited from box, and no example sets it: users do not expect an image on a code block, so the field is only noise in the dialog and should be hidden for this macro.

languageKOOKOKKOKOKON/AOKOKN/A

C1, minor: the description "the language identifier (java, python, etc.)" does not say that an empty value detects the language automatically, which is what the empty field means, nor that "none" turns the highlighting off.

C4: a free text field, although the accepted values are the fixed list of Pygments languages: the user has to know the identifier, like "html" or "bash", and a wrong one, like "nosuchlanguage", is accepted silently. A select with suggestions could be used.

C6: language is the only parameter set in the most common example, but it comes fourth, after Content, CssClass and Image, in alphabetical order.

layoutOKOKOKOKKOOKN/AOKOKN/A

C3: the select preselects Plain, the declared default.

sourceOKOKOKKOKOOKOKOKOKN/A

C4: a free text field for a typed reference, like "page:Page/SubPage" or "attachment:[email protected]", with no page or attachment picker: the user has to know the syntax of the reference. A proper field needs design work, since some types, like "script:", cannot have a picker.

C7: source and the content are alternative ways to give the code, the source replacing the content, but the Edit Macro dialog cannot present them as one choice, since only parameters can be part of a feature. A limitation of the macro system, listed in the Cross-macro findings, reported as XWIKI-25456.

C9: advanced, and used by realistic examples of the documentation, like highlighting an attachment.

titleKOOKOKOKKOOKN/AOKOKN/A

C1: the description "the title which is to be displayed in the message box" calls the code block a message box. It does not say either that the title accepts wiki syntax.

C9: no example sets it, but a file name above a snippet is a realistic use.

widthOKOKOKOKKOOKN/AOKOKKO

C13: only accepts a CSS length "in px or %", but declares no format: "abc" is accepted by the dialog and output as style="width:abc".

KOs and tickets

Every KO needs a ticket covering it, see Report the KOs. One ticket can cover several KOs. Known issues filed for this macro are suggested in italics: link them as existing tickets when they cover the KO.

KOCriterionExisting ticketsNew tickets
D3D3 categoryXWIKI-25481
D4D4 quick-actionXWIKI-25444
L2L2 doc-parametersXINFRA-525
I1I1 patternXWIKI-25572
I2I2 zero-configXWIKI-25572
I3I3 instantXWIKI-25572
I4I4 cursorXWIKI-25569
E2E2 errorsXCOMMONS-3847 (partial), XRENDERING-823 (partial), XRENDERING-826 (partial), XWIKI-25561 (partial), XWIKI-25582 (partial)
E5E5 emptyXWIKI-19978
E6E6 inplaceXWIKI-19978
C5:cssClassC5 groups, cssClassXWIKI-25482
C8:cssClassC8 advanced, cssClassXWIKI-25482
C1:imageC1 clarity, imageXWIKI-25481, XWIKI-25561
C4:imageC4 input-assistance, imageXWIKI-25561
C5:imageC5 groups, imageXWIKI-25482, XWIKI-25561
C9:imageC9 technical, imageXWIKI-25482, XWIKI-25561
C1:languageC1 clarity, languageXWIKI-17167
C4:languageC4 input-assistance, languageXWIKI-17167
C5:languageC5 groups, languageXWIKI-17167
C6:languageC6 order, languageXWIKI-25482
C5:layoutC5 groups, layoutXWIKI-25482
C4:sourceC4 input-assistance, sourceXWIKI-25575
C5:sourceC5 groups, sourceXWIKI-25482
C1:titleC1 clarity, titleXWIKI-25481
C5:titleC5 groups, titleXWIKI-25482
C5:widthC5 groups, widthXWIKI-25482
C13:widthC13 constraints, widthXRENDERING-826
Success

Every KO is covered by a ticket.

Tickets

TicketOriginKOs coveredKOs partly covered
XWIKI-17167ExistingC1:language C4:language C5:language
XWIKI-19978ExistingE5 E6
XCOMMONS-3847NewE2
XRENDERING-823NewE2
XWIKI-25444NewD4
XWIKI-25481NewC1:image C1:title D3
XWIKI-25482NewC5:cssClass C5:image C5:layout C5:source C5:title C5:width C6:language C8:cssClass C9:image
XINFRA-525NewL2
XRENDERING-826NewC13:widthE2
XWIKI-25561NewC1:image C4:image C5:image C9:imageE2
XWIKI-25569NewI4
XWIKI-25572NewI1 I3 I2
XWIKI-25575NewC4:source
XWIKI-25582NewE2

Known issues of this macro

IssueTitleStatusCriteria
XRENDERING-773Leading and final new lines should not be discarded from inline macro contentReopenedE4
XWIKI-17167Code Macro Edit Dialog Should Better Document Supported LanguagesOpenC4
XWIKI-19978"Code" Macro Should Support Inline EditingOpenE6
XWIKI-22210Blocks of {{code}} are acting strangeOpen
XWIKI-22832Display line numbers when editing code macroOpenE6

68 more known issues affect every macro: see the Known issues.

Get Connected