Issue #3167034: CKeditor support
parent
5b41c50453
commit
cf8181e886
|
@ -852,6 +852,8 @@ function template_preprocess_image(&$variables) {
|
|||
}
|
||||
}
|
||||
|
||||
// Without dimensions specified, layout shifts can occur,
|
||||
// which are more noticeable on pages that take some time to load.
|
||||
if (isset($variables['width'], $variables['height'])) {
|
||||
$variables['attributes']['loading'] = 'lazy';
|
||||
}
|
||||
|
|
|
@ -79,6 +79,14 @@ class EditorFileReference extends FilterBase implements ContainerFactoryPluginIn
|
|||
$file = $this->entityRepository->loadEntityByUuid('file', $uuid);
|
||||
if ($file instanceof FileInterface) {
|
||||
$node->setAttribute('src', $file->createFileUrl());
|
||||
if ($node->nodeName == 'img') {
|
||||
// Without dimensions specified, layout shifts can occur,
|
||||
// which are more noticeable on pages that take some time to load.
|
||||
list($width, $height) = getimagesize($file->getFileUri());
|
||||
$node->setAttribute('width', $width);
|
||||
$node->setAttribute('height', $height);
|
||||
$node->setAttribute('loading', 'lazy');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
namespace Drupal\Tests\editor\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\File\FileSystemInterface;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\filter\FilterPluginCollection;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\Tests\TestFileCreationTrait;
|
||||
|
||||
/**
|
||||
* Tests Editor module's file reference filter.
|
||||
|
@ -14,6 +16,8 @@ use Drupal\KernelTests\KernelTestBase;
|
|||
*/
|
||||
class EditorFileReferenceFilterTest extends KernelTestBase {
|
||||
|
||||
use TestFileCreationTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
|
@ -57,14 +61,29 @@ class EditorFileReferenceFilterTest extends KernelTestBase {
|
|||
return $filter->process($input, 'und');
|
||||
};
|
||||
|
||||
file_put_contents('public://llama.jpg', $this->randomMachineName());
|
||||
/** @var array stdClass */
|
||||
$files = $this->getTestFiles('image');
|
||||
list($width, $height) = getimagesize($files[0]->uri);
|
||||
$images[] = [
|
||||
'uri' => $files[0]->uri,
|
||||
'dimensions' => 'width="' . $width . '" height="' . $height . '"',
|
||||
];
|
||||
list($width, $height) = getimagesize($files[1]->uri);
|
||||
$images[] = [
|
||||
'uri' => $files[1]->uri,
|
||||
'dimensions' => 'width="' . $width . '" height="' . $height . '"',
|
||||
];
|
||||
|
||||
unset($files);
|
||||
|
||||
\Drupal::service('file_system')->copy($images[0]['uri'], 'public://llama.jpg', FileSystemInterface::EXISTS_RENAME);
|
||||
$image = File::create(['uri' => 'public://llama.jpg']);
|
||||
$image->save();
|
||||
$id = $image->id();
|
||||
$uuid = $image->uuid();
|
||||
$cache_tag = ['file:' . $id];
|
||||
|
||||
file_put_contents('public://alpaca.jpg', $this->randomMachineName());
|
||||
\Drupal::service('file_system')->copy($images[1]['uri'], 'public://alpaca.jpg', FileSystemInterface::EXISTS_RENAME);
|
||||
$image_2 = File::create(['uri' => 'public://alpaca.jpg']);
|
||||
$image_2->save();
|
||||
$id_2 = $image_2->id();
|
||||
|
@ -83,23 +102,23 @@ class EditorFileReferenceFilterTest extends KernelTestBase {
|
|||
|
||||
// One data-entity-uuid attribute.
|
||||
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" ' . $images[0]['dimensions'] . ' loading="lazy" />';
|
||||
$output = $test($input);
|
||||
$this->assertIdentical($expected_output, $output->getProcessedText());
|
||||
$this->assertIdentical($output->getProcessedText(), $expected_output);
|
||||
$this->assertEqual($cache_tag, $output->getCacheTags());
|
||||
|
||||
// One data-entity-uuid attribute with odd capitalization.
|
||||
$input = '<img src="llama.jpg" data-entity-type="file" DATA-entity-UUID = "' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" ' . $images[0]['dimensions'] . ' loading="lazy" />';
|
||||
$output = $test($input);
|
||||
$this->assertIdentical($expected_output, $output->getProcessedText());
|
||||
$this->assertIdentical($output->getProcessedText(), $expected_output);
|
||||
$this->assertEqual($cache_tag, $output->getCacheTags());
|
||||
|
||||
// One data-entity-uuid attribute on a non-image tag.
|
||||
$input = '<video src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<video src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '"></video>';
|
||||
$output = $test($input);
|
||||
$this->assertIdentical($expected_output, $output->getProcessedText());
|
||||
$this->assertIdentical($output->getProcessedText(), $expected_output);
|
||||
$this->assertEqual($cache_tag, $output->getCacheTags());
|
||||
|
||||
// One data-entity-uuid attribute with an invalid value.
|
||||
|
@ -111,19 +130,19 @@ class EditorFileReferenceFilterTest extends KernelTestBase {
|
|||
// Two different data-entity-uuid attributes.
|
||||
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$input .= '<img src="alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output .= '<img src="/' . $this->siteDirectory . '/files/alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" ' . $images[0]['dimensions'] . ' loading="lazy" />';
|
||||
$expected_output .= '<img src="/' . $this->siteDirectory . '/files/alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" ' . $images[1]['dimensions'] . ' loading="lazy" />';
|
||||
$output = $test($input);
|
||||
$this->assertIdentical($expected_output, $output->getProcessedText());
|
||||
$this->assertIdentical($output->getProcessedText(), $expected_output);
|
||||
$this->assertEqual(Cache::mergeTags($cache_tag, $cache_tag_2), $output->getCacheTags());
|
||||
|
||||
// Two identical data-entity-uuid attributes.
|
||||
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$input .= '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output .= '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
|
||||
$expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" ' . $images[0]['dimensions'] . ' loading="lazy" />';
|
||||
$expected_output .= '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" ' . $images[0]['dimensions'] . ' loading="lazy" />';
|
||||
$output = $test($input);
|
||||
$this->assertIdentical($expected_output, $output->getProcessedText());
|
||||
$this->assertIdentical($output->getProcessedText(), $expected_output);
|
||||
$this->assertEqual($cache_tag, $output->getCacheTags());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue