Skip to content

Drop \n when highlighting #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged

Drop \n when highlighting #79

merged 2 commits into from
Aug 10, 2023

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Aug 8, 2023

In a second step the CSS will need to be adjusted.


Actual newlines will be replaced by <br /> during highlighting and the \n in the boilerplate conflicts with white-space: pre;.

see php/doc-en#2648

Actual newlines will be replaced by `<br />` during highlighting and the `\n`
in the boilerplate conflicts with `white-space: pre;`.

see php/doc-en#2648
@TimWolla TimWolla requested a review from Girgias August 8, 2023 15:10
@kamil-tekiela
Copy link
Member

For an easier understanding, it would be nice if you could provide examples. How exactly does it interfere? Why do we now need to remove the \n?

@TimWolla
Copy link
Member Author

TimWolla commented Aug 8, 2023

To properly include the original indentation of stuff output of <?php … ?> tags (i.e. embedded HTML), the existing:

.phpcode code span span { white-space: pre; }

formatting is insufficient, because the selector doesn't match.

However putting white-space: pre onto .phpcode code will cause bogus formatting, because of the included newlines (there is one at the beginning right in front of the code and one at the end):

image

Without the newlines:

image

@kamil-tekiela
Copy link
Member

kamil-tekiela commented Aug 8, 2023

Honestly, we should probably stop using highlight_string(). It breaks HTML standard in multiple ways and the resulting formatting is questionable. This is just one of many issues that we had on php.net.
We abuse the <code> element which is meant to be an inline element. The proper solution to the current issue would be to wrap the <code> element within <pre> as is recommended. But as you noticed, we have newlines in weird spots within the highlighted string.

We could fix the function within php-src because we technically are allowed to by the PHP documentation:

The HTML markup generated is subject to change.

I think it would be the best fix, but we would need to wait for the next minor PHP release and then upgrade the environment for php.net.

The fix would be easy:

  • Instead of <br> output \n
  • Instead of &nbsp; output
  • Instead of <code> use <pre>
  • Instead of <span> use <code>
  • Remove the first enclosing <code> (old <span>)
  • Remove the enclosing newlines

No more breaking HTML standard, no more adjustments of the output, and no more CSS styles on php.net.

For the time being, I guess we could accept your PR.

@@ -63,7 +63,10 @@ public function highlight($text, $role, $format)

if ($role == 'php') {
try {
return str_replace('&nbsp;', ' ', highlight_string($text, 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str_replace() can also take an array of values to replace and an array of what those values should be, why not just use that?

But the issue seems to be more that \n tags before and after are being converted to <br> so wouldn't it make more sense to just ltrim() new lines of the $text variable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str_replace() can also take an array of values to replace and an array of what those values should be, why not just use that?

see #79 (comment)

But the issue seems to be more that \n tags before and after are being converted to
so wouldn't it make more sense to just ltrim() new lines of the $text variable?

This wouldn't work, because the extra newlines are inserted by highlight_string(), namely here:

https://github.com/php/php-src/blob/f8b42da3a4635aa2ace3d4dbfdbae6835fb43d7f/Zend/zend_highlight.c#L92

and here:

https://github.com/php/php-src/blob/f8b42da3a4635aa2ace3d4dbfdbae6835fb43d7f/Zend/zend_highlight.c#L165-L167

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh why is it doing that >.>

@TimWolla TimWolla merged commit 01d6beb into php:master Aug 10, 2023
@TimWolla TimWolla deleted the highlight-drop-nl branch August 16, 2023 11:51
cmb69 added a commit to cmb69/phd that referenced this pull request Oct 5, 2024
PHP 8.3 changed the details of highlighting PHP code[1], basically like
we did[2] at roughly the same time.  However, both don't work well
together, so we avoid that.

[1] <php/php-src#11913>
[2] <php#79>
cmb69 added a commit that referenced this pull request Oct 5, 2024
PHP 8.3 changed the details of highlighting PHP code[1], basically like
we did[2] at roughly the same time.  However, both don't work well
together, so we avoid that.

[1] <php/php-src#11913>
[2] <#79>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants