|
| 1 | +library angular.directive.ng_bind_html; |
| 2 | + |
| 3 | +import 'dart:html' as dom; |
| 4 | +import '../dom/directive.dart'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Creates a binding that will innerHTML the result of evaluating the |
| 8 | + * `expression` bound to `ng-bind-html` into the current element in a secure |
| 9 | + * way. This expression must evaluate to a string. The innerHTML-ed content |
| 10 | + * will be sanitized using a default [NodeValidator] constructed as `new |
| 11 | + * dom.NodeValidatorBuilder.common()`. In a future version, when Strict |
| 12 | + * Contextual Escaping support has been added to Angular.dart, this directive |
| 13 | + * will allow one to bypass the sanitizaton and innerHTML arbitrary trusted |
| 14 | + * HTML. |
| 15 | + * |
| 16 | + * Example: |
| 17 | + * |
| 18 | + * <div ng-bind-html="htmlVar"></div> |
| 19 | + */ |
| 20 | +@NgDirective( |
| 21 | + selector: '[ng-bind-html]', |
| 22 | + map: const {'ng-bind-html': '=.value'}) |
| 23 | +class NgBindHtmlAttrDirective { |
| 24 | + // The default HTML sanitizer. Eventually, we'll make this configurable or |
| 25 | + // use an optionally loaded `$sanitize` service. |
| 26 | + static final dom.NodeValidator validator = new dom.NodeValidatorBuilder.common(); |
| 27 | + |
| 28 | + dom.Element element; |
| 29 | + |
| 30 | + NgBindHtmlAttrDirective(dom.Element this.element); |
| 31 | + |
| 32 | + /** |
| 33 | + * Parsed expression from the `ng-bind-html` attribute. The result of this |
| 34 | + * expression is innerHTML'd according to the rules specified in this class' |
| 35 | + * documention. |
| 36 | + */ |
| 37 | + set value(value) => element.setInnerHtml((value == null ? '' : value.toString()), |
| 38 | + validator: validator) ; |
| 39 | +} |
0 commit comments