Completed
Push — master ( ef9455...966ba4 )
by Jean-Christophe
02:39
created

Javascript::prep_jquery_selector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Ajax\service;
4
5
class Javascript {
6
	public static $preventDefault="\nif(event && event.preventDefault) event.preventDefault();\n";
7
	public static $stopPropagation="\nif(event && event.stopPropagation) event.stopPropagation();\n";
8
9
	public static function containsCode($expression){
10
		return strrpos($expression, 'this')!==false||strrpos($expression, 'event')!==false||strrpos($expression, 'self')!==false;
11
	}
12
13
	/**
14
	 * Puts HTML element in quotes for use in jQuery code
15
	 * unless the supplied element is the Javascript 'this'
16
	 * object, in which case no quotes are added
17
	 *
18
	 * @param string $element
19
	 * @return string
20
	 */
21
	public static function prep_element($element) {
22
		if (self::containsCode($element)===false) {
23
			$element='"'.addslashes($element).'"';
24
		}
25
		return $element;
26
	}
27
28
	/**
29
	 * Puts HTML values in quotes for use in jQuery code
30
	 * unless the supplied value contains the Javascript 'this' or 'event'
31
	 * object, in which case no quotes are added
32
	 *
33
	 * @param string $value
34
	 * @return string
35
	 */
36
	public static function prep_value($value) {
37
		if (\is_array($value)) {
38
			$value=implode(",", $value);
39
		}
40
		if (self::containsCode($value)===false) {
41
			$value='"'.$value.'"';
42
		}
43
		return $value;
44
	}
45
46
	public static function prep_jquery_selector($value){
47
		if(JString::startswith($value, '$(')===false){
48
			return '$('.$value.')';
49
		}
50
		return $value;
51
	}
52
}
53