├── .editorconfig ├── .gitignore ├── README.md ├── composer.json ├── example ├── example.php └── test.js ├── phpunit.xml ├── src ├── JqueryToJS.php └── Traits │ ├── DOM.php │ ├── Events.php │ ├── Requests.php │ └── Selectors.php └── tests └── JqueryToJSTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | 7 | # Matches multiple files with brace expansion notation 8 | [*.{js,jsx,html,sass,graphql,php}] 9 | charset = utf-8 10 | indent_style = tab 11 | indent_size = 2 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jQuery to Javascript Converter 2 | This PHP class helps to convert your (really) simple jquery codes to vanilla javascript codes. 3 | 4 | There is a lot of issues, it's not completed yet, yet you can convert your simple jquery codes to vanilla js instead of use jquery library using this php class. 5 | 6 | > Inspired from [youmightnotneedjquery.com](http://youmightnotneedjquery.com) 7 | 8 | for javascript adaptation follow this repo as well: https://github.com/aykutkardas/jquery-to-javascript 9 | 10 | # Installation 11 | 12 | Install with composer 13 | 14 | ``` 15 | composer require tayfunerbilen/jquery-to-javascript-convert dev-master 16 | ``` 17 | 18 | then include `autoload.php` file in to your php file. 19 | 20 | ```php 21 | require __DIR__ . '/vendor/autoload.php'; 22 | ``` 23 | 24 | you're ready to go. 25 | 26 | # Usage 27 | 28 | convert onload function 29 | ```php 30 | $js = << { 39 | 40 | }); 41 | */ 42 | ``` 43 | 44 | convert variables 45 | 46 | ```php 47 | echo Erbilen\JqueryToJS::convert("var test"); 48 | // let test 49 | ``` 50 | 51 | convert id selectors 52 | ```php 53 | echo Erbilen\JqueryToJS::convert("var test = $('#test')"); 54 | // let test = document.getElementById("test") 55 | ``` 56 | 57 | convert class selectors 58 | ```php 59 | echo Erbilen\JqueryToJS::convert("var list = $('.list')"); 60 | // let list = document.getElementByClassName("list") 61 | ``` 62 | 63 | or 64 | 65 | ```php 66 | echo Erbilen\JqueryToJS::convert("var list = $('.list li')"); 67 | // let list = document.querySelectorAll(".list li") 68 | ``` 69 | 70 | convert `html()` method 71 | ```php 72 | echo Erbilen\JqueryToJS::convert("var content = $('#content').html()"); 73 | // let content = document.getElementById("content").innerHTML 74 | ``` 75 | 76 | or 77 | 78 | ```php 79 | echo Erbilen\JqueryToJS::convert("var content = $('#content').html('new content')"); 80 | // let content = document.getElementById("content").innerHTML = 'new content' 81 | ``` 82 | 83 | convert `text()` method 84 | ```php 85 | echo Erbilen\JqueryToJS::convert("var content = $('#content').text()"); 86 | // let content = document.getElementById("content").innerText 87 | ``` 88 | 89 | or 90 | 91 | ```php 92 | echo Erbilen\JqueryToJS::convert("var content = $('#content').text('new content')"); 93 | // let content = document.getElementById("content").innerText = 'new content' 94 | ``` 95 | 96 | convert `val()` method 97 | ```php 98 | echo Erbilen\JqueryToJS::convert("var name = $('#name').val()"); 99 | // let name = document.getElementById("name").value 100 | ``` 101 | 102 | or 103 | 104 | ```php 105 | echo Erbilen\JqueryToJS::convert("var name = $('#name').val('Tayfun Erbilen')"); 106 | // let name = document.getElementById("name").value = 'Tayfun Erbilen' 107 | ``` 108 | 109 | convert `show()` method 110 | 111 | ```php 112 | echo Erbilen\JqueryToJS::convert("$('#content').show()"); 113 | // document.getElementById("content").style.display = "" 114 | ``` 115 | 116 | convert `hide()` method 117 | 118 | ```php 119 | echo Erbilen\JqueryToJS::convert("$('#content').hide()"); 120 | // document.getElementById("content").style.display = "none" 121 | ``` 122 | 123 | convert `remove()` method 124 | 125 | ```php 126 | echo Erbilen\JqueryToJS::convert("$('#container').remove()"); 127 | /* 128 | let container = document.getElementById("container"); 129 | container.parentNode.removeChild(container); 130 | */ 131 | ``` 132 | 133 | convert `addClass()` method 134 | 135 | ```php 136 | echo Erbilen\JqueryToJS::convert("$('#container').addClass('active')"); 137 | // document.getElementById("container").classList.add("active") 138 | ``` 139 | 140 | convert `removeClass()` method 141 | 142 | ```php 143 | echo Erbilen\JqueryToJS::convert("$('#container').removeClass('active')"); 144 | // document.getElementById("container").classList.remove("active") 145 | ``` 146 | 147 | convert `hasClass()` method 148 | 149 | ```php 150 | echo Erbilen\JqueryToJS::convert("$('#container').hasClass('active')"); 151 | // document.getElementById("container").classList.contains("active") 152 | ``` 153 | 154 | convert `toggleClass()` method 155 | 156 | ```php 157 | echo Erbilen\JqueryToJS::convert("$('#container').toggleClass('active')"); 158 | // document.getElementById("container").classList.toggle("active") 159 | ``` 160 | 161 | convert `next()` method 162 | 163 | ```php 164 | echo Erbilen\JqueryToJS::convert("$('#test').next()"); 165 | // document.getElementById("test").nextElementSibling; 166 | ``` 167 | 168 | convert `prev()` method 169 | 170 | ```php 171 | echo Erbilen\JqueryToJS::convert("$('#test').prev()"); 172 | // document.getElementById("test").previousElementSibling; 173 | ``` 174 | 175 | convert `clone()` method 176 | ```php 177 | echo Erbilen\JqueryToJS::convert("var test = $('#test').clone()"); 178 | // let test = document.getElementById("test").cloneNode(true) 179 | ``` 180 | 181 | convert `$.each()` method 182 | ```php 183 | $js = << { 209 | 210 | }); 211 | */ 212 | ``` 213 | 214 | or 215 | 216 | ```php 217 | $js = << { 275 | if (this.status >= 200 && this.status < 400) { 276 | let responseVar = this.response; 277 | document.getElementById("response").innerHTML = responseVar; 278 | } 279 | } 280 | 281 | request.onerror = (err) => { 282 | document.getElementById("error").innerHTML = err; 283 | } 284 | 285 | request.send(data); 286 | */ 287 | ``` 288 | 289 | convert `$.getJSON()` method 290 | 291 | ```php 292 | $js = <<= 200 && this.status < 400) { 305 | let json = JSON.parse(this.response); 306 | console.log(json); 307 | } 308 | }; 309 | 310 | request.send(); 311 | */ 312 | ``` 313 | 314 | also you can send data as well 315 | 316 | ```php 317 | $js = <<= 200 && this.status < 400) { 338 | let json = JSON.parse(this.response); 339 | console.log(json); 340 | } 341 | }; 342 | 343 | request.send(JSON.stringify(data)); 344 | */ 345 | ``` 346 | 347 | # converting little bit complex code 348 | ```php 349 | $js = <<container hidden'); 358 | } else { 359 | container.addClass('actived'); 360 | text.html('container showed'); 361 | } 362 | e.preventDefault(); 363 | }); 364 | 365 | $('#load-btn').on('click', function (e) { 366 | 367 | $.ajax({ 368 | type: 'GET', 369 | url: 'api/load-more', 370 | success: function (result) { 371 | console.log(result); 372 | }, 373 | error: function (err) { 374 | console.log(err); 375 | } 376 | }); 377 | 378 | e.preventDefault(); 379 | }); 380 | 381 | }); 382 | JS; 383 | 384 | echo \Erbilen\JqueryToJS::convert($js); 385 | 386 | /* 387 | 388 | document.addEventListener("DOMContentLoaded", () => { 389 | 390 | document.getElementById("button").addEventListener('click', (e) => { 391 | let container = document.getElementById("container"), 392 | text = document.getElementById("text"); 393 | if (container.classList.contains("active")) { 394 | container.classList.remove("active"); 395 | text.innerHTML = 'container hidden'; 396 | } else { 397 | container.classList.add("actived"); 398 | text.innerHTML = 'container showed'; 399 | } 400 | e.preventDefault(); 401 | }); 402 | 403 | document.getElementById("load-btn").addEventListener('click', (e) => { 404 | 405 | let request = new XMLHttpRequest(); 406 | request.open('GET', 'api/load-more', true); 407 | 408 | request.onload = () => { 409 | if (this.status >= 200 && this.status < 400) { 410 | let result = this.response; 411 | console.log(result); 412 | } 413 | }; 414 | 415 | request.onerror = (err) => { 416 | console.log(err); 417 | }; 418 | 419 | request.send(); 420 | 421 | e.preventDefault(); 422 | }); 423 | 424 | }); 425 | */ 426 | ``` 427 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tayfunerbilen/jquery-to-javascript-convert", 3 | "type": "class", 4 | "description": "It helps to convert your simple jquery codes to vanilla javascript codes", 5 | "keywords": [ 6 | "php", 7 | "jquery", 8 | "javascript", 9 | "converter" 10 | ], 11 | "homepage": "https://github.com/tayfunerbilen/jquery-to-javascript-convert", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Tayfun Erbilen", 16 | "email": "tayfunerbilen@gmail.com", 17 | "homepage": "http://www.erbilen.net", 18 | "role": "Developer" 19 | } 20 | ], 21 | "minimum-stability": "dev", 22 | "require": { 23 | "php": ">=7.0.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Erbilen\\": "src" 28 | } 29 | }, 30 | "autoload-dev": { 31 | "psr-4": { 32 | "Erbilen\\JqueryToJS\\Test\\": "tests" 33 | } 34 | }, 35 | "require-dev": { 36 | "phpunit/phpunit": "^8.5.2" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /example/example.php: -------------------------------------------------------------------------------- 1 | container hidden'); 9 | } else { 10 | container.addClass('actived'); 11 | text.html('container showed'); 12 | } 13 | e.preventDefault(); 14 | }); 15 | 16 | $('#load-btn').on('click', function (e) { 17 | 18 | $.ajax({ 19 | type: 'GET', 20 | url: 'api/load-more', 21 | success: function (result) { 22 | console.log(result); 23 | }, 24 | error: function (err) { 25 | console.log(err); 26 | } 27 | }); 28 | 29 | e.preventDefault(); 30 | }); 31 | 32 | }); 33 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests/ 6 | 7 | 8 | 9 | 10 | ./src 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/JqueryToJS.php: -------------------------------------------------------------------------------- 1 | {' . $js[1] . '});'; 28 | }, self::$js); 29 | } 30 | 31 | /** 32 | * Açıklama satırlarını siler 33 | */ 34 | public static function removeComments() 35 | { 36 | 37 | // çok satırlı açıklamaları sil 38 | $pattern = '@/\*(.*?)\*/@s'; 39 | self::$js = preg_replace($pattern, null, self::$js); 40 | 41 | // tek satırlı açıklamarı sil 42 | $pattern = '@//(.+)@'; 43 | self::$js = preg_replace($pattern, null, self::$js); 44 | } 45 | 46 | public static function convert($js) 47 | { 48 | self::$js = $js; 49 | self::documentReady(); 50 | if (self::$removeComments) 51 | self::removeComments(); 52 | if (self::$replaceVarToLet) 53 | self::varToLet(); 54 | self::replaceVars(); 55 | self::idSelectors(); 56 | self::classSelectors(); 57 | self::html(); 58 | self::text(); 59 | self::addClass(); 60 | self::removeClass(); 61 | self::toggleClass(); 62 | self::hasClass(); 63 | self::hide(); 64 | self::show(); 65 | self::remove(); 66 | self::val(); 67 | self::next(); 68 | self::prev(); 69 | self::clone(); 70 | self::each(); 71 | self::on(); 72 | self::trigger(); 73 | self::ajax(); 74 | self::getJSON(); 75 | return self::$js; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/Traits/DOM.php: -------------------------------------------------------------------------------- 1 | '], null, $js[3]); 129 | $el = (self::$replaceVarToLet ? 'let' : 'var') . ' ' . $varName . ' = document.' . $js[1] . '(' . $js[2] . $js[3] . $js[4] . ');'; 130 | return $el . PHP_EOL . "\t" . $varName . ".parentNode.removeChild(" . $varName . ");"; 131 | }, self::$js); 132 | } 133 | 134 | /** 135 | * .val(x) metodunu dönüştürür 136 | */ 137 | public static function val() 138 | { 139 | $pattern = "@\.val\(([0-9a-zA-Z-_'\"]+|)\)@"; 140 | self::$js = preg_replace_callback($pattern, function ($js) { 141 | if (!empty($js[1])) 142 | return '.value = ' . $js[1]; 143 | else 144 | return '.value'; 145 | }, self::$js); 146 | } 147 | 148 | /** 149 | * .next() metodunu dönüştürür 150 | */ 151 | public static function next() 152 | { 153 | $pattern = "@\.next\(\)@"; 154 | self::$js = preg_replace_callback($pattern, function ($js) { 155 | return '.nextElementSibling'; 156 | }, self::$js); 157 | // @TODO çoklu seçim için güncelleme yapılacak 158 | } 159 | 160 | /** 161 | * .prev() metodunu dönüştürür 162 | */ 163 | public static function prev() 164 | { 165 | $pattern = "@\.prev\(\)@"; 166 | self::$js = preg_replace_callback($pattern, function ($js) { 167 | return '.previousElementSibling'; 168 | }, self::$js); 169 | // @TODO çoklu seçim için güncelleme yapılacak 170 | } 171 | 172 | /** 173 | * .clone() metodunu dönüştürür 174 | */ 175 | public static function clone() 176 | { 177 | $pattern = "@.clone\(\)@"; 178 | self::$js = preg_replace($pattern, '.cloneNode(true)', self::$js); 179 | } 180 | 181 | /** 182 | * $.each() metodunu dönüştürür 183 | */ 184 | public static function each() 185 | { 186 | 187 | $pattern = "@\\$\.each\(\s?(\w+)\s?\,\s?function\s?\((.*?)\)\s?{(.*?)}\s?\)@s"; 188 | self::$js = preg_replace_callback($pattern, function ($js) { 189 | $params = array_reverse(explode(',', str_replace(' ', null, trim($js[2])))); 190 | return $js[1] . '.forEach(function(' . implode(', ', $params) . '){ 191 | ' . trim($js[3]) . ' 192 | })'; 193 | }, self::$js); 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/Traits/Events.php: -------------------------------------------------------------------------------- 1 | {' . self::this($js[3], $js[2]) . '});'; 27 | }, self::$js); 28 | } 29 | 30 | /** 31 | * .on() metodundaki $(this) değerini değiştirir 32 | * @param $js 33 | * @return string|string[]|null 34 | */ 35 | public static function this($js, $event = null) 36 | { 37 | $pattern = "@\\$\(\s?this\s?\)@"; 38 | return preg_replace_callback($pattern, function ($js) use ($event) { 39 | return (empty($event) ? 'e' : $event) . '.target'; 40 | }, $js); 41 | } 42 | 43 | /** 44 | * .trigger() metodunu dönüştürür 45 | */ 46 | public static function trigger() 47 | { 48 | $pattern = '@document.(getElementByClassName|getElementById|querySelector|querySelectorAll)\("([0-9a-zA-Z-_]+)"\)\.trigger\((\'|")([a-zA-Z]+)(\'|")\);?@'; 49 | self::$js = preg_replace_callback($pattern, function ($js) { 50 | return 'var event = document.createEvent(\'HTMLEvents\'); 51 | event.initEvent(\'' . $js[4] . '\', true, false); 52 | document.' . $js[1] . '("' . $js[2] . '").dispatchEvent(event); 53 | '; 54 | }, self::$js); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/Traits/Requests.php: -------------------------------------------------------------------------------- 1 | = 200 && this.status < 400) { 66 | let {$successVariable} = this.response; 67 | {$successCallback} 68 | } 69 | } 70 | JS; 71 | 72 | if ($errorVariable) 73 | $js .= <<= 200 && this.status < 400) { 99 | let ' . $js[8] . ' = JSON.parse(this.response); 100 | ' . trim($js[9]) . ' 101 | } 102 | }; 103 | 104 | request.send(' . ($js[5] ? 'JSON.stringify(' : null) . $js[4] . $js[5] . $js[6] . ($js[5] ? ')' : null) . ');'; 105 | }, self::$js); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/Traits/Selectors.php: -------------------------------------------------------------------------------- 1 | assertSame($result, "document.getElementById(\"selector\")"); 14 | } 15 | 16 | public function testClassSelectors() 17 | { 18 | $result = JqueryToJS::convert("$('.selector')"); 19 | $this->assertSame($result, "document.getElementByClassName(\"selector\")"); 20 | } 21 | } 22 | --------------------------------------------------------------------------------