| 
									
										
										
										
											2018-11-27 12:32:14 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2018-11-27 12:32:14 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import * as html from '@angular/compiler/src/ml_parser/ast'; | 
					
						
							|  |  |  | import {getHtmlTagDefinition} from '@angular/compiler/src/ml_parser/html_tags'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _SerializerVisitor implements html.Visitor { | 
					
						
							|  |  |  |   visitElement(element: html.Element, context: any): any { | 
					
						
							|  |  |  |     if (getHtmlTagDefinition(element.name).isVoid) { | 
					
						
							|  |  |  |       return `<${element.name}${this._visitAll(element.attrs, ' ')}/>`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |     return `<${element.name}${this._visitAll(element.attrs, ' ')}>${ | 
					
						
							|  |  |  |         this._visitAll(element.children)}</${element.name}>`; | 
					
						
							| 
									
										
										
										
											2018-11-27 12:32:14 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitAttribute(attribute: html.Attribute, context: any): any { | 
					
						
							|  |  |  |     return `${attribute.name}="${attribute.value}"`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |   visitText(text: html.Text, context: any): any { | 
					
						
							|  |  |  |     return text.value; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-27 12:32:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |   visitComment(comment: html.Comment, context: any): any { | 
					
						
							|  |  |  |     return `<!--${comment.value}-->`; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-27 12:32:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   visitExpansion(expansion: html.Expansion, context: any): any { | 
					
						
							|  |  |  |     return `{${expansion.switchValue}, ${expansion.type},${this._visitAll(expansion.cases)}}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { | 
					
						
							|  |  |  |     return ` ${expansionCase.value} {${this._visitAll(expansionCase.expression)}}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private _visitAll(nodes: html.Node[], join: string = ''): string { | 
					
						
							|  |  |  |     if (nodes.length == 0) { | 
					
						
							|  |  |  |       return ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return join + nodes.map(a => a.visit(this, null)).join(join); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const serializerVisitor = new _SerializerVisitor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function serializeNodes(nodes: html.Node[]): string[] { | 
					
						
							|  |  |  |   return nodes.map(node => node.visit(serializerVisitor, null)); | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  | } |