diff --git a/docs/progress.html b/docs/progress.html index 9400314b..80c6a9b1 100644 --- a/docs/progress.html +++ b/docs/progress.html @@ -149,7 +149,7 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;} floatENUM(left, right, none), May require layout precautions with clear fontSHORTHAND -font-familyCSS validator may complain if fallback font +font-familyCSS validator may complain if fallback font family not specified font-sizeCOMPOSITE(<absolute-size>, <relative-size>, <length>, <percentage>) diff --git a/library/HTMLPurifier/AttrDef/FontFamily.php b/library/HTMLPurifier/AttrDef/FontFamily.php new file mode 100644 index 00000000..bb49d6da --- /dev/null +++ b/library/HTMLPurifier/AttrDef/FontFamily.php @@ -0,0 +1,59 @@ + true, + 'sans-serif' => true, + 'monospace' => true, + 'fantasy' => true, + 'cursive' => true + ); + + function validate($string, $config, &$context) { + $string = $this->parseCDATA($string); + // assume that no font names contain commas in them + $fonts = explode(',', $string); + $final = ''; + foreach($fonts as $font) { + $font = trim($font); + if ($font === '') continue; + // match a generic name + if (isset($this->generic_names[$font])) { + $final .= $font . ', '; + continue; + } + // match a quoted name + if ($font[0] === '"' || $font[0] === "'") { + $length = strlen($font); + if ($length <= 2) continue; + $quote = $font[0]; + if ($font[$length - 1] !== $quote) continue; + $font = substr($font, 1, $length - 2); + } + // process font + if (ctype_alnum($font)) { + // very simple font, allow it in unharmed + $final .= $font . ', '; + continue; + } + $nospace = str_replace(array(' ', '.', '!'), '', $font); + if (ctype_alnum($nospace)) { + // font with spaces in it + $final .= "'$font', "; + continue; + } + } + $final = rtrim($final, ', '); + if ($final === '') return false; + return $final; + } + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/CSSDefinition.php b/library/HTMLPurifier/CSSDefinition.php index 138a54eb..12dc62b6 100644 --- a/library/HTMLPurifier/CSSDefinition.php +++ b/library/HTMLPurifier/CSSDefinition.php @@ -7,6 +7,7 @@ require_once 'HTMLPurifier/AttrDef/CSSLength.php'; require_once 'HTMLPurifier/AttrDef/Percentage.php'; require_once 'HTMLPurifier/AttrDef/Multiple.php'; require_once 'HTMLPurifier/AttrDef/TextDecoration.php'; +require_once 'HTMLPurifier/AttrDef/FontFamily.php'; class HTMLPurifier_CSSDefinition { @@ -143,6 +144,8 @@ class HTMLPurifier_CSSDefinition $this->info['text-decoration'] = new HTMLPurifier_AttrDef_TextDecoration(); + $this->info['font-family'] = new HTMLPurifier_AttrDef_FontFamily(); + // this could use specialized code $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum( array('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', diff --git a/tests/HTMLPurifier/AttrDef/CSSTest.php b/tests/HTMLPurifier/AttrDef/CSSTest.php index d1570c44..897c09e4 100644 --- a/tests/HTMLPurifier/AttrDef/CSSTest.php +++ b/tests/HTMLPurifier/AttrDef/CSSTest.php @@ -58,6 +58,8 @@ class HTMLPurifier_AttrDef_CSSTest extends HTMLPurifier_AttrDefHarness $this->assertDef('width:auto;'); $this->assertDef('width:-50px;', false); $this->assertDef('text-decoration:underline;'); + $this->assertDef('font-family:sans-serif;'); + $this->assertDef('font-family:Gill, \'Times New Roman\', sans-serif;'); // duplicates $this->assertDef('text-align:right;text-align:left;', diff --git a/tests/HTMLPurifier/AttrDef/FontFamilyTest.php b/tests/HTMLPurifier/AttrDef/FontFamilyTest.php new file mode 100644 index 00000000..47c0e779 --- /dev/null +++ b/tests/HTMLPurifier/AttrDef/FontFamilyTest.php @@ -0,0 +1,24 @@ +def = new HTMLPurifier_AttrDef_FontFamily(); + + $this->assertDef('Gill, Helvetica, sans-serif'); + $this->assertDef('\'Times New Roman\', serif'); + $this->assertDef('"Times New Roman"', "'Times New Roman'"); + $this->assertDef('01234'); + $this->assertDef(',', false); + $this->assertDef('Times New Roman, serif', '\'Times New Roman\', serif'); + + } + +} + +?> \ No newline at end of file diff --git a/tests/index.php b/tests/index.php index f46fda73..eb751ec5 100644 --- a/tests/index.php +++ b/tests/index.php @@ -72,6 +72,7 @@ $test_files[] = 'AttrDef/CSSLengthTest.php'; $test_files[] = 'AttrDef/PercentageTest.php'; $test_files[] = 'AttrDef/MultipleTest.php'; $test_files[] = 'AttrDef/TextDecorationTest.php'; +$test_files[] = 'AttrDef/FontFamilyTest.php'; $test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'TagTransformTest.php'; $test_files[] = 'AttrTransform/LangTest.php';