Paste this

$html = "<p>line 1<br />\nline 2<br />line 3</p><p>paragraph</p>";
dpm($html);
dpm(drupal_html_to_text($html));

into Devel's Execute PHP textarea (devel/php) and you'll get

  • <p>line 1<br />
    line 2<br />line 3</p><p>paragraph</p>
  • line 1line 2line 3paragraph

What you should get is

  • <p>line 1<br />
    line 2<br />line 3</p><p>paragraph</p>
  • line 1
    line 2
    line 3

    paragraph

The culprit is the last change in this commit.

The commit points to #356074: Provide a sequences API, but this is completely unrelated. Apparently, the committer had applied at least one additional patch when he committed 356074, but I haven't been able to find the other issue.

Here's a patch that adds the newline back.

CommentFileSizeAuthor
drupal_html_to_text-linebreaks.patch580 bytessalvis

Comments

salvis’s picture

Status: Active » Needs review
droplet’s picture

RTBC. but leave it for one more to review it

marcingy’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs review » Reviewed & tested by the community

Does this need to be applied to d8 first?

salvis’s picture

Issue tags: +Needs backport to D7

Yes, D8 first, thanks!

D6 is fine, the "\n" is still there in D6 (line 406 of 6.x), only D7 was borked.

droplet’s picture

@salvis,
thanks. the API site is wrong. :(

damien tournoud’s picture

Status: Reviewed & tested by the community » Closed (works as designed)

This is just completely by design. This was a bug in the function now fixed. Fixing the remaining bugs is handled by #299138: Improve \Drupal\Core\Utility\Mail::htmlToText().

salvis’s picture

Status: Closed (works as designed) » Closed (duplicate)

Well, no, it's not working as designed (or at least not as it's supposed to work). It's a regression that was introduced in D7 with the commit that I linked to. I don't remember having any problems with the D6 version of drupal_html_to_text().

Thank you for the link. I'm fine with having this (and any other newly introduced bugs) fixed elsewhere, which makes this issue a duplicate.

It would be nice if you could use a kinder, less condescending tone (see http://drupal.org/dcoc).

damien tournoud’s picture

Status: Closed (duplicate) » Closed (works as designed)

This is *by design*. In HTML line breaks are *not* significant. drupal_html_to_text() takes HTML as the input and returns an acceptable plain-text representation. It should (and does in Drupal 7, since fixed) collapse whitespace.

damien tournoud’s picture

Status: Closed (works as designed) » Closed (duplicate)

Ok, I misread.

salvis’s picture

Issue tags: -Needs backport to D7

Thank you for your graceful apology, Damien.

Just for the sake of completeness here's what I found out about this issue: #407452-64: Remove use of drupal_html_to_text() from system mails is what was committed inadvertently, which was apparently an unfinished attempt to enable drupal_mail() to send HTML emails.

The error was noticed a year ago in #299138-48: Improve \Drupal\Core\Utility\Mail::htmlToText(), but instead of rolling back the inadvertent commit or raising priority to critical (after all, D7 is unable to send emails with any reasonable formatting such as line breaks), this has been dragging on ever since.

In #57 sun, the author of the premature patch, concluded that Test results are clear, the current function is horribly broken, and in #69 pillarsdotnet posted essentially the same patch as mine above. That was 10 weeks ago.

Seeing that that thread might take a while to come to a conclusion, pillarsdotnet tried to get this fixed quickly with #1130198: Regression: line-breaks are mangled by \Drupal\Core\Utility\Mail::htmlToText(), but apparently we wanted to release 7.1 with broken mail functionality. Sorry about the sarcasm here...

#128 and #129 provide essential information, which I had been missing before.

My conclusion is that drupal_mail() is and for the foreseeable future remains unusable, but the D6 mail.inc should work just fine. I'll add that to my project for now, renaming the functions to avoid conflicts, of course.