Merge branch 'MDL-61394-master' of git://git.cameron1729.xyz/moodle

This commit is contained in:
Andrew Nicols
2018-02-22 08:30:53 +08:00
7 changed files with 184 additions and 9 deletions
+2
View File
@@ -14,6 +14,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
$temp->add(new admin_setting_configcheckbox('dndallowtextandlinks', new lang_string('dndallowtextandlinks', 'admin'), new lang_string('configdndallowtextandlinks', 'admin'), 0));
$temp->add(new admin_setting_configexecutable('pathtosassc', new lang_string('pathtosassc', 'admin'), new lang_string('pathtosassc_help', 'admin'), null));
$ADMIN->add('experimental', $temp);
// "debugging" settingpage
+2
View File
@@ -837,6 +837,8 @@ $string['pathtopsqldesc'] = 'This is only necessary to enter if you have more th
$string['pathtopsqlinvalid'] = 'Invalid path to psql - either wrong path or not executable';
$string['pathtopython'] = 'Path to Python';
$string['pathtopythondesc'] = 'Path to your executable Python binary (both Python 2 and Python 3 are acceptable).';
$string['pathtosassc'] = 'Path to SassC';
$string['pathtosassc_help'] = 'Specifying the location of the SassC binary will switch the SASS compiler from Moodle\'s PHP implementation to SassC. See https://github.com/sass/sassc for more information.';
$string['pcreunicodewarning'] = 'It is strongly recommended to use PCRE PHP extension that is compatible with Unicode characters.';
$string['perfdebug'] = 'Performance info';
$string['performance'] = 'Performance';
+48
View File
@@ -98,6 +98,54 @@ class core_scss extends \Leafo\ScssPhp\Compiler {
return $this->compile($content);
}
/**
* Compile scss.
*
* Overrides ScssPHP's implementation, using the SassC compiler if it is available.
*
* @param string $code SCSS to compile.
* @param string $path Path to SCSS to compile.
*
* @return string The compiled CSS.
*/
public function compile($code, $path = null) {
global $CFG;
$pathtosassc = trim($CFG->pathtosassc);
if (!empty($pathtosassc) && is_executable($pathtosassc) && !is_dir($pathtosassc)) {
$process = proc_open(
$pathtosassc . ' -I' . implode(':', $this->importPaths) . ' -s',
[
['pipe', 'r'], // Set the process stdin pipe to read mode.
['pipe', 'w'], // Set the process stdout pipe to write mode.
['pipe', 'w'] // Set the process stderr pipe to write mode.
],
$pipes // Pipes become available in $pipes (pass by reference).
);
if (is_resource($process)) {
fwrite($pipes[0], $code); // Write the raw scss to the sassc process stdin.
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
// The proc_close function returns the process exit status. Anything other than 0 is bad.
if (proc_close($process) !== 0) {
throw new coding_exception($stderr);
}
// Compiled CSS code will be available from stdout.
return $stdout;
}
}
return parent::compile($code, $path);
}
/**
* Compile child; returns a value to halt execution
*
+1 -1
View File
@@ -1475,7 +1475,7 @@ class theme_config {
// Compile!
$compiled = $compiler->to_css();
} catch (\Leafo\ScssPhp\Exception $e) {
} catch (\Exception $e) {
$compiled = false;
debugging('Error while compiling SCSS: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
+74 -1
View File
@@ -70,6 +70,61 @@ class core_scss_testcase extends advanced_testcase {
];
}
/**
* Test cases for SassC compilation.
*/
public function scss_compilation_provider() {
return [
'simple' => [
'scss' => '$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}',
'expected' => <<<CSS
body {
font: 100% Helvetica, sans-serif;
color: #333; }
CSS
],
'nested' => [
'scss' => 'nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}',
'expected' => <<<CSS
nav ul {
margin: 0;
padding: 0;
list-style: none; }
nav li {
display: inline-block; }
nav a {
display: block;
padding: 6px 12px;
text-decoration: none; }
CSS
]
];
}
/**
* @dataProvider is_valid_file_provider
*/
@@ -78,4 +133,22 @@ class core_scss_testcase extends advanced_testcase {
$pathvalid = phpunit_util::call_internal_method($scss, 'is_valid_file', [$path], \core_scss::class);
$this->assertSame($valid, $pathvalid);
}
}
/**
* Test that we can use the SassC compiler if it's provided.
*
* @dataProvider scss_compilation_provider
* @param string $scss The raw scss to compile.
* @param string $expectedcss The expected CSS output.
*/
public function test_scss_compilation_with_sassc($scss, $expectedcss) {
if (!defined('PHPUNIT_PATH_TO_SASSC')) {
$this->markTestSkipped('Path to SassC not provided');
}
$this->resetAfterTest();
set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
$compiler = new core_scss();
$this->assertSame($compiler->compile($scss), $expectedcss);
}
}
+7 -7
View File
@@ -153,12 +153,12 @@ div.backup-section + form,
// I think this could be avoided (or at least tidied up) ifr
// we used HTML5 input types like url, phone, email, number etc.
/* rtl:ignore */
.mform .fitem .felement input[name=email],
.mform .fitem .felement input[name=email2],
.mform .fitem .felement input[name=url],
.mform .fitem .felement input[name=idnumber],
.mform .fitem .felement input[name=phone1],
.mform .fitem .felement input[name=phone2] {
.mform .fitem .felement input[name="email"],
.mform .fitem .felement input[name="email2"],
.mform .fitem .felement input[name="url"],
.mform .fitem .felement input[name="idnumber"],
.mform .fitem .felement input[name="phone1"],
.mform .fitem .felement input[name="phone2"] {
text-align: left;
direction: ltr;
}
@@ -326,7 +326,7 @@ select[size],
select[multiple] {
overflow: auto;
}
select[size=1] {
select[size="1"] {
overflow: visible;
}
+50
View File
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the unittests for boost's scss compilation.
*
* @package theme_boost
* @copyright 2018 Cameron Ball <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for scss compilation.
*
* @package theme_boost
* @copyright 2016 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class theme_boost_scss_testcase extends advanced_testcase {
/**
* Test that boost can be compiled using SassC (the defacto implemention).
*/
public function test_scss_compilation_with_sassc() {
if (!defined('PHPUNIT_PATH_TO_SASSC')) {
$this->markTestSkipped('Path to SassC not provided');
}
$this->resetAfterTest();
set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
$this->assertNotEmpty(
theme_config::load('boost')->get_css_content_debug('scss', null, null)
);
}
}