Skip to content

Commit

Permalink
Added support for <style> tag processing and fixed index file not cor…
Browse files Browse the repository at this point in the history
…rectly identified. (#11)

* Added support for <style> tag processing.

* Fixed index file not correctly identified.

* Fixed formatter link title not being using description.

* Fixed cleanup of meta charset.

* Fixed URL encoding.
  • Loading branch information
Alex Skrypnyk authored Dec 5, 2019
1 parent e9577e7 commit 6c80287
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
17 changes: 9 additions & 8 deletions minisite.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ function template_preprocess_minisite_link(array &$variables) {
// Fix in https://www.drupal.org/node/2646744.
$variables['#cache']['contexts'][] = 'url.site';

$link_text = $file_entity->getFilename();

// Asset path is provided.
if (isset($variables['asset_path'])) {
// Asset path is a file.
Expand All @@ -100,6 +102,11 @@ function template_preprocess_minisite_link(array &$variables) {
else {
$url = Url::fromUserInput($variables['asset_path'])->toString();
}

// Use the description as the link text if available.
if (!empty($variables['description'])) {
$link_text = $variables['description'];
}
}
// Falling back to the archive file.
else {
Expand All @@ -122,14 +129,8 @@ function template_preprocess_minisite_link(array &$variables) {
$variables['attributes']->addClass($classes);
}

// Use the description as the link text if available.
if (empty($variables['description'])) {
$link_text = $file_entity->getFilename();
}
else {
$link_text = $variables['description'];
$options['attributes']['title'] = $file_entity->getFilename();
}
// Set link title.
$options['attributes']['title'] = $link_text;

$variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUserInput($url, $options));
}
9 changes: 8 additions & 1 deletion src/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,14 @@ public function getLanguage() {
* {@inheritdoc}
*/
public function isIndex() {
return UrlValidator::urlIsIndex($this->urlBag->getUri(), self::INDEX_FILE);
if (!UrlValidator::urlIsIndex($this->urlBag->getUri(), self::INDEX_FILE)) {
return FALSE;
}

$path = $this->urlBag->getPathInArchive();
$in_root = strpos($path, DIRECTORY_SEPARATOR) == FALSE;

return $in_root;
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/PageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function process() {
foreach ($this->document->getElementsByTagName('script') as $item) {
$this->processTagScript($item);
}

foreach ($this->document->getElementsByTagName('style') as $item) {
$this->processTagStyle($item);
}
}

/**
Expand Down Expand Up @@ -198,6 +202,39 @@ protected function processTagScript(\DOMNode $item) {
$item->setAttribute('src', $url);
}

/**
* Process <style> tag.
*
* @param \DOMNode $item
* Document node object to process.
*/
protected function processTagStyle(\DOMNode $item) {
$content = $item->textContent;

// Replace imported styles.
preg_match_all('/@import url\(([^)]+)\)/i', $content, $matches, PREG_SET_ORDER);

if (empty($matches)) {
return;
}

foreach ($matches as $match) {
if (count($match) != 2) {
continue;
}

$url = $match[1];

$url = self::urlExtractPath($url);
$url = UrlValidator::relativeToRoot($url, $this->urlBag->getAssetDir() . '/' . $this->urlBag->getRootDir());

$str = str_replace($match[1], $url, $match[0]);
$content = str_replace($match[0], $str, $content);
}

$item->textContent = $content;
}

/**
* Process <img> tag.
*
Expand Down Expand Up @@ -231,6 +268,8 @@ protected function loadDocument($content) {

$document = new \DOMDocument();

$content = $this->cleanupContent($content);

$loaded = $document->loadHTML($content);
if (!$loaded || empty($document) || empty($document->textContent)) {
throw new PageProcessorException(sprintf('Unable to parse document: %s', libxml_get_last_error()));
Expand All @@ -239,6 +278,16 @@ protected function loadDocument($content) {
return $document;
}

/**
* Cleanup content of the page before loading it int internal document.
*/
protected function cleanupContent($content) {
$content = preg_replace('/\<meta\s+http-equiv\s*=\s*\"content-type\"\s+content\s*=\s*\".*charset=ISO-8859-1\"\s*(\/?)\>/i', '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">', $content);
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");

return $content;
}

/**
* Extract path from the URL.
*
Expand Down
2 changes: 2 additions & 0 deletions src/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public static function relativeToRoot($url, $parent) {
$url = str_replace('../', '', $url);
$url = rtrim($parent, '/') . '/' . ltrim($url, '/');
$url = \Drupal::service('stream_wrapper_manager')->isValidUri($url) ? file_url_transform_relative(file_create_url($url)) : $url;
// Decode URL encoded in file_create_url().
$url = rawurldecode($url);
$url = '/' . ltrim($url, '/');

return $url;
Expand Down
32 changes: 32 additions & 0 deletions tests/src/Functional/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,36 @@ public function dataProviderAssetFromValues() {
];
}

/**
* Test Asset::isIndex().
*
* @dataProvider dataProviderIsIndex
* @covers \Drupal\minisite\Asset::isIndex
*/
public function testIsIndex($path, $is_index) {
$instance = new Asset(
'node',
$this->contentType,
1,
2,
Language::LANGCODE_DEFAULT,
'field_minisite_test',
$path
);

$this->assertEqual($instance->isIndex(), $is_index);
}

/**
* Data provider for testIsIndex.
*/
public function dataProviderIsIndex() {
return [
['public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/index.html', TRUE],
['public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/page.html', FALSE],
['public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/index.html', FALSE],
['public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page.html', FALSE],
];
}

}

0 comments on commit 6c80287

Please sign in to comment.