protected function assertConversion($input, $expect, $unit = null, $test_negative = true) { $length = HTMLPurifier_Length::make($input); if ($expect !== false) { $expectl = HTMLPurifier_Length::make($expect); } else { $expectl = false; } $to_unit = $unit !== null ? $unit : $expectl->getUnit(); $converter = new HTMLPurifier_UnitConverter(4, 10); $result = $converter->convert($length, $to_unit); if (!$result || !$expectl) { $this->assertIdentical($result, $expectl); } else { $this->assertIdentical($result->toString(), $expectl->toString()); } $converter = new HTMLPurifier_UnitConverter(4, 10, true); $result = $converter->convert($length, $to_unit); if (!$result || !$expectl) { $this->assertIdentical($result, $expectl); } else { $this->assertIdentical($result->toString(), $expectl->toString(), 'BCMath substitute: %s'); } if ($test_negative) { $this->assertConversion("-{$input}", $expect === false ? false : "-{$expect}", $unit, false); } }
/** * @param $s1 First string to compare * @param $s2 Second string to compare * @param $expect 0 for $s1 == $s2, 1 for $s1 > $s2 and -1 for $s1 < $s2 */ protected function assertComparison($s1, $s2, $expect = 0) { $l1 = HTMLPurifier_Length::make($s1); $l2 = HTMLPurifier_Length::make($s2); $r1 = $l1->compareTo($l2); $r2 = $l2->compareTo($l1); $this->assertIdentical($r1 == 0 ? 0 : ($r1 > 0 ? 1 : -1), $expect); $this->assertIdentical($r2 == 0 ? 0 : ($r2 > 0 ? 1 : -1), -$expect); }
/** * @param string $string * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @return bool|string */ public function validate($string, $config, $context) { $string = $this->parseCDATA($string); // Optimizations if ($string === '') { return false; } if ($string === '0') { return '0'; } if (strlen($string) === 1) { return false; } $length = HTMLPurifier_Length::make($string); if (!$length->isValid()) { return false; } if ($this->min) { $c = $length->compareTo($this->min); if ($c === false) { return false; } if ($c < 0) { return false; } } if ($this->max) { $c = $length->compareTo($this->max); if ($c === false) { return false; } if ($c > 0) { return false; } } return $length->toString(); }