SVG <text> element with xml:preserve-whitespace is affect by mix format

I need to show a inside a text element in SVG and try to use xml:preserve-whitespace which works but mix format on save modifies the output because it adds a space before and after (on a different line).

  def tile(assigns) do
    ~H"""
    <svg
      viewBox="0 0 100 100"
      width="100%"
      fill="grey"
    >
      <rect
        width="98"
        height="98"
        x="1"
        y="1"
        rx="2"
        ry="2"
        fill="ivory"
        stroke="darkolivegreen"
        stroke-width="1"
      >
      </rect>
      <text
        x="49"
        y="70"
        text-anchor="middle"
        font-size="65"
        stroke="brown"
        fill="brown"
      >
        {@letter}
      </text>
      <text x="71" y="87" font-size="20" stroke="darkblue" fill="darkblue">{@score}</text>
    </svg>
    """
  end

Is there a way round this?

Disable formatting for heex, or just in general. Unfortunately the heex formatter takes a simplistic approach when it comes to xml in general.

1 Like

Have you tried adding phx-no-format?

Thanks this is correct.
But my problem was actually imaginary.

For some reason cast in Ecto.Changeset is removing " " from and array

Ecto.Changeset.cast(%Scrabble.Games.Player{ name: nil, user_id: nil, rack: nil, score: 0},  %{
                                            name: "AN Other",
                                            user_id: "019d011c-2f62-7339-8615-175e2228a55d",
                                            rack: ["Z", " ", " ", " ", " ", " ", " "],
                                            score: 0
                                          },
                      [:name, :user_id, :rack, :score], [])
#Ecto.Changeset<
  action: nil,
  changes: %{
    name: "AN Other",
    user_id: "019d011c-2f62-7339-8615-175e2228a55d",
    rack: ["Z"]
  },
  errors: [],
  data: #Scrabble.Games.Player<>,
  valid?: true,
  ...
>

Is there something I am missing in the schema?

@primary_key false
  embedded_schema do
    field :user_id, Ecto.UUID
    field :name, :string
    field :rack, {:array, :string}
    field :score, :integer
  end

See

1 Like

does phx-no-format work on this? i’ved used it with success on <a> tags. edit to see the earlier comments :slight_smile:

Yes it does