Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Removed spurious 'new' in doc. Need to call htmlentities() twice to e…
Browse files Browse the repository at this point in the history
…nsure proper rendering of HTML content.
  • Loading branch information
gorhill committed Aug 16, 2011
1 parent d954b60 commit e2098e3
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions finediff.php
Expand Up @@ -44,7 +44,7 @@
* // FineDiff::$wordGranularity = word level
* // FineDiff::$characterGranularity = character level [default]
*
* $opcodes = new FineDiff::getDiffOpcodes($from_text, $to_text [, $granularityStack = null] );
* $opcodes = FineDiff::getDiffOpcodes($from_text, $to_text [, $granularityStack = null] );
* // store opcodes for later use...
*
* ...
Expand Down Expand Up @@ -671,17 +671,17 @@ private static function renderToTextFromOpcode($opcode, $from, $from_offset, $fr

private static function renderDiffToHTMLFromOpcode($opcode, $from, $from_offset, $from_len) {
if ( $opcode === 'c' ) {
echo htmlentities(substr($from, $from_offset, $from_len));
echo htmlentities(htmlentities(substr($from, $from_offset, $from_len)));
}
else if ( $opcode === 'd' ) {
$deletion = substr($from, $from_offset, $from_len);
if ( strcspn($deletion, " \n\r") === 0 ) {
$deletion = str_replace(array("\n","\r"), array('\n','\r'), $deletion);
}
echo '<del>', htmlentities($deletion), '</del>';
echo '<del>', htmlentities(htmlentities($deletion)), '</del>';
}
else /* if ( $opcode === 'i' ) */ {
echo '<ins>', htmlentities(substr($from, $from_offset, $from_len)), '</ins>';
echo '<ins>', htmlentities(htmlentities(substr($from, $from_offset, $from_len))), '</ins>';
}
}
}
Expand Down

4 comments on commit e2098e3

@elektronaut1024
Copy link

Choose a reason for hiding this comment

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

Why was the call to htmlentities doubled? This creates unreadable HTML output when comparing HTML or PHP files with lots of > and <...

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on e2098e3 Apr 1, 2014

Choose a reason for hiding this comment

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

I can't remember, I would have to look again in all this to figure why.

The diff'ed HTML does display properly on this page: http://www.raymondhill.net/finediff/viewdiff.php?data=c923f8bf66c6c66df22ef63d43faefc4da98757c. So clearly it is required.

However not on http://www.raymondhill.net/finediff/viewdiff-ex.php , because this one also call htmlentities() on the result -- which means 3 levels of htmlentities encoding. I believe this was done one extra time there because it is also possible to test the standard diff library, which doesn't escape HTML entities. Trying to figure why it doesn't display properly in viewdiff-ex.php.

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on e2098e3 Apr 1, 2014

Choose a reason for hiding this comment

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

Ok, never mind my above mumbling. I don't know why I ended up encoding twice, this doesn't make much sense. It displays properly in viewdiff.php simply because I decode once using mb_convert_encoding before outputting the result. If a caller wants to encode twice, it is its business, but the library shouldn't be doing this. It appears I got lost somewhere in the maze of encoding/decoding.

I will remove the silly double encoding.

@elektronaut1024
Copy link

Choose a reason for hiding this comment

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

Thanks! I know (from personal experience) exactly how things like that happen...

Please sign in to comment.