- Fixed invalid elements after testing
This commit is contained in:
		
							parent
							
								
									a0bf505377
								
							
						
					
					
						commit
						4bf08def27
					
				
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | |||||||
|  | The MIT License (MIT) | ||||||
|  | 
 | ||||||
|  | Copyright (c) 2019 Dave Mc Nicoll | ||||||
|  | 
 | ||||||
|  | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  | of this software and associated documentation files (the "Software"), to deal | ||||||
|  | in the Software without restriction, including without limitation the rights | ||||||
|  | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  | copies of the Software, and to permit persons to whom the Software is | ||||||
|  | furnished to do so, subject to the following conditions: | ||||||
|  | 
 | ||||||
|  | The above copyright notice and this permission notice shall be included in all | ||||||
|  | copies or substantial portions of the Software. | ||||||
|  | 
 | ||||||
|  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||
|  | SOFTWARE. | ||||||
| @ -23,7 +23,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|      * |      * | ||||||
|      *  key                  value           uses |      *  key                  value           uses | ||||||
|      *  -------------------------------------------------------------------------------------------- |      *  -------------------------------------------------------------------------------------------- | ||||||
|      *  single-tag                           Only render a single tag. Cannot contains any childrens |      *  tag-type             single          Only render a single tag. Cannot contains any childrens | ||||||
|      *  force-tag-open       tag             Control the way the opening tag is rendered |      *  force-tag-open       tag             Control the way the opening tag is rendered | ||||||
|      *  force-tag-close      tag-close       "    "    "    "    closing   "    "    " |      *  force-tag-close      tag-close       "    "    "    "    closing   "    "    " | ||||||
|      *  no-attr                              Attributes will not be rendered |      *  no-attr                              Attributes will not be rendered | ||||||
| @ -35,7 +35,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
| 
 | 
 | ||||||
|     public $selected = null; |     public $selected = null; | ||||||
| 
 | 
 | ||||||
|     public string $content; |     public string $content = ""; | ||||||
| 
 | 
 | ||||||
|     protected ?UiQuery $kwery = null; |     protected ?UiQuery $kwery = null; | ||||||
| 
 | 
 | ||||||
| @ -150,8 +150,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|     public function render() { |     public function render() { | ||||||
|         $attributesList = []; |         $attributesList = []; | ||||||
| 
 | 
 | ||||||
|         foreach ( $this->attr as $key => $value ) { |         foreach ( $this->attributes as $key => $value ) { | ||||||
| 
 |  | ||||||
|             if ( is_array($value) ) { |             if ( is_array($value) ) { | ||||||
|                 if (empty($value)) continue; |                 if (empty($value)) continue; | ||||||
| 
 | 
 | ||||||
| @ -163,12 +162,12 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|                     } |                     } | ||||||
| 
 | 
 | ||||||
|                     $attributesList[] = "$key=\"".implode(';', $style).'"'; |                     $attributesList[] = "$key=\"".implode(';', $style).'"'; | ||||||
| 
 |  | ||||||
|                 } |                 } | ||||||
|                 else { |                 else { | ||||||
|                     $attributesList[] = implode(' ', $value); |                     $attributesList[] = implode(' ', $value); | ||||||
|                 } |                 } | ||||||
|             }else if ( !is_numeric($key) ) { |             } | ||||||
|  |             else if ( !is_numeric($key) ) { | ||||||
|                 # will output something like  <tag $key=$value></tag>
 |                 # will output something like  <tag $key=$value></tag>
 | ||||||
|                 $attributesList[] = "$key=\"$value\"";
 |                 $attributesList[] = "$key=\"$value\"";
 | ||||||
|             } |             } | ||||||
| @ -190,30 +189,30 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if ( $this->options->exist('no-tag') ) { |         if ( in_array('no-tag', $this->options) ) { | ||||||
|             return $this->content . $content; |             return $this->content . $content; | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             $attributesstr = (count($attributesList) && !$this->options->exist('no-attr') ? " " . implode($attributesList, " ") : ""); |             $attributesstr = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : ""); | ||||||
| 
 | 
 | ||||||
|             # Force the node to contain a certain opening tag (php is a good example)
 |             # Force the node to contain a certain opening tag (php is a good example)
 | ||||||
|             if ( $this->options->exist('force-tag-open') ) { |             if ( in_array('force-tag-open', $this->options) ) { | ||||||
|                 $opentag = $this->options['force-tag-open'] . $attributesstr; |                 $opentag = $this->options['force-tag-open'] . $attributesstr; | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 $opentag = $this->tag ? "<{$this->tag}" . $attributesstr . ">" : ""; |                 $opentag = $this->tag ? "<{$this->tag}" . $attributesstr . ">" : ""; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if ( $this->options->exist('tag-type') ) { |             if ( $this->options['tag-type'] ?? false ) { | ||||||
|                 if ( $this->options['tag-type'] === "single" ) { |                 if ( $this->options['tag-type'] === "single" ) { | ||||||
|                     return $opentag; |                     return $opentag; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             else if ( $this->options->exist('force-tag-close') ) { |             else if ( in_array('force-tag-close', $this->options) ) { | ||||||
|                 $closetag = $this->options['force-tag-close']; |                 $closetag = $this->options['force-tag-close']; | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 $closetag = $this->tag ? "<" . ($this->options->exist('escape-tag-end') ? "\/" : "/" ) . "{$this->tag}>" : ""; |                 $closetag = $this->tag ? "<" . (in_array('escape-tag-end', $this->options) ? "\/" : "/" ) . "{$this->tag}>" : ""; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             return $opentag . $this->content . $content . $closetag; |             return $opentag . $this->content . $content . $closetag; | ||||||
| @ -290,8 +289,8 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|     public function attributes(array $attributes) : self |     public function attributes(array $attributes) : self | ||||||
|     { |     { | ||||||
|         foreach($attributes as $key => $value) { |         foreach($attributes as $key => $value) { | ||||||
|             switch($key) { |             switch ((string) $key) { | ||||||
|                 case 'class' : |                 case 'class': | ||||||
|                     $this->addClass($value); |                     $this->addClass($value); | ||||||
|                 break; |                 break; | ||||||
| 
 | 
 | ||||||
| @ -308,8 +307,9 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { | |||||||
|                     elseif ( is_array($this->attributes[$key] ?? false) ) { |                     elseif ( is_array($this->attributes[$key] ?? false) ) { | ||||||
|                         $this->attributes[$key] = array_replace($value, $this->attributes[$key]); |                         $this->attributes[$key] = array_replace($value, $this->attributes[$key]); | ||||||
|                     } |                     } | ||||||
| 
 |                     else { | ||||||
|                     $this->attributes[$key] = $value; |                         $this->attributes[$key] = $value; | ||||||
|  |                     } | ||||||
|                 break; |                 break; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -4,12 +4,14 @@ namespace Picea\Ui\Form; | |||||||
| 
 | 
 | ||||||
| use Picea\Ui\Common\UiElement; | use Picea\Ui\Common\UiElement; | ||||||
| use Picea\Extension\Extension; | use Picea\Extension\Extension; | ||||||
|  | use Picea\Extension\ExtensionTrait; | ||||||
| 
 | 
 | ||||||
| class UiForm extends UiElement implements Extension { | class UiForm extends UiElement implements Extension { | ||||||
|  |     use ExtensionTrait; | ||||||
| 
 | 
 | ||||||
|     public string $defaultMethod = "get"; |     public string $defaultMethod = "get"; | ||||||
| 
 | 
 | ||||||
|     public string $token = "ui.form"; |     public array $token = [ "ui.form", "ui.endform", "ui.form.get", "ui.form.post" ]; | ||||||
| 
 | 
 | ||||||
|     public string $tag = "form"; |     public string $tag = "form"; | ||||||
| 
 | 
 | ||||||
| @ -19,40 +21,33 @@ class UiForm extends UiElement implements Extension { | |||||||
| 
 | 
 | ||||||
|     public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string |     public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string | ||||||
|     { |     { | ||||||
|        list($action, $options) = array_pad(explode(',', $arguments), 2, null); |         if ( $token === 'ui.endform' ) { | ||||||
| 
 |             return "</form>"; | ||||||
|        switch($token) { |  | ||||||
|            case "ui.form.get": |  | ||||||
|                $this->attributes['method'] = "get"; |  | ||||||
|            break; |  | ||||||
| 
 |  | ||||||
|            case "ui.form.post": |  | ||||||
|                $this->attributes['method'] = "post"; |  | ||||||
|            break; |  | ||||||
| 
 |  | ||||||
|            default: |  | ||||||
|            case $this->token: |  | ||||||
|                $this->attributes['method'] = $this->defaultMethod; |  | ||||||
|            break; |  | ||||||
|        } |  | ||||||
| 
 |  | ||||||
|        $this->attributes['action'] = $action; |  | ||||||
|        $this->attributes($this->parseAttributes($options)); |  | ||||||
|        return $this->render(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function parseAttributes(string $options) : array |  | ||||||
|     { |  | ||||||
|         if ( $options ?? false ) { |  | ||||||
|             $attributes = eval($options); |  | ||||||
| 
 |  | ||||||
|             if ( ! is_array($attributes) ) { |  | ||||||
|                throw new InvalidArgumentException("Given options `$options` is not a valid attributes array."); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             return $attributes; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|        return []; |         list($action, $options) = array_pad(explode(',', $arguments), 2, null); | ||||||
|  | 
 | ||||||
|  |         $action = $this->trim($action); | ||||||
|  | 
 | ||||||
|  |         switch($token) { | ||||||
|  |             case "ui.form.get": | ||||||
|  |                $this->attributes['method'] = "get"; | ||||||
|  |             break; | ||||||
|  | 
 | ||||||
|  |             case "ui.form.post": | ||||||
|  |                $this->attributes['method'] = "post"; | ||||||
|  |                $this->attributes['enctype'] = "multipart/form-data"; | ||||||
|  |             break; | ||||||
|  | 
 | ||||||
|  |             default: | ||||||
|  |             case $this->token: | ||||||
|  |                $this->attributes['method'] = $this->defaultMethod; | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         $this->option('tag-type', 'single'); | ||||||
|  |         $this->attributes['action'] = $this->trim($action); | ||||||
|  |         $this->attributes($this->parseOptions($options)); | ||||||
|  |         return $this->render(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -4,8 +4,10 @@ namespace Picea\Ui\Form; | |||||||
| 
 | 
 | ||||||
| use Picea\Ui\Common\UiElement; | use Picea\Ui\Common\UiElement; | ||||||
| use Picea\Extension\Extension; | use Picea\Extension\Extension; | ||||||
|  | use Picea\Extension\ExtensionTrait; | ||||||
| 
 | 
 | ||||||
| class UiInput extends UiElement implements Extension { | class UiInput extends UiElement implements Extension { | ||||||
|  |     use ExtensionTrait; | ||||||
| 
 | 
 | ||||||
|     public string $token = "ui.input"; |     public string $token = "ui.input"; | ||||||
| 
 | 
 | ||||||
| @ -19,29 +21,16 @@ class UiInput extends UiElement implements Extension { | |||||||
|     { |     { | ||||||
|         list($name, $value, $options) = array_pad(explode(',', $arguments), 3, null); |         list($name, $value, $options) = array_pad(explode(',', $arguments), 3, null); | ||||||
| 
 | 
 | ||||||
|         $this->attributes['name'] = $name; |         $this->attributes['name'] = $this->trim($name); | ||||||
|         $this->setValue($value); |         $this->setValue($this->trim($value)); | ||||||
|         $this->attributes($this->parseAttributes($options)); |         $this->attributes($this->parseOptions($options)); | ||||||
|         return $this->render(); |         return $this->render(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function parseAttributes(string $options) : array |  | ||||||
|     { |  | ||||||
|         if ( $options ?? false ) { |  | ||||||
|             $attributes = eval($options); |  | ||||||
| 
 |  | ||||||
|             if ( ! is_array($attributes) ) { |  | ||||||
|                throw new InvalidArgumentException("Given options `$options` is not a valid attributes array."); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             return $attributes; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         return []; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     protected function setValue($value) : void |     protected function setValue($value) : void | ||||||
|     { |     { | ||||||
|         $this->attributes['value'] = $value; |         $this->attributes['value'] = $value; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -13,7 +13,7 @@ class UiTel extends UiInput { | |||||||
| 
 | 
 | ||||||
|    public string $defaultPattern = "[0-9]{1}-[0-9]{3}-[0-9]{3}-[0-9]{4}"; |    public string $defaultPattern = "[0-9]{1}-[0-9]{3}-[0-9]{3}-[0-9]{4}"; | ||||||
| 
 | 
 | ||||||
|    protected function parseAttributes(string $options) : array |    protected function parseAttributes(?string $options) : array | ||||||
|    { |    { | ||||||
|        return parent::parseAttributes($options) + [ 'pattern' => $this->defaultPattern ]; |        return parent::parseAttributes($options) + [ 'pattern' => $this->defaultPattern ]; | ||||||
|    } |    } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user