0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

[3.1.2] Add %Output.SortAttr to deal with FCKeditor bug

If %Output.SortAttr is true, attributes are sorted to be
in alphabetical order. This was requested by frank farmer.

See also: http://htmlpurifier.org/phorum/read.php?2,1576

Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
Edward Z. Yang 2008-06-24 22:36:27 -04:00
parent 85fb192d93
commit 24f6db6fb2
6 changed files with 41 additions and 6 deletions

2
NEWS
View File

@ -12,6 +12,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
3.2.0, unknown release date 3.2.0, unknown release date
3.1.2, unknown release date 3.1.2, unknown release date
! %Output.AttrSort for when you need your attributes in alphabetical order to
deal with a bug in FCKEditor. Requested by frank farmer.
3.1.1, released 2008-06-19 3.1.1, released 2008-06-19
# %URI.Munge now, by default, does not munge resources (for example, <img src="">) # %URI.Munge now, by default, does not munge resources (for example, <img src="">)

View File

@ -96,17 +96,22 @@
</directive> </directive>
<directive id="Output.CommentScriptContents"> <directive id="Output.CommentScriptContents">
<file name="HTMLPurifier/Generator.php"> <file name="HTMLPurifier/Generator.php">
<line>40</line> <line>45</line>
</file>
</directive>
<directive id="Output.SortAttr">
<file name="HTMLPurifier/Generator.php">
<line>46</line>
</file> </file>
</directive> </directive>
<directive id="Output.TidyFormat"> <directive id="Output.TidyFormat">
<file name="HTMLPurifier/Generator.php"> <file name="HTMLPurifier/Generator.php">
<line>69</line> <line>75</line>
</file> </file>
</directive> </directive>
<directive id="Output.Newline"> <directive id="Output.Newline">
<file name="HTMLPurifier/Generator.php"> <file name="HTMLPurifier/Generator.php">
<line>83</line> <line>89</line>
</file> </file>
</directive> </directive>
<directive id="HTML.BlockWrapper"> <directive id="HTML.BlockWrapper">

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,13 @@
Output.SortAttr
TYPE: bool
VERSION: 3.1.2
DEFAULT: false
--DESCRIPTION--
<p>
If true, HTML Purifier will sort attributes by name before writing them back
to the document, converting a tag like: <code>&lt;el b="" a="" c="" /&gt;</code>
to <code>&lt;el a="" b="" c="" /&gt;</code>. This is a workaround for
a bug in FCKeditor which causes it to swap attributes order, adding noise
to text diffs. If you're not seeing this bug, chances are, you don't need
this directive.
</p>

View File

@ -26,6 +26,11 @@ class HTMLPurifier_Generator
*/ */
private $_def; private $_def;
/**
* Cache of %Output.SortAttr
*/
private $_sortAttr;
/** /**
* Configuration for the generator * Configuration for the generator
*/ */
@ -38,6 +43,7 @@ class HTMLPurifier_Generator
public function __construct($config, $context) { public function __construct($config, $context) {
$this->config = $config; $this->config = $config;
$this->_scriptFix = $config->get('Output', 'CommentScriptContents'); $this->_scriptFix = $config->get('Output', 'CommentScriptContents');
$this->_sortAttr = $config->get('Output', 'SortAttr');
$this->_def = $config->getHTMLDefinition(); $this->_def = $config->getHTMLDefinition();
$this->_xhtml = $this->_def->doctype->xml; $this->_xhtml = $this->_def->doctype->xml;
} }
@ -142,6 +148,7 @@ class HTMLPurifier_Generator
*/ */
public function generateAttributes($assoc_array_of_attributes, $element = false) { public function generateAttributes($assoc_array_of_attributes, $element = false) {
$html = ''; $html = '';
if ($this->_sortAttr) ksort($assoc_array_of_attributes);
foreach ($assoc_array_of_attributes as $key => $value) { foreach ($assoc_array_of_attributes as $key => $value) {
if (!$this->_xhtml) { if (!$this->_xhtml) {
// Remove namespaced attributes // Remove namespaced attributes

View File

@ -209,7 +209,6 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
} }
function test_generateFromTokens_XHTMLoff() { function test_generateFromTokens_XHTMLoff() {
$this->config = HTMLPurifier_Config::createDefault();
$this->config->set('HTML', 'XHTML', false); $this->config->set('HTML', 'XHTML', false);
// omit trailing slash // omit trailing slash
@ -237,7 +236,6 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
// just don't test; Tidy is exploding on me. // just don't test; Tidy is exploding on me.
return; return;
$this->config = HTMLPurifier_Config::createDefault();
$this->config->set('Core', 'TidyFormat', true); $this->config->set('Core', 'TidyFormat', true);
$this->config->set('Output', 'Newline', "\n"); $this->config->set('Output', 'Newline', "\n");
@ -253,5 +251,15 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
} }
function test_generateFromTokens_sortAttr() {
$this->config->set('Output', 'SortAttr', true);
$this->assertGeneration(
array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
'<p a="d" b="c">'
);
}
} }