Source for file CSSValidator.php
Documentation is available at CSSValidator.php
* Copyright (c) 2007, Laurent Laville <pear@laurent-laville.org>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @package Services_W3C_CSSValidator
* @author Laurent Laville <pear@laurent-laville.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @link http://pear.php.net/package/Services_W3C_CSSValidator
* @since File available since Release 0.1.0
require_once 'HTTP/Request.php';
require_once 'Services/W3C/CSSValidator/Response.php';
require_once 'Services/W3C/CSSValidator/Error.php';
require_once 'Services/W3C/CSSValidator/Warning.php';
* Base class for utilizing the W3C CSS Validator service.
* @package Services_W3C_CSSValidator
* @author Laurent Laville <pear@laurent-laville.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @link http://pear.php.net/package/Services_W3C_CSSValidator
* @since Class available since Release 0.1.0
* URI to the W3C validator.
const VALIDATOR_URI = 'http://jigsaw.w3.org/css-validator/validator';
* The URL of the document to validate
* Internally used filename of a file to upload to the validator
* POSTed as multipart/form-data
* CSS fragment to validate.
* Full documents only. At the moment, will only work if data is sent with the
* Options list (available with default values) :
* Triggers the various outputs formats of the validator. If unset,
* the usual Web html format will be sent. If set to soap12,
* the SOAP1.2 interface will be triggered.
* warning - Warning level
* Default value is '1', and value could one of these :
* <li>2</li> all warning messages
* <li>1</li> normal report
* <li>0</li> most important warning messages
* <li>no</li> none messages
* Default value is 'css21', and value could one of these :
* <li>none</li> none profile
* <li>css1</li> CSS level 1
* <li>css2</li> CSS level 2
* <li>css21</li> CSS level 2.1
* <li>css3</li> CSS level 3
* <li>svgbasic</li> SVG Basic
* <li>svgtiny</li> SVG Tiny
* <li>atsc-tv</li> ATSC TV
* usermedium - User medium
* Default value is 'all', and value could one of these :
* lang - Language used for response messages
* Default value is 'en', and value could one of these :
* en, fr, ja, es, zh-cn, nl, de
* Constructor for the class.
$this->options = array ('output' => 'soap12', 'warning' => '1',
'profile' => 'css21', 'usermedium' => 'all', 'lang' => 'en');
* Sets options for the class.
* @param string $option Name of option to set
* @param string $val Value of option to set
public function __set($option, $val)
// properties that can be set directly
static $setting_allowed = array ('uri');
if (isset ($this->options [$option])) {
$this->options [$option] = $val;
} elseif (property_exists ($this, $option)) {
if (in_array ($option, $setting_allowed)) {
* Gets options for the class.
* @param string $option Name of option to set
public function __get ($option)
// properties that can be get directly
static $getting_allowed = array ('uri');
if (isset ($this->options [$option])) {
$r = $this->options [$option];
} elseif (property_exists ($this, $option)) {
if (in_array ($option, $getting_allowed)) {
* Executes the validator using the current parameters and returns a Response
* @param string $uri The address to the page to validate ex: http://example.com/
* @return mixed object Services_W3C_CSSValidator_Response
* if web service call successfull,
* boolean FALSE otherwise
* Validates the local file
* Requests validation on the local file, from an instance of the W3C validator.
* The file is posted to the W3C validator using multipart/form-data.
* @param string $file file to be validated.
* @return mixed object Services_W3C_CSSValidator_Response
* if web service call successfull,
* boolean FALSE otherwise
* Validate an html string
* @param string $css Full css document fragment
* @return mixed object Services_W3C_CSSValidator_Response
* if web service call successfull,
* boolean FALSE otherwise
* Prepares a request object to send to the validator.
* @param string $type uri, file, or fragment
$this->request = new HTTP_Request ();
$this->request->setURL (self ::VALIDATOR_URI );
$this->request->setMethod (HTTP_REQUEST_METHOD_GET );
$this->request->addQueryString ('uri', $this->uri);
$method = 'addQueryString';
$this->request->setMethod (HTTP_REQUEST_METHOD_POST );
$this->request->setMethod (HTTP_REQUEST_METHOD_GET );
$method = 'addQueryString';
$options = array ('output', 'warning', 'profile', 'usermedium', 'lang');
foreach ($options as $option) {
if (isset ($this->options[$option])) {
* Actually sends the request to the CSS Validator service
* @return bool TRUE if request was sent successfully, FALSE otherwise
if (PEAR ::isError ($this->request->sendRequest ())) {
* Parse an XML response from the validator
* This function parses a SOAP 1.2 response xml string from the validator.
* @param string $xml The raw soap12 XML response from the validator.
* @return mixed object Services_W3C_CSSValidator_Response
* if parsing soap12 response successfully,
* boolean FALSE otherwise
$doc = new DOMDocument ();
// try to load soap 1.2 xml response, and suppress warning reports if any
if (@$doc->loadXML ($xml)) {
// Get the standard CDATA elements
$cdata = array ('uri', 'checkedby', 'csslevel', 'date');
foreach ($cdata as $var) {
$element = $doc->getElementsByTagName ($var);
$response->$var = $element->item (0 )->nodeValue;
// Handle the bool element validity
$element = $doc->getElementsByTagName ('validity');
$element->item (0 )->nodeValue == 'true') {
$response->validity = true;
$response->validity = false;
if (!$response->validity ) {
$errors = $doc->getElementsByTagName ('error');
foreach ($errors as $error) {
$warnings = $doc->getElementsByTagName ('warning');
foreach ($warnings as $warning) {
$response->addWarning (new
// Could not load the XML document
Documentation generated on Fri, 23 Nov 2007 13:00:07 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|