| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** Collects several code emitters for the template tool. */ | 5 /** Collects several code emitters for the template tool. */ |
| 6 library emitters; | 6 library emitters; |
| 7 | 7 |
| 8 import 'dart:uri'; | 8 import 'dart:uri'; |
| 9 import 'package:csslib/parser.dart' as css; | 9 import 'package:csslib/parser.dart' as css; |
| 10 import 'package:csslib/visitor.dart'; | 10 import 'package:csslib/visitor.dart'; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 var path = _computeNodePath(info.node, parent.node); | 84 var path = _computeNodePath(info.node, parent.node); |
| 85 var pathExpr = path.map((p) => '.nodes[$p]').join(); | 85 var pathExpr = path.map((p) => '.nodes[$p]').join(); |
| 86 | 86 |
| 87 printer.addLine("$id = ${parent.identifier}$pathExpr;", | 87 printer.addLine("$id = ${parent.identifier}$pathExpr;", |
| 88 span: info.node.sourceSpan); | 88 span: info.node.sourceSpan); |
| 89 } | 89 } |
| 90 | 90 |
| 91 printer.add(childrenPrinter); | 91 printer.add(childrenPrinter); |
| 92 | 92 |
| 93 if (info.childrenCreatedInCode && !info.hasIterate && !info.hasIfCondition) { | 93 if (info.childrenCreatedInCode && !info.hasLoop && !info.hasCondition) { |
| 94 _emitAddNodes(printer, context.statics, info.children, '$id.nodes'); | 94 _emitAddNodes(printer, context.statics, info.children, '$id.nodes'); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 /** | 98 /** |
| 99 * Returns the path of the node from the provided root element. For example, | 99 * Returns the path of the node from the provided root element. For example, |
| 100 * given a tree like: | 100 * given a tree like: |
| 101 * | 101 * |
| 102 * <a><b></b><c><d></d></c></a> | 102 * <a><b></b><c><d></d></c></a> |
| 103 * | 103 * |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 ..indent += 1 | 327 ..indent += 1 |
| 328 ..add(childContext.declarations) | 328 ..add(childContext.declarations) |
| 329 ..add(childContext.printer) | 329 ..add(childContext.printer) |
| 330 ..indent -= 1; | 330 ..indent -= 1; |
| 331 _emitAddNodes(printer, childContext.statics, info.children, '__t'); | 331 _emitAddNodes(printer, childContext.statics, info.children, '__t'); |
| 332 printer..addLine('});\n'); | 332 printer..addLine('});\n'); |
| 333 } | 333 } |
| 334 | 334 |
| 335 /** | 335 /** |
| 336 * Emits code for template lists like `<template iterate='item in items'>` or | 336 * Emits code for template lists like `<template iterate='item in items'>` or |
| 337 * `<td template iterate='item in items'>`. | 337 * `<td template repeat='item in items'>`. |
| 338 */ | 338 */ |
| 339 void emitLoop(TemplateInfo info, CodePrinter printer, Context childContext) { | 339 void emitLoop(TemplateInfo info, CodePrinter printer, Context childContext) { |
| 340 var id = info.identifier; | 340 var id = info.identifier; |
| 341 var items = info.loopItems; | 341 var items = info.loopItems; |
| 342 var loopVar = info.loopVariable; | 342 var loopVar = info.loopVariable; |
| 343 printer..addLine('__t.loop($id, () => $items, ($loopVar, __t) {', | 343 |
| 344 var suffix = ''; |
| 345 // TODO(jmesserly): remove this functionality after a grace period. |
| 346 if (!info.isTemplateElement && !info.isRepeat) suffix = 'IterateAttr'; |
| 347 |
| 348 printer..addLine('__t.loop$suffix($id, () => $items, ($loopVar, __t) {', |
| 344 span: info.node.sourceSpan) | 349 span: info.node.sourceSpan) |
| 345 ..indent += 1 | 350 ..indent += 1 |
| 346 ..add(childContext.declarations) | 351 ..add(childContext.declarations) |
| 347 ..add(childContext.printer) | 352 ..add(childContext.printer) |
| 348 ..indent -= 1; | 353 ..indent -= 1; |
| 349 _emitAddNodes(printer, childContext.statics, info.children, '__t'); | 354 _emitAddNodes(printer, childContext.statics, info.children, '__t'); |
| 350 printer..addLine(info.isTemplateElement | 355 printer.addLine('});'); |
| 351 ? '});' : '}, isTemplateElement: false);'); | |
| 352 } | 356 } |
| 353 | 357 |
| 354 | 358 |
| 355 /** | 359 /** |
| 356 * An visitor that applies [NodeFieldEmitter], [EventListenerEmitter], | 360 * An visitor that applies [NodeFieldEmitter], [EventListenerEmitter], |
| 357 * [DataValueEmitter], [ConditionalEmitter], and | 361 * [DataValueEmitter], [ConditionalEmitter], and |
| 358 * [ListEmitter] recursively on a DOM tree. | 362 * [ListEmitter] recursively on a DOM tree. |
| 359 */ | 363 */ |
| 360 class RecursiveEmitter extends InfoVisitor { | 364 class RecursiveEmitter extends InfoVisitor { |
| 361 final FileInfo _fileInfo; | 365 final FileInfo _fileInfo; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 376 | 380 |
| 377 var indent = _context.printer.indent; | 381 var indent = _context.printer.indent; |
| 378 var childPrinter = new CodePrinter(indent); | 382 var childPrinter = new CodePrinter(indent); |
| 379 emitDeclarations(info, _context.declarations); | 383 emitDeclarations(info, _context.declarations); |
| 380 emitInitializations(info, _context, childPrinter); | 384 emitInitializations(info, _context, childPrinter); |
| 381 emitEventListeners(info, _context.printer); | 385 emitEventListeners(info, _context.printer); |
| 382 emitAttributeBindings(info, _context.printer); | 386 emitAttributeBindings(info, _context.printer); |
| 383 emitComponentCreation(info, _context.printer); | 387 emitComponentCreation(info, _context.printer); |
| 384 | 388 |
| 385 var childContext = null; | 389 var childContext = null; |
| 386 if (info.hasIfCondition) { | 390 if (info.hasCondition) { |
| 387 childContext = new Context(statics: _context.statics, indent: indent + 1); | 391 childContext = new Context(statics: _context.statics, indent: indent + 1); |
| 388 emitConditional(info, _context.printer, childContext); | 392 emitConditional(info, _context.printer, childContext); |
| 389 } else if (info.hasIterate) { | 393 } else if (info.hasLoop) { |
| 390 childContext = new Context(statics: _context.statics, indent: indent + 1); | 394 childContext = new Context(statics: _context.statics, indent: indent + 1); |
| 391 emitLoop(info, _context.printer, childContext); | 395 emitLoop(info, _context.printer, childContext); |
| 392 } else { | 396 } else { |
| 393 childContext = new Context(declarations: _context.declarations, | 397 childContext = new Context(declarations: _context.declarations, |
| 394 statics: _context.statics, printer: childPrinter, | 398 statics: _context.statics, printer: childPrinter, |
| 395 isClass: _context.isClass); | 399 isClass: _context.isClass); |
| 396 } | 400 } |
| 397 | 401 |
| 398 // Invoke super to visit children. | 402 // Invoke super to visit children. |
| 399 var oldContext = _context; | 403 var oldContext = _context; |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 // For simplicity we emit the warning always, like validator.nu does. | 797 // For simplicity we emit the warning always, like validator.nu does. |
| 794 if (doctype.tagName != 'html' || commentIndex != 1) { | 798 if (doctype.tagName != 'html' || commentIndex != 1) { |
| 795 messages.warning('file should start with <!DOCTYPE html> ' | 799 messages.warning('file should start with <!DOCTYPE html> ' |
| 796 'to avoid the possibility of it being parsed in quirks mode in IE. ' | 800 'to avoid the possibility of it being parsed in quirks mode in IE. ' |
| 797 'See http://www.w3.org/TR/html5-diff/#doctype', doctype.sourceSpan); | 801 'See http://www.w3.org/TR/html5-diff/#doctype', doctype.sourceSpan); |
| 798 } | 802 } |
| 799 } | 803 } |
| 800 document.nodes.insert(commentIndex, parseFragment( | 804 document.nodes.insert(commentIndex, parseFragment( |
| 801 '\n<!-- This file was auto-generated from $filePath. -->\n')); | 805 '\n<!-- This file was auto-generated from $filePath. -->\n')); |
| 802 } | 806 } |
| OLD | NEW |