Same name in other branches
- 5.0.x advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
- 6.0.x advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
- 8.x-2.x advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
- 8.x-3.x advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
Configure form for W3C validation of CSS files.
Hierarchy
- class \Drupal\advagg\Form\AdvaggFormBase extends \Drupal\Core\Form\ConfigFormBase
- class \Drupal\advagg_validator\Form\BaseValidatorForm extends \Drupal\advagg\Form\AdvaggFormBase
- class \Drupal\advagg_validator\Form\CssW3Form extends \Drupal\advagg_validator\Form\BaseValidatorForm uses \Drupal\advagg\AdvaggSettersTrait
- class \Drupal\advagg_validator\Form\BaseValidatorForm extends \Drupal\advagg\Form\AdvaggFormBase
Expanded class hierarchy of CssW3Form
1 file declares its use of CssW3Form
- CssW3FormTest.php dans advagg_validator/
tests/ src/ Kernel/ Form/ CssW3FormTest.php
1 string reference to 'CssW3Form'
- advagg_validator.routing.yml dans advagg_validator/
advagg_validator.routing.yml - advagg_validator/advagg_validator.routing.yml
Fichier
-
advagg_validator/
src/ Form/ CssW3Form.php, line 14
Namespace
Drupal\advagg_validator\FormView source
class CssW3Form extends BaseValidatorForm {
use AdvaggSettersTrait;
/**
* The Guzzle HTTP Client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The Drupal renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/**
* @var \Drupal\advagg_validator\Form\CssW3Form
*/
$instance = parent::create($container);
$instance->setRequestStack($container->get('request_stack'));
$instance->setHttpClient($container->get('http_client'));
$instance->setRenderer($container->get('renderer'));
return $instance;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'advagg_validator_cssw3';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::generateForm('css', FALSE);
$form['notice'] = [
'#markup' => '<div>' . $this->t('Notice: The form below will submit files to the <a href="http://jigsaw.w3.org/css-validator/">http://jigsaw.w3.org/css-validator/</a> service if used.') . '</div>',
'#weight' => -1,
];
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitCheckAll(array &$form, FormStateInterface $form_state) {
$dir = $form_state->getTriggeringElement()['#name'];
$files = [];
foreach ($form_state->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
// Check list.
$info = $this->testFiles($files);
$info = $this->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
$this->messenger()
->addMessage($this->renderer
->render($output));
}
/**
* Display validation info via ajax callback.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function ajaxCheck(array &$form, FormStateInterface $form_state) {
$dir = $form_state->getTriggeringElement()['#name'];
return $this->getElement($form, explode('/', $dir))['wrapper'];
}
/**
* {@inheritdoc}
*/
public function submitCheckDirectory(array &$form, FormStateInterface $form_state) {
$dir = $form_state->getTriggeringElement()['#name'];
$files = [];
$slash_count = substr_count('/' . $dir, '/');
foreach ($form_state->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || substr_count($value, '/') > $slash_count || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
// Check list.
$info = $this->testFiles($files);
$info = $this->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
$this->messenger()
->addMessage($this->renderer
->render($output));
}
/**
* {@inheritdoc}
*/
protected function testFiles(array $files, array $options = []) {
$output = [];
foreach ($files as $filename) {
// Skip missing files.
if (!file_exists($filename)) {
continue;
}
$lines = file($filename);
// Run jigsaw.w3.org validator.
$output[$filename]['jigsaw.w3.org'] = $this->testW3C($filename, $options);
// Get extra context for errors.
if (!empty($output[$filename]['jigsaw.w3.org']['errors'])) {
foreach ($output[$filename]['jigsaw.w3.org']['errors'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
if (!empty($output[$filename]['jigsaw.w3.org']['warnings'])) {
foreach ($output[$filename]['jigsaw.w3.org']['warnings'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
}
return $output;
}
/**
* Given a CSS file, test to make sure it is valid CSS.
*
* @param string $filename
* The name of the file.
* @param array $validator_options
* List of options to pass along to the CSS Validator.
*
* @return array
* Info from the w3c server.
*/
private function testW3C($filename, array &$validator_options = []) {
// Get CSS files contents.
$validator_options['text'] = file_get_contents($filename);
if (strlen($validator_options['text']) > 50000) {
unset($validator_options['text']);
$validator_options['uri'] = $this->requestStack
->getCurrentRequest()
->getBaseUrl() . $filename;
}
// Add in defaults.
$validator_options += [
'output' => 'soap12',
'warning' => '1',
'profile' => 'css3',
'usermedium' => 'all',
'lang' => 'en',
];
// Build request URL.
// API Documentation http://jigsaw.w3.org/css-validator/api.html
$request_url = 'http://jigsaw.w3.org/css-validator/validator';
$query = http_build_query($validator_options, '', '&');
$url = $request_url . '?' . $query;
try {
$data = $this->httpClient
->get($url)
->getBody();
} catch (RequestException $e) {
watchdog_exception('AdvAgg Validator', $e);
} catch (\Exception $e) {
watchdog_exception('AdvAgg Validator', $e);
}
if (!empty($data)) {
// Parse XML and return info.
$return = $this->parseSoapResponse($data);
$return['filename'] = $filename;
if (isset($validator_options['text'])) {
unset($validator_options['text']);
}
elseif (isset($validator_options['uri'])) {
unset($validator_options['uri']);
}
$return['options'] = $validator_options;
return $return;
}
return [
'error' => $this->t('W3C Server did not return a 200 or request data was empty.'),
];
}
/**
* {@inheritdoc}
*/
private function parseSoapResponse($xml) {
$doc = new DOMDocument();
$response = [];
// Try to load soap 1.2 XML response, and suppress warning reports if any.
if (!@$doc->loadXML($xml)) {
// Could not load the XML document.
return $response;
}
// Get the standard CDATA elements.
$cdata = [
'uri',
'checkedby',
'csslevel',
'date',
];
foreach ($cdata as $var) {
$element = $doc->getElementsByTagName($var);
if ($element->length) {
$response[$var] = $element->item(0)->nodeValue;
}
}
// Handle the element validity and get errors if not valid.
$element = $doc->getElementsByTagName('validity');
if ($element->length && $element->item(0)->nodeValue === 'true') {
$response['validity'] = TRUE;
}
else {
$response['validity'] = FALSE;
$errors = $doc->getElementsByTagName('error');
foreach ($errors as $error) {
$response['errors'][] = $this->domExtractor($error);
}
}
// Get warnings.
$warnings = $doc->getElementsByTagName('warning');
foreach ($warnings as $warning) {
$response['warnings'][] = $this->domExtractor($warning);
}
// Return response array.
return $response;
}
}
Members
Titre Trier par ordre décroissant | Modifiers | Object type | Résumé | Overriden Title | Overrides |
---|---|---|---|---|---|
AdvaggFormBase::isAjax | protected | function | Checks if the form was submitted by AJAX. | ||
AdvaggSettersTrait::$cache | protected | property | The AdvAgg cache. | 3 | |
AdvaggSettersTrait::$cssCollectionOptimizer | protected | property | The CSS asset collection optimizer service. | ||
AdvaggSettersTrait::$dateFormatter | protected | property | The date formatter service. | 1 | |
AdvaggSettersTrait::$fileSystem | protected | property | The File System service. | ||
AdvaggSettersTrait::$jsCollectionOptimizer | protected | property | The JavaScript asset collection optimizer service. | ||
AdvaggSettersTrait::$languageManager | protected | property | The core language manager service. | 1 | |
AdvaggSettersTrait::$moduleHandler | protected | property | The module handler service. | 1 | |
AdvaggSettersTrait::$privateKey | protected | property | The private key service. | ||
AdvaggSettersTrait::$state | protected | property | The state service. | ||
AdvaggSettersTrait::$themeRegistry | protected | property | The theme registry service. | 1 | |
AdvaggSettersTrait::$time | protected | property | Obtaining system time. | ||
AdvaggSettersTrait::setCache | public | function | The set cache backend. | ||
AdvaggSettersTrait::setCssCollectionOptimizer | public | function | Set CSS asset collection optimizer service. | ||
AdvaggSettersTrait::setDateFomatter | public | function | Set date formatter service. | ||
AdvaggSettersTrait::setFileSystem | public | function | Set File System service. | ||
AdvaggSettersTrait::setHttpClient | public | function | Set http client. | ||
AdvaggSettersTrait::setJsCollectionOptimizer | public | function | Set JavaScript asset collection optimizer service. | ||
AdvaggSettersTrait::setLanguageManager | public | function | Set language manager service. | ||
AdvaggSettersTrait::setModuleHandler | public | function | Set module handler. | ||
AdvaggSettersTrait::setPrivateKey | public | function | Set private key service. | ||
AdvaggSettersTrait::setRenderer | public | function | Set renderer. | ||
AdvaggSettersTrait::setState | public | function | Set state service. | ||
AdvaggSettersTrait::setThemeRegistry | public | function | The theme registry service. | ||
AdvaggSettersTrait::setTime | public | function | Obtaining system time. | ||
BaseValidatorForm::$streamWrapperManager | protected | property | The StreamWrapper manage. | ||
BaseValidatorForm::domExtractor | protected | function | Extract info from the DOMNode Object. | ||
BaseValidatorForm::generateForm | public | function | Generate a hierarchical form sorted by path from asset files. | ||
BaseValidatorForm::getEditableConfigNames | protected | function | |||
BaseValidatorForm::getElement | protected | function | Get array element that corresponds to directory. | ||
BaseValidatorForm::hideGoodFiles | protected | function | Do not display info on a file if it is valid. | ||
BaseValidatorForm::scanAllDirs | protected | function | Recursively scan the drupal webroot for files matching the given extension. | ||
BaseValidatorForm::scanDirectory | protected | function | Finds all files that match a given mask in a given directory. | ||
CssW3Form::$httpClient | protected | property | The Guzzle HTTP Client. | Overrides AdvaggSettersTrait::$httpClient | |
CssW3Form::$renderer | protected | property | The Drupal renderer. | Overrides AdvaggSettersTrait::$renderer | |
CssW3Form::ajaxCheck | public | function | Display validation info via ajax callback. | ||
CssW3Form::buildForm | public | function | |||
CssW3Form::create | public static | function | Overrides BaseValidatorForm::create | ||
CssW3Form::getFormId | public | function | |||
CssW3Form::parseSoapResponse | private | function | |||
CssW3Form::submitCheckAll | public | function | |||
CssW3Form::submitCheckDirectory | public | function | |||
CssW3Form::testFiles | protected | function | Perform server side test(s) on all given files. | Overrides BaseValidatorForm::testFiles | |
CssW3Form::testW3C | private | function | Given a CSS file, test to make sure it is valid CSS. |