| OLD | NEW |
| 1 /// | 1 /// |
| 2 /// Script to create boilerplate for a Polymer element. | 2 /// Script to create boilerplate for a Polymer element. |
| 3 /// Produces .dart and .html files for the element. | 3 /// Produces .dart and .html files for the element. |
| 4 /// | 4 /// |
| 5 /// Run this script with pub run: | 5 /// Run this script with pub run: |
| 6 /// | 6 /// |
| 7 /// pub run polymer:new_element element-name [-o output_dir] | 7 /// pub run polymer:new_element element-name [-o output_dir] |
| 8 /// | 8 /// |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
| 11 import 'package:path/path.dart' as path show absolute, dirname, join, split; | 11 import 'package:path/path.dart' as path show absolute, dirname, join, split; |
| 12 import 'package:polymer/html_element_names.dart'; |
| 12 | 13 |
| 13 void printUsage(ArgParser parser) { | 14 void printUsage(ArgParser parser) { |
| 14 print('pub run polymer:new_element [-o output_dir] [-e super-element] ' | 15 print('pub run polymer:new_element [-o output_dir] [-e super-element] ' |
| 15 'element-name'); | 16 'element-name'); |
| 16 print(parser.getUsage()); | 17 print(parser.getUsage()); |
| 17 } | 18 } |
| 18 | 19 |
| 19 void main(List<String> args) { | 20 void main(List<String> args) { |
| 20 var parser = new ArgParser(allowTrailingOptions: true); | 21 var parser = new ArgParser(allowTrailingOptions: true); |
| 21 | 22 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 String _findDirWithFile(String dir, String filename) { | 120 String _findDirWithFile(String dir, String filename) { |
| 120 while (!new File(path.join(dir, filename)).existsSync()) { | 121 while (!new File(path.join(dir, filename)).existsSync()) { |
| 121 var parentDir = path.dirname(dir); | 122 var parentDir = path.dirname(dir); |
| 122 // If we reached root and failed to find it, bail. | 123 // If we reached root and failed to find it, bail. |
| 123 if (parentDir == dir) return null; | 124 if (parentDir == dir) return null; |
| 124 dir = parentDir; | 125 dir = parentDir; |
| 125 } | 126 } |
| 126 return dir; | 127 return dir; |
| 127 } | 128 } |
| 128 | 129 |
| 129 bool _isDOMElement(String element) => (_htmlElementNames[element] != null); | 130 bool _isDOMElement(String element) => (HTML_ELEMENT_NAMES[element] != null); |
| 130 | 131 |
| 131 bool _isPolymerElement(String element) { | 132 bool _isPolymerElement(String element) { |
| 132 return element.contains('-') && (element.toLowerCase() == element); | 133 return element.contains('-') && (element.toLowerCase() == element); |
| 133 } | 134 } |
| 134 | 135 |
| 135 String _toCamelCase(String s) { | 136 String _toCamelCase(String s) { |
| 136 return s[0].toUpperCase() + s.substring(1); | 137 return s[0].toUpperCase() + s.substring(1); |
| 137 } | 138 } |
| 138 | 139 |
| 139 void _createBoilerPlate(String element, String superClass, String directory, | 140 void _createBoilerPlate(String element, String superClass, String directory, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 160 superClass.split('-').map((e) => _toCamelCase(e)).join(''); | 161 superClass.split('-').map((e) => _toCamelCase(e)).join(''); |
| 161 classDeclaration = 'class $capitalizedName extends $camelSuperClass {'; | 162 classDeclaration = 'class $capitalizedName extends $camelSuperClass {'; |
| 162 extendsElementString = ' extends="$superClass"'; | 163 extendsElementString = ' extends="$superClass"'; |
| 163 shadowString = | 164 shadowString = |
| 164 '\n <!-- Render extended element\'s Shadow DOM here -->\n' | 165 '\n <!-- Render extended element\'s Shadow DOM here -->\n' |
| 165 ' <shadow>\n </shadow>'; | 166 ' <shadow>\n </shadow>'; |
| 166 } else { | 167 } else { |
| 167 // The element being extended is a DOM Class. | 168 // The element being extended is a DOM Class. |
| 168 importDartHtml = "import 'dart:html';\n"; | 169 importDartHtml = "import 'dart:html';\n"; |
| 169 classDeclaration = | 170 classDeclaration = |
| 170 'class $capitalizedName extends ${_htmlElementNames[superClass]} ' | 171 'class $capitalizedName extends ${HTML_ELEMENT_NAMES[superClass]} ' |
| 171 'with Polymer, Observable {'; | 172 'with Polymer, Observable {'; |
| 172 polymerCreatedString = '\n polymerCreated();'; | 173 polymerCreatedString = '\n polymerCreated();'; |
| 173 extendsElementString = ' extends="$superClass"'; | 174 extendsElementString = ' extends="$superClass"'; |
| 174 } | 175 } |
| 175 | 176 |
| 176 String html = ''' | 177 String html = ''' |
| 177 <!-- import polymer-element's definition --> | 178 <!-- import polymer-element's definition --> |
| 178 <link rel="import" href="${pathToPackages}packages/polymer/polymer.html"> | 179 <link rel="import" href="${pathToPackages}packages/polymer/polymer.html"> |
| 179 | 180 |
| 180 <polymer-element name="$element"$extendsElementString> | 181 <polymer-element name="$element"$extendsElementString> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 236 } |
| 236 '''; | 237 '''; |
| 237 | 238 |
| 238 String dartFile = path.join(directory, underscoreName + '.dart'); | 239 String dartFile = path.join(directory, underscoreName + '.dart'); |
| 239 new File(dartFile).writeAsStringSync(dart); | 240 new File(dartFile).writeAsStringSync(dart); |
| 240 | 241 |
| 241 print('Successfully created:'); | 242 print('Successfully created:'); |
| 242 print(' ' + path.absolute(path.join(directory, underscoreName + '.dart'))); | 243 print(' ' + path.absolute(path.join(directory, underscoreName + '.dart'))); |
| 243 print(' ' + path.absolute(path.join(directory, underscoreName + '.html'))); | 244 print(' ' + path.absolute(path.join(directory, underscoreName + '.html'))); |
| 244 } | 245 } |
| 245 | |
| 246 /** | |
| 247 * HTML element to DOM type mapping. Source: | |
| 248 * <http://dev.w3.org/html5/spec/section-index.html#element-interfaces> | |
| 249 * | |
| 250 * The 'HTML' prefix has been removed to match `dart:html`, as per: | |
| 251 * <http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/lib/
html/scripts/htmlrenamer.py> | |
| 252 * It does not appear any element types are being renamed other than the prefix. | |
| 253 * However there does not appear to be the last subtypes for the following tags: | |
| 254 * command, data, dialog, td, th, and time. | |
| 255 */ | |
| 256 const _htmlElementNames = const { | |
| 257 'a': 'AnchorElement', | |
| 258 'abbr': 'Element', | |
| 259 'address': 'Element', | |
| 260 'area': 'AreaElement', | |
| 261 'article': 'Element', | |
| 262 'aside': 'Element', | |
| 263 'audio': 'AudioElement', | |
| 264 'b': 'Element', | |
| 265 'base': 'BaseElement', | |
| 266 'bdi': 'Element', | |
| 267 'bdo': 'Element', | |
| 268 'blockquote': 'QuoteElement', | |
| 269 'body': 'BodyElement', | |
| 270 'br': 'BRElement', | |
| 271 'button': 'ButtonElement', | |
| 272 'canvas': 'CanvasElement', | |
| 273 'caption': 'TableCaptionElement', | |
| 274 'cite': 'Element', | |
| 275 'code': 'Element', | |
| 276 'col': 'TableColElement', | |
| 277 'colgroup': 'TableColElement', | |
| 278 'command': 'Element', // see doc comment, was: 'CommandElement' | |
| 279 'data': 'Element', // see doc comment, was: 'DataElement' | |
| 280 'datalist': 'DataListElement', | |
| 281 'dd': 'Element', | |
| 282 'del': 'ModElement', | |
| 283 'details': 'DetailsElement', | |
| 284 'dfn': 'Element', | |
| 285 'dialog': 'Element', // see doc comment, was: 'DialogElement' | |
| 286 'div': 'DivElement', | |
| 287 'dl': 'DListElement', | |
| 288 'dt': 'Element', | |
| 289 'em': 'Element', | |
| 290 'embed': 'EmbedElement', | |
| 291 'fieldset': 'FieldSetElement', | |
| 292 'figcaption': 'Element', | |
| 293 'figure': 'Element', | |
| 294 'footer': 'Element', | |
| 295 'form': 'FormElement', | |
| 296 'h1': 'HeadingElement', | |
| 297 'h2': 'HeadingElement', | |
| 298 'h3': 'HeadingElement', | |
| 299 'h4': 'HeadingElement', | |
| 300 'h5': 'HeadingElement', | |
| 301 'h6': 'HeadingElement', | |
| 302 'head': 'HeadElement', | |
| 303 'header': 'Element', | |
| 304 'hgroup': 'Element', | |
| 305 'hr': 'HRElement', | |
| 306 'html': 'HtmlElement', | |
| 307 'i': 'Element', | |
| 308 'iframe': 'IFrameElement', | |
| 309 'img': 'ImageElement', | |
| 310 'input': 'InputElement', | |
| 311 'ins': 'ModElement', | |
| 312 'kbd': 'Element', | |
| 313 'keygen': 'KeygenElement', | |
| 314 'label': 'LabelElement', | |
| 315 'legend': 'LegendElement', | |
| 316 'li': 'LIElement', | |
| 317 'link': 'LinkElement', | |
| 318 'map': 'MapElement', | |
| 319 'mark': 'Element', | |
| 320 'menu': 'MenuElement', | |
| 321 'meta': 'MetaElement', | |
| 322 'meter': 'MeterElement', | |
| 323 'nav': 'Element', | |
| 324 'noscript': 'Element', | |
| 325 'object': 'ObjectElement', | |
| 326 'ol': 'OListElement', | |
| 327 'optgroup': 'OptGroupElement', | |
| 328 'option': 'OptionElement', | |
| 329 'output': 'OutputElement', | |
| 330 'p': 'ParagraphElement', | |
| 331 'param': 'ParamElement', | |
| 332 'pre': 'PreElement', | |
| 333 'progress': 'ProgressElement', | |
| 334 'q': 'QuoteElement', | |
| 335 'rp': 'Element', | |
| 336 'rt': 'Element', | |
| 337 'ruby': 'Element', | |
| 338 's': 'Element', | |
| 339 'samp': 'Element', | |
| 340 'script': 'ScriptElement', | |
| 341 'section': 'Element', | |
| 342 'select': 'SelectElement', | |
| 343 'small': 'Element', | |
| 344 'source': 'SourceElement', | |
| 345 'span': 'SpanElement', | |
| 346 'strong': 'Element', | |
| 347 'style': 'StyleElement', | |
| 348 'sub': 'Element', | |
| 349 'summary': 'Element', | |
| 350 'sup': 'Element', | |
| 351 'table': 'TableElement', | |
| 352 'tbody': 'TableSectionElement', | |
| 353 'td': 'TableCellElement', // see doc comment, was: 'TableDataCellElement' | |
| 354 'template': 'Element', // should be 'TemplateElement', but it is not yet | |
| 355 // in dart:html | |
| 356 'textarea': 'TextAreaElement', | |
| 357 'tfoot': 'TableSectionElement', | |
| 358 'th': 'TableCellElement', // see doc comment, was: 'TableHeaderCellElement' | |
| 359 'thead': 'TableSectionElement', | |
| 360 'time': 'Element', // see doc comment, was: 'TimeElement' | |
| 361 'title': 'TitleElement', | |
| 362 'tr': 'TableRowElement', | |
| 363 'track': 'TrackElement', | |
| 364 'u': 'Element', | |
| 365 'ul': 'UListElement', | |
| 366 'var': 'Element', | |
| 367 'video': 'VideoElement', | |
| 368 'wbr': 'Element', | |
| 369 }; | |
| OLD | NEW |