aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-03 13:55:50 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-03 13:55:50 +0100
commit2d1cc8aaebc567cc006d387921c7fbbed35280ce (patch)
treed48f4af93ed9316f48af3bbe0a01d1eab8e95087
parente62b6c1617886b2cdd7553ea9b119c431e4eb363 (diff)
parent32ab973254678c1130559c977f59374484d92ca6 (diff)
downloadnextcloud-server-2d1cc8aaebc567cc006d387921c7fbbed35280ce.tar.gz
nextcloud-server-2d1cc8aaebc567cc006d387921c7fbbed35280ce.zip
Merge pull request #19461 from owncloud/reuse_code
reuse code
-rw-r--r--lib/private/util.php81
-rw-r--r--tests/lib/util.php91
2 files changed, 129 insertions, 43 deletions
diff --git a/lib/private/util.php b/lib/private/util.php
index 5466082d030..ac42b96de2d 100644
--- a/lib/private/util.php
+++ b/lib/private/util.php
@@ -457,19 +457,12 @@ class OC_Util {
*/
public static function addScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'js', $file);
- //TODO eliminate double code
- if (!in_array($path, self::$scripts)) {
- // core js files need separate handling
- if ($application !== 'core' && $file !== null) {
- self::addTranslations($application);
- }
- if ($prepend===true) {
- array_unshift(self::$scripts, $path);
- }
- else {
- self::$scripts[] = $path;
- }
+
+ // core js files need separate handling
+ if ($application !== 'core' && $file !== null) {
+ self::addTranslations ( $application );
}
+ self::addExternalResource($application, $prepend, $path, "script");
}
/**
@@ -482,14 +475,7 @@ class OC_Util {
*/
public static function addVendorScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file);
- //TODO eliminate double code
- if (! in_array ( $path, self::$scripts )) {
- if ($prepend === true) {
- array_unshift ( self::$scripts, $path );
- } else {
- self::$scripts [] = $path;
- }
- }
+ self::addExternalResource($application, $prepend, $path, "script");
}
/**
@@ -508,14 +494,7 @@ class OC_Util {
} else {
$path = "l10n/$languageCode";
}
- //TODO eliminate double code
- if (!in_array($path, self::$scripts)) {
- if ($prepend === true) {
- array_unshift ( self::$scripts, $path );
- } else {
- self::$scripts [] = $path;
- }
- }
+ self::addExternalResource($application, $prepend, $path, "script");
}
/**
@@ -528,14 +507,7 @@ class OC_Util {
*/
public static function addStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'css', $file);
- //TODO eliminate double code
- if (!in_array($path, self::$styles)) {
- if ($prepend === true) {
- array_unshift ( self::$styles, $path );
- } else {
- self::$styles[] = $path;
- }
- }
+ self::addExternalResource($application, $prepend, $path, "style");
}
/**
@@ -548,13 +520,36 @@ class OC_Util {
*/
public static function addVendorStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file);
- //TODO eliminate double code
- if (!in_array($path, self::$styles)) {
- if ($prepend === true) {
- array_unshift ( self::$styles, $path );
- } else {
- self::$styles[] = $path;
- }
+ self::addExternalResource($application, $prepend, $path, "style");
+ }
+
+ /**
+ * add an external resource css/js file
+ *
+ * @param string $application application id
+ * @param bool $prepend prepend the file to the beginning of the list
+ * @param string $path
+ * @param string $type (script or style)
+ * @return void
+ */
+ private static function addExternalResource($application, $prepend, $path, $type = "script") {
+
+ if ($type === "style") {
+ if (!in_array($path, self::$styles)) {
+ if ($prepend === true) {
+ array_unshift ( self::$styles, $path );
+ } else {
+ self::$styles[] = $path;
+ }
+ }
+ } elseif ($type === "script") {
+ if (!in_array($path, self::$scripts)) {
+ if ($prepend === true) {
+ array_unshift ( self::$scripts, $path );
+ } else {
+ self::$scripts [] = $path;
+ }
+ }
}
}
diff --git a/tests/lib/util.php b/tests/lib/util.php
index a6ec06aa41f..9b82be36955 100644
--- a/tests/lib/util.php
+++ b/tests/lib/util.php
@@ -401,6 +401,97 @@ class Test_Util extends \Test\TestCase {
$this->assertNotEmpty($errors);
}
}
+
+ protected function setUp() {
+ parent::setUp();
+
+ \OC_Util::$scripts = [];
+ \OC_Util::$styles = [];
+ }
+ protected function tearDown() {
+ parent::tearDown();
+
+ \OC_Util::$scripts = [];
+ \OC_Util::$styles = [];
+ }
+
+ public function testAddScript() {
+ \OC_Util::addScript('core', 'myFancyJSFile1');
+ \OC_Util::addScript('myApp', 'myFancyJSFile2');
+ \OC_Util::addScript('core', 'myFancyJSFile0', true);
+ \OC_Util::addScript('core', 'myFancyJSFile10', true);
+ // add duplicate
+ \OC_Util::addScript('core', 'myFancyJSFile1');
+
+ $this->assertEquals([
+ 'core/js/myFancyJSFile10',
+ 'core/js/myFancyJSFile0',
+ 'core/js/myFancyJSFile1',
+ 'myApp/l10n/en',
+ 'myApp/js/myFancyJSFile2',
+ ], \OC_Util::$scripts);
+ $this->assertEquals([], \OC_Util::$styles);
+ }
+
+ public function testAddVendorScript() {
+ \OC_Util::addVendorScript('core', 'myFancyJSFile1');
+ \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
+ \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
+ \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
+ // add duplicate
+ \OC_Util::addVendorScript('core', 'myFancyJSFile1');
+
+ $this->assertEquals([
+ 'core/vendor/myFancyJSFile10',
+ 'core/vendor/myFancyJSFile0',
+ 'core/vendor/myFancyJSFile1',
+ 'myApp/vendor/myFancyJSFile2',
+ ], \OC_Util::$scripts);
+ $this->assertEquals([], \OC_Util::$styles);
+ }
+
+ public function testAddTranslations() {
+ \OC_Util::addTranslations('appId', 'de');
+
+ $this->assertEquals([
+ 'appId/l10n/de'
+ ], \OC_Util::$scripts);
+ $this->assertEquals([], \OC_Util::$styles);
+ }
+
+ public function testAddStyle() {
+ \OC_Util::addStyle('core', 'myFancyCSSFile1');
+ \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
+ \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
+ \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
+ // add duplicate
+ \OC_Util::addStyle('core', 'myFancyCSSFile1');
+
+ $this->assertEquals([], \OC_Util::$scripts);
+ $this->assertEquals([
+ 'core/css/myFancyCSSFile10',
+ 'core/css/myFancyCSSFile0',
+ 'core/css/myFancyCSSFile1',
+ 'myApp/css/myFancyCSSFile2',
+ ], \OC_Util::$styles);
+ }
+
+ public function testAddVendorStyle() {
+ \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
+ \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
+ \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
+ \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
+ // add duplicate
+ \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
+
+ $this->assertEquals([], \OC_Util::$scripts);
+ $this->assertEquals([
+ 'core/vendor/myFancyCSSFile10',
+ 'core/vendor/myFancyCSSFile0',
+ 'core/vendor/myFancyCSSFile1',
+ 'myApp/vendor/myFancyCSSFile2',
+ ], \OC_Util::$styles);
+ }
}
/**