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 'package:csslib/parser.dart' as css; | 8 import 'package:csslib/parser.dart' as css; |
9 import 'package:csslib/visitor.dart'; | 9 import 'package:csslib/visitor.dart'; |
10 import 'package:html5lib/dom.dart'; | 10 import 'package:html5lib/dom.dart'; |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 '\n</style>'; | 607 '\n</style>'; |
608 var template = elemInfo.node; | 608 var template = elemInfo.node; |
609 template.insertBefore(new Element.html(styleSheet), | 609 template.insertBefore(new Element.html(styleSheet), |
610 template.hasChildNodes() ? template.children[0] : null); | 610 template.hasChildNodes() ? template.children[0] : null); |
611 } | 611 } |
612 | 612 |
613 _context.statics.add('final', '__shadowTemplate', | 613 _context.statics.add('final', '__shadowTemplate', |
614 elemInfo.node.sourceSpan, | 614 elemInfo.node.sourceSpan, |
615 "new autogenerated.DocumentFragment.html('''" | 615 "new autogenerated.DocumentFragment.html('''" |
616 "${escapeDartString(elemInfo.node.innerHtml, triple: true)}" | 616 "${escapeDartString(elemInfo.node.innerHtml, triple: true)}" |
617 "''')"); | 617 "''', treeSanitizer: autogenerated.nullTreeSanitizer)"); |
618 _context.printer.addLine( | 618 _context.printer.addLine( |
619 "__root.nodes.add(__shadowTemplate.clone(true));"); | 619 "__root.nodes.add(__shadowTemplate.clone(true));"); |
620 } | 620 } |
621 | 621 |
622 visit(elemInfo); | 622 visit(elemInfo); |
623 | 623 |
624 bool hasExtends = info.extendsComponent != null; | 624 bool hasExtends = info.extendsComponent != null; |
625 var codeInfo = info.userCode; | 625 var codeInfo = info.userCode; |
626 var classDecl = info.classDeclaration; | 626 var classDecl = info.classDeclaration; |
627 if (classDecl == null) return null; | 627 if (classDecl == null) return null; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 String _emitCreateHtml(Node node, Declarations statics) { | 821 String _emitCreateHtml(Node node, Declarations statics) { |
822 if (node is Text) { | 822 if (node is Text) { |
823 return "new autogenerated.Text('${escapeDartString(node.value)}')"; | 823 return "new autogenerated.Text('${escapeDartString(node.value)}')"; |
824 } | 824 } |
825 | 825 |
826 // Namespace constants from: | 826 // Namespace constants from: |
827 // http://dev.w3.org/html5/spec/namespaces.html#namespaces | 827 // http://dev.w3.org/html5/spec/namespaces.html#namespaces |
828 var isHtml = node.namespace == 'http://www.w3.org/1999/xhtml'; | 828 var isHtml = node.namespace == 'http://www.w3.org/1999/xhtml'; |
829 var isSvg = node.namespace == 'http://www.w3.org/2000/svg'; | 829 var isSvg = node.namespace == 'http://www.w3.org/2000/svg'; |
830 var isEmpty = node.attributes.length == 0 && node.nodes.length == 0; | 830 var isEmpty = node.attributes.length == 0 && node.nodes.length == 0; |
| 831 var specialTags = const ['body', 'caption', 'col', 'colgroup', 'head', |
| 832 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'track']; |
831 | 833 |
832 var constructor; | 834 var expr; |
833 // Generate precise types like "new ButtonElement()" if we can. | 835 // Generate precise types like "new ButtonElement()" if we can. |
834 if (isEmpty && isHtml) { | 836 if (isEmpty && isHtml) { |
835 constructor = htmlElementConstructors[node.tagName]; | 837 var constructor = htmlElementConstructors[node.tagName]; |
836 if (constructor != null) { | 838 if (constructor != null) { |
837 constructor = '$constructor()'; | 839 expr = 'new autogenerated.$constructor()'; |
838 } else { | 840 } else { |
839 constructor = "Element.tag('${node.tagName}')"; | 841 expr = "new autogenerated.Element.tag('${node.tagName}')"; |
840 } | 842 } |
841 } else if (isEmpty && isSvg) { | 843 } else if (isEmpty && isSvg) { |
842 constructor = "_svg.SvgElement.tag('${node.tagName}')"; | 844 expr = "new autogenerated_svg.SvgElement.tag('${node.tagName}')"; |
| 845 } else if (isSvg || !specialTags.contains(node.tagName)) { |
| 846 // TODO(sigmund): does this work for the mathml namespace? |
| 847 var constructor = isSvg ? 'autogenerated_svg.SvgElement.svg' |
| 848 : 'autogenerated.Element.html'; |
| 849 expr = "new $constructor(" |
| 850 "'${escapeDartString(node.outerHtml)}', " |
| 851 "treeSanitizer: autogenerated.nullTreeSanitizer)"; |
843 } else { | 852 } else { |
844 // TODO(sigmund): does this work for the mathml namespace? | 853 expr = "autogenerated.createSafeNode('${node.tagName}'," |
845 var target = isSvg ? '_svg.SvgElement.svg' : 'Element.html'; | 854 " ${_attributesAsString(node.attributes)}," |
846 constructor = "$target('${escapeDartString(node.outerHtml)}')"; | 855 "'${escapeDartString(node.innerHtml)}')"; |
847 } | 856 } |
848 | |
849 var expr = 'new autogenerated.$constructor'; | |
850 var varName = '__html${statics.declarations.length}'; | 857 var varName = '__html${statics.declarations.length}'; |
851 statics.add('final', varName, node.sourceSpan, expr); | 858 statics.add('final', varName, node.sourceSpan, expr); |
852 return '${varName}.clone(true)'; | 859 return '${varName}.clone(true)'; |
853 } | 860 } |
854 | 861 |
| 862 String _attributesAsString(Map attributes) { |
| 863 var sb = new StringBuffer(); |
| 864 sb.write('{'); |
| 865 bool first = true; |
| 866 for (var key in attributes.keys) { |
| 867 if (!first) sb.write(','); |
| 868 first = false; |
| 869 var sKey = escapeDartString(key); |
| 870 var sValue = escapeDartString(attributes[key]); |
| 871 sb.write("'$sKey': '$sValue'"); |
| 872 } |
| 873 sb.write('}'); |
| 874 return sb.toString(); |
| 875 } |
| 876 |
855 /** Trim down the html for the main html page. */ | 877 /** Trim down the html for the main html page. */ |
856 void transformMainHtml(Document document, FileInfo fileInfo, | 878 void transformMainHtml(Document document, FileInfo fileInfo, |
857 PathMapper pathMapper, bool hasCss, bool rewriteUrls, Messages messages, | 879 PathMapper pathMapper, bool hasCss, bool rewriteUrls, Messages messages, |
858 String bootstrapOutName) { | 880 String bootstrapOutName) { |
859 var filePath = fileInfo.inputUrl.resolvedPath; | 881 var filePath = fileInfo.inputUrl.resolvedPath; |
860 | 882 |
861 var dartLoaderTag = null; | 883 var dartLoaderTag = null; |
862 for (var tag in document.queryAll('script')) { | 884 for (var tag in document.queryAll('script')) { |
863 var src = tag.attributes['src']; | 885 var src = tag.attributes['src']; |
864 if (src != null && src.split('/').last == 'dart.js') { | 886 if (src != null && src.split('/').last == 'dart.js') { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 return """ | 974 return """ |
953 // Auto-generated from $filename. | 975 // Auto-generated from $filename. |
954 // DO NOT EDIT. | 976 // DO NOT EDIT. |
955 $lib | 977 $lib |
956 import 'dart:html' as autogenerated; | 978 import 'dart:html' as autogenerated; |
957 import 'dart:svg' as autogenerated_svg; | 979 import 'dart:svg' as autogenerated_svg; |
958 import 'package:web_ui/web_ui.dart' as autogenerated; | 980 import 'package:web_ui/web_ui.dart' as autogenerated; |
959 import 'package:web_ui/observe/observable.dart' as __observe; | 981 import 'package:web_ui/observe/observable.dart' as __observe; |
960 """; | 982 """; |
961 } | 983 } |
OLD | NEW |