Why does #sym.dash.en in a box create a break where other characters do not

While trying to implement something like TEX’s \llap I came across an odd behavior with endash in which a narrow box forces the rest of the text to the next line, leaving the dash above. This is best displayed by running the following snippet:

#for c in ("A", sym.dash.en, "+", sym.dash.em, sym.hash) [
 #c: #h(1cm) #box(width:0pt)[#text(fill: red)[#align(right)[#c~]]] XXX\
]

Is this explained somewhere?

I would say that it’s understandable, since I don’t know what is supposed to happen to the content inside box(width: 0pt). You can open an issue about it. It would be great if it behaved consistently.

#set box(width: 0pt) // Toggle
#set block(spacing: 0pt)
#set line(length: 1cm, stroke: 0.5pt + gray)
#for c in ("A", sym.dash.en, "+", sym.dash.em, sym.hash) {
  line()
  block[#c#h(5pt)#box[#c~]]
  line()
}

It might be something like word-splitting behavior or minus sign or some other special behavior.

Actually the normal minus also has this issue:

#set box(width: 0pt) // Toggle
#set block(spacing: 0pt)
#set line(length: 1cm, stroke: 0.5pt + gray)
#for c in ("A", sym.dash.en, "-", "+", sym.dash.em, sym.hash) {
  line()
  block[#c#h(5pt)#box[#c~]]
  line()
}

So it’s [-] and [--].

Here is an issue: `-` and `--` behave differently with no-break space than all other characters when inside zero-width `box` · Issue #6400 · typst/typst · GitHub.

As a workaround, you can convince the dash and nbsp to stick together by putting a word joiner between them:

#box[#c#sym.wj~]