mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-56488 boost: validate scss settings for parsable scss
* Parses the scss and fails validation if a parse error is found * Does not detect the situation when variables are not present - this would involve us parsing the entire tree and would be slow. It could also change over time, depending on whats defined in the scss from themes. * Introduces a new admin_setting_scsscode to do this
This commit is contained in:
parent
c4cf1c60f5
commit
0e34e46fd0
3 changed files with 41 additions and 2 deletions
|
@ -969,6 +969,7 @@ $string['roleswithexceptions'] = '{$a->roles}, with {$a->exceptions}';
|
||||||
$string['rssglobaldisabled'] = 'Disabled at server level';
|
$string['rssglobaldisabled'] = 'Disabled at server level';
|
||||||
$string['save'] = 'Save';
|
$string['save'] = 'Save';
|
||||||
$string['savechanges'] = 'Save changes';
|
$string['savechanges'] = 'Save changes';
|
||||||
|
$string['scssinvalid'] = 'SCSS code is not valid, fails with: {$a}';
|
||||||
$string['search'] = 'Search';
|
$string['search'] = 'Search';
|
||||||
$string['searchalldeleted'] = 'All indexed contents have been deleted';
|
$string['searchalldeleted'] = 'All indexed contents have been deleted';
|
||||||
$string['searchareaenabled'] = 'Search area enabled';
|
$string['searchareaenabled'] = 'Search area enabled';
|
||||||
|
|
|
@ -10180,3 +10180,41 @@ class admin_setting_searchsetupinfo extends admin_setting {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to validate the contents of SCSS code and ensuring they are parsable.
|
||||||
|
*
|
||||||
|
* It does not attempt to detect undefined SCSS variables because it is designed
|
||||||
|
* to be used without knowledge of other config/scss included.
|
||||||
|
*
|
||||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
|
* @copyright 2016 Dan Poltawski <dan@moodle.com>
|
||||||
|
*/
|
||||||
|
class admin_setting_scsscode extends admin_setting_configtextarea {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate the contents of the SCSS to ensure its parsable. Does not
|
||||||
|
* attempt to detect undefined scss variables.
|
||||||
|
*
|
||||||
|
* @param string $data The scss code from text field.
|
||||||
|
* @return mixed bool true for success or string:error on failure.
|
||||||
|
*/
|
||||||
|
public function validate($data) {
|
||||||
|
if (empty($data)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$scss = new core_scss();
|
||||||
|
try {
|
||||||
|
$scss->compile($data);
|
||||||
|
} catch (Leafo\ScssPhp\Exception\ParserException $e) {
|
||||||
|
return get_string('scssinvalid', 'admin', $e->getMessage());
|
||||||
|
} catch (Leafo\ScssPhp\Exception\CompilerException $e) {
|
||||||
|
// Silently ignore this - it could be a scss variable defined from somewhere
|
||||||
|
// else which we are not examining here.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -73,13 +73,13 @@ if ($ADMIN->fulltree) {
|
||||||
$page = new admin_settingpage('theme_boost_advanced', get_string('advancedsettings', 'theme_boost'));
|
$page = new admin_settingpage('theme_boost_advanced', get_string('advancedsettings', 'theme_boost'));
|
||||||
|
|
||||||
// Raw SCSS to include before the content.
|
// Raw SCSS to include before the content.
|
||||||
$setting = new admin_setting_configtextarea('theme_boost/scsspre',
|
$setting = new admin_setting_scsscode('theme_boost/scsspre',
|
||||||
get_string('rawscsspre', 'theme_boost'), get_string('rawscsspre_desc', 'theme_boost'), '', PARAM_RAW);
|
get_string('rawscsspre', 'theme_boost'), get_string('rawscsspre_desc', 'theme_boost'), '', PARAM_RAW);
|
||||||
$setting->set_updatedcallback('theme_reset_all_caches');
|
$setting->set_updatedcallback('theme_reset_all_caches');
|
||||||
$page->add($setting);
|
$page->add($setting);
|
||||||
|
|
||||||
// Raw SCSS to include after the content.
|
// Raw SCSS to include after the content.
|
||||||
$setting = new admin_setting_configtextarea('theme_boost/scss', get_string('rawscss', 'theme_boost'),
|
$setting = new admin_setting_scsscode('theme_boost/scss', get_string('rawscss', 'theme_boost'),
|
||||||
get_string('rawscss_desc', 'theme_boost'), '', PARAM_RAW);
|
get_string('rawscss_desc', 'theme_boost'), '', PARAM_RAW);
|
||||||
$setting->set_updatedcallback('theme_reset_all_caches');
|
$setting->set_updatedcallback('theme_reset_all_caches');
|
||||||
$page->add($setting);
|
$page->add($setting);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue