44
55namespace Tempest \View \Elements ;
66
7- use Dom \Comment ;
8- use Dom \DocumentType ;
9- use Dom \Element as DomElement ;
10- use Dom \Node ;
11- use Dom \Text ;
127use Tempest \Container \Container ;
138use Tempest \Core \AppConfig ;
9+ use Tempest \View \Attributes \PhpAttribute ;
1410use Tempest \View \Element ;
15- use Tempest \View \Renderers \TempestViewCompiler ;
11+ use Tempest \View \Parser \TempestViewCompiler ;
12+ use Tempest \View \Parser \Token ;
13+ use Tempest \View \Parser \TokenType ;
1614use Tempest \View \ViewComponent ;
1715use Tempest \View \ViewConfig ;
1816
@@ -35,64 +33,69 @@ public function setViewCompiler(TempestViewCompiler $compiler): self
3533 return $ this ;
3634 }
3735
38- public function make (Node $ node ): ?Element
36+ public function make (Token $ token ): ?Element
3937 {
4038 return $ this ->makeElement (
41- node : $ node ,
39+ token : $ token ,
4240 parent: null ,
4341 );
4442 }
4543
46- private function makeElement (Node $ node , ?Element $ parent ): ?Element
44+ private function makeElement (Token $ token , ?Element $ parent ): ?Element
4745 {
48- if ($ node instanceof DocumentType) {
49- $ content = $ node ->ownerDocument ->saveHTML ($ node );
50-
51- return new RawElement (tag: null , content: $ content );
46+ if (
47+ $ token ->type === TokenType::OPEN_TAG_END ||
48+ $ token ->type === TokenType::ATTRIBUTE_NAME ||
49+ $ token ->type === TokenType::ATTRIBUTE_VALUE ||
50+ $ token ->type === TokenType::SELF_CLOSING_TAG_END
51+ ) {
52+ return null ;
5253 }
5354
54- if ($ node instanceof Text) {
55- if (trim ($ node ->textContent ) === '' ) {
55+ if ($ token ->type === TokenType::CONTENT ) {
56+ $ text = $ token ->compile ();
57+
58+ if (trim ($ text ) === '' ) {
5659 return null ;
5760 }
5861
59- return new TextElement (
60- text: $ node ->textContent ,
61- );
62+ return new TextElement (text: $ text );
6263 }
6364
64- if ($ node instanceof Comment) {
65- return new CommentElement (
66- content: $ node ->textContent ,
67- );
65+ if (! $ token ->tag || $ token ->type === TokenType::COMMENT || $ token ->type === TokenType::PHP ) {
66+ return new RawElement (tag: null , content: $ token ->compile ());
6867 }
6968
70- $ tagName = strtolower ($ node ->tagName );
71-
7269 $ attributes = [];
7370
74- /** @var \Dom\Attr $attribute */
75- foreach ($ node ->attributes ?? [] as $ attribute ) {
76- $ name = str ($ attribute ->name )->camel ()->toString ();
71+ foreach ($ token ->htmlAttributes as $ name => $ value ) {
72+ $ name = str ($ name )
73+ ->trim ()
74+ ->before ('= ' )
75+ ->camel ()
76+ ->toString ();
7777
78- $ attributes [$ name ] = $ attribute ->value ;
79- }
78+ $ value = str ($ value )
79+ ->afterFirst ('" ' )
80+ ->beforeLast ('" ' )
81+ ->toString ();
8082
81- if (! ( $ node instanceof DomElement) || $ tagName === ' pre ' || $ tagName === ' code ' ) {
82- $ content = '' ;
83+ $ attributes [ $ name ] = $ value ;
84+ }
8385
84- foreach ($ node -> childNodes as $ child ) {
85- $ content .= $ node -> ownerDocument -> saveHTML ( $ child );
86- }
86+ foreach ($ token -> phpAttributes as $ index => $ content ) {
87+ $ attributes [] = new PhpAttribute (( string ) $ index , $ content );
88+ }
8789
90+ if ($ token ->tag === 'code ' || $ token ->tag === 'pre ' ) {
8891 return new RawElement (
89- tag: $ tagName ,
90- content: $ content ,
92+ tag: $ token -> tag ,
93+ content: $ token -> compileChildren () ,
9194 attributes: $ attributes ,
9295 );
9396 }
9497
95- if ($ viewComponentClass = $ this ->viewConfig ->viewComponents [$ tagName ] ?? null ) {
98+ if ($ viewComponentClass = $ this ->viewConfig ->viewComponents [$ token -> tag ] ?? null ) {
9699 if (! ($ viewComponentClass instanceof ViewComponent)) {
97100 $ viewComponentClass = $ this ->container ->get ($ viewComponentClass );
98101 }
@@ -103,27 +106,27 @@ private function makeElement(Node $node, ?Element $parent): ?Element
103106 viewComponent: $ viewComponentClass ,
104107 attributes: $ attributes ,
105108 );
106- } elseif ($ tagName === 'x-template ' ) {
109+ } elseif ($ token -> tag === 'x-template ' ) {
107110 $ element = new TemplateElement (
108111 attributes: $ attributes ,
109112 );
110- } elseif ($ tagName === 'x-slot ' ) {
113+ } elseif ($ token -> tag === 'x-slot ' ) {
111114 $ element = new SlotElement (
112- name: $ node ->getAttribute ('name ' ) ?: 'slot ' ,
115+ name: $ token ->getAttribute ('name ' ) ?? 'slot ' ,
113116 attributes: $ attributes ,
114117 );
115118 } else {
116119 $ element = new GenericElement (
117- tag: $ tagName ,
120+ tag: $ token -> tag ,
118121 attributes: $ attributes ,
119122 );
120123 }
121124
122125 $ children = [];
123126
124- foreach ($ node -> childNodes as $ child ) {
127+ foreach ($ token -> children as $ child ) {
125128 $ childElement = $ this ->clone ()->makeElement (
126- node : $ child ,
129+ token : $ child ,
127130 parent: $ parent ,
128131 );
129132
0 commit comments