mangstadt/ez-vcard

Caret encoding: \n not encoded to ^n

Closed this issue · 3 comments

Hi,

Thanks for your great library! When testing around with DAVx⁵, I have found the following:

According to RFC 6868 p.2, when generating vCards, caret encoding means:

o formatted text line breaks are encoded into ^n (U+005E, U+006E)
o the ^ character (U+005E) is encoded into ^^ (U+005E, U+005E)
o the " character (U+0022) is encoded into ^' (U+005E, U+0027)

ez-vcard encodes the ^ and ", but not \n or \r\n:

    @Test
    fun testGenerateCaretNewline() {
        val vCard = VCard()
        vCard.addAddress(Address().apply {
            label = "Li^ne 1,1\n- \" -"
            streetAddress = "Line 1"
            country = "Line 2"
        })
        val str = Ezvcard .write(vCard)
                .version(VCardVersion.V4_0)
                .caretEncoding(true)
                .go().lines().filter { it.startsWith("ADR") }.first()
        // would expect:
        // assertEquals("ADR;LABEL=\"Li^^ne 1,1^n- ^' -\":;;Line 1;;;;Line 2", str)
        // but this happens:
        assertEquals("ADR;LABEL=\"Li^^ne 1,1\\n- ^' -\":;;Line 1;;;;Line 2", str)
    }
    ```

I'm not absolutely sure as I'm not an expert on caret encoding, but shouldn't the newline be encoded as `^n`?

Hello again! According to RFC 6350 p.33, newlines can be escaped in the LABEL parameter using \n. This rule only applies to the LABEL parameter of the ADR property.

      The property can also include a "LABEL" parameter to present a
      delivery address label for the address.  Its value is a plain-text
      string representing the formatted address.  Newlines are encoded
      as \n, as they are for property values.

Because this rule is unique to this specific parameter, I decided to have this rule take precedence over the caret encoding rules.

If you add a newline to any other parameter, you'll see that it escapes the newline according to the caret encoding syntax.

Ahh, thanks! :) I guess \n is also more compatible.

Sorry for the noise.

I guess \n is also more compatible.

I'm not sure why they didn't use backslash escaping everywhere. It would have made more sense (to me, anyway).

Sorry for the noise.

It's no problem, I appreciate the feedback.