Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom styles from 1.x can crash the app #4159

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trim_trailing_whitespace = false
[*.{php,xml,json}]
indent_size = 4

[{tsconfig.json,prettierrc.json}]
[{tsconfig.json,prettierrc.json,package.json}]
indent_size = 2

[*.neon]
Expand Down
14 changes: 13 additions & 1 deletion framework/core/js/src/admin/components/EditCustomCssModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import app from '../../admin/app';
import SettingsModal from './SettingsModal';
import SettingsModal, { type ISettingsModalAttrs } from './SettingsModal';
import Mithril from 'mithril';

export default class EditCustomCssModal extends SettingsModal {
oninit(vnode: Mithril.Vnode<ISettingsModalAttrs, this>) {
super.oninit(vnode);

if (this.setting('custom_less_error')()) {
this.alertAttrs = {
type: 'error',
content: this.setting('custom_less_error')(),
};
}
}

className() {
return 'EditCustomCssModal TextareaCodeModal Modal--large';
}
Expand Down
8 changes: 5 additions & 3 deletions framework/core/src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function register(): void
$sources->addFile(__DIR__.'/../../less/forum.less');
$sources->addString(function () use ($container) {
return $container->make(SettingsRepositoryInterface::class)->get('custom_less', '');
});
}, 'custom_less');
});

$container->make(AddTranslations::class)->forFrontend('forum')->to($assets);
Expand Down Expand Up @@ -195,7 +195,8 @@ function (Saved $event) use ($container) {
$container->make('flarum.assets.forum'),
$container->make('flarum.locales'),
$container,
$container->make('flarum.less.config')
$container->make(SettingsRepositoryInterface::class),
$container->make('flarum.less.config'),
);
$validator->whenSettingsSaved($event);
}
Expand All @@ -208,7 +209,8 @@ function (Saving $event) use ($container) {
$container->make('flarum.assets.forum'),
$container->make('flarum.locales'),
$container,
$container->make('flarum.less.config')
$container->make(SettingsRepositoryInterface::class),
$container->make('flarum.less.config'),
);
$validator->whenSettingsSaving($event);
}
Expand Down
9 changes: 8 additions & 1 deletion framework/core/src/Forum/ValidateCustomLess.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function __construct(
protected Assets $assets,
protected LocaleManager $locales,
protected Container $container,
protected array $customLessSettings = []
protected SettingsRepositoryInterface $settings,
protected array $customLessSettings = [],
) {
}

Expand Down Expand Up @@ -72,6 +73,8 @@ function ($settings) use ($event) {
$adapter = new InMemoryFilesystemAdapter();
$this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem($adapter), $adapter));

$this->settings->delete('custom_less_error');

try {
$this->assets->makeCss()->commit();

Expand All @@ -82,6 +85,10 @@ function ($settings) use ($event) {
throw new ValidationException(['custom_less' => $e->getMessage()]);
}

if (! empty($this->settings->get('custom_less_error'))) {
throw new ValidationException(['custom_less' => $this->settings->get('custom_less_error')]);
}

$this->assets->setAssetsDir($assetsDir);
$this->container->instance(SettingsRepositoryInterface::class, $settings);
}
Expand Down
27 changes: 26 additions & 1 deletion framework/core/src/Frontend/Compiler/LessCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Frontend\Compiler\Source\FileSource;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Less_Exception_Compiler;
use Less_FileManager;
use Less_Parser;
use Less_Tree_Import;
Expand Down Expand Up @@ -71,6 +72,10 @@ protected function compile(array $sources): string
return '';
}

if (! empty($this->settings->get('custom_less_error'))) {
unset($sources['custom_less']);
}

ini_set('xdebug.max_nesting_level', '200');

$parser = new Less_Parser([
Expand All @@ -96,7 +101,27 @@ protected function compile(array $sources): string
$parser->registerFunction($name, $callback);
}

return $this->finalize($parser->getCss());
try {
$compiled = $this->finalize($parser->getCss());

if (isset($sources['custom_less'])) {
$this->settings->delete('custom_less_error');
}

return $compiled;
} catch (Less_Exception_Compiler $e) {
if (isset($sources['custom_less'])) {
unset($sources['custom_less']);

$compiled = $this->compile($sources);

$this->settings->set('custom_less_error', $e->getMessage());

return $compiled;
}

throw $e;
}
}

protected function finalize(string $parsedCss): string
Expand Down
2 changes: 2 additions & 0 deletions framework/core/src/Frontend/Compiler/RevisionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Frontend\Compiler\Source\FileSource;
use Flarum\Frontend\Compiler\Source\SourceInterface;
use Flarum\Frontend\Compiler\Source\StringSource;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;

/**
Expand All @@ -29,6 +30,7 @@ class RevisionCompiler implements CompilerInterface
public function __construct(
protected Cloud $assetsDir,
protected string $filename,
protected SettingsRepositoryInterface $settings
) {
$this->versioner = new FileVersioner($assetsDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ public function addFile(string $file, ?string $extensionId = null): static
return $this;
}

public function addString(Closure $callback): static
public function addString(Closure $callback, ?string $key = null): static
{
$this->sources[] = $this->validateSourceType(
$source = $this->validateSourceType(
new StringSource($callback)
);

if (! empty($key)) {
$this->sources[$key] = $source;
} else {
$this->sources[] = $source;
}

return $this;
}

Expand Down
Loading