| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 1 part of fn; | 5 part of fn; |
| 2 | 6 |
| 3 class Style { | 7 class Style { |
| 4 final String _className; | 8 final String _className; |
| 5 static Map<String, Style> _cache = null; | 9 static final Map<String, Style> _cache = new HashMap<String, Style>(); |
| 6 | 10 |
| 7 static int nextStyleId = 1; | 11 static int nextStyleId = 1; |
| 8 | 12 |
| 9 static String nextClassName(String styles) { | 13 static String nextClassName(String styles) { |
| 10 assert(sky.document != null); | 14 assert(sky.document != null); |
| 11 var className = "style$nextStyleId"; | 15 String className = "style$nextStyleId"; |
| 12 nextStyleId++; | 16 nextStyleId++; |
| 13 | 17 |
| 14 var styleNode = sky.document.createElement('style'); | 18 sky.Element styleNode = sky.document.createElement('style'); |
| 15 styleNode.setChild(new sky.Text(".$className { $styles }")); | 19 styleNode.setChild(new sky.Text(".$className { $styles }")); |
| 16 sky.document.appendChild(styleNode); | 20 sky.document.appendChild(styleNode); |
| 17 | 21 |
| 18 return className; | 22 return className; |
| 19 } | 23 } |
| 20 | 24 |
| 21 factory Style(String styles) { | 25 factory Style(String styles) { |
| 22 if (_cache == null) { | 26 return _cache.putIfAbsent(styles, () { |
| 23 _cache = new HashMap<String, Style>(); | 27 return new Style._internal(nextClassName(styles)); |
| 24 } | 28 }); |
| 25 | |
| 26 var style = _cache[styles]; | |
| 27 if (style == null) { | |
| 28 style = new Style._internal(nextClassName(styles)); | |
| 29 _cache[styles] = style; | |
| 30 } | |
| 31 | |
| 32 return style; | |
| 33 } | 29 } |
| 34 | 30 |
| 35 Style._internal(this._className); | 31 Style._internal(this._className); |
| 36 } | 32 } |
| OLD | NEW |