Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(sanitization): make NodeValidator injectable
Browse files Browse the repository at this point in the history
Closes #490
Closes #498
  • Loading branch information
bgourlie authored and mhevery committed Feb 6, 2014
1 parent e62b269 commit 47ab48a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/directive/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class NgDirectiveModule extends Module {
value(NgBindDirective, null);
value(NgBindTemplateDirective, null);
value(NgBindHtmlDirective, null);
value(dom.NodeValidator, new dom.NodeValidatorBuilder.common());

This comment has been minimized.

Copy link
@zoechi

zoechi Feb 19, 2014

Contributor

The class doc comment seems not to be accurate anymore as the validator is now injectable.

value(NgClassDirective, null);
value(NgClassOddDirective, null);
value(NgClassEvenDirective, null);
Expand Down
13 changes: 5 additions & 8 deletions lib/directive/ng_bind_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ part of angular.directive;
selector: '[ng-bind-html]',
map: const {'ngBindHtml': '=>value'})
class NgBindHtmlDirective {
// The default HTML sanitizer. Eventually, we'll make this configurable or
// use an optionally loaded `$sanitize` service.
static final dom.NodeValidator validator = new dom.NodeValidatorBuilder.common();

final dom.Element element;

NgBindHtmlDirective(this.element);
final dom.NodeValidator validator;

NgBindHtmlDirective(this.element, dom.NodeValidator this.validator);

/**
* Parsed expression from the `ng-bind-html` attribute.  The result of this
* expression is innerHTML'd according to the rules specified in this class'
* documention.
* documentation.
*/
set value(value) => element.setInnerHtml(value == null ? '' : value.toString(),
validator: validator) ;
validator: validator);
}
27 changes: 24 additions & 3 deletions test/directive/ng_bind_html_spec.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
library ng_bind_html_spec;

import 'dart:html' as dom;
import '../_specs.dart';

main() {
describe('BindHtmlDirective', () {
TestBed _;

beforeEach(inject((TestBed tb) => _ = tb));

it('should sanitize and set innerHtml and sanitize and set html',
inject((Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
Expand All @@ -17,5 +15,28 @@ main() {
// Sanitization removes the href attribute on the <a> tag.
expect(element.html()).toEqual('<a><b>Google!</b></a>');
}));

it('should use injected NodeValidator and override default sanitize behavior',
module((Module module) {
module.factory(dom.NodeValidator, (_) {
final validator = new NodeValidatorBuilder();
validator.allowNavigation(new AnyUriPolicy());
validator.allowTextElements();
return validator;
});

inject((Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
var element = $('<div ng-bind-html="htmlVar"></div>');
compiler(element, directives)(injector, element);
scope.htmlVar = '<a href="http://www.google.com"><b>Google!</b></a>';
scope.$digest();
// Sanitation allows href attributes per injected sanitizer.
expect(element.html()).toEqual('<a href="http://www.google.com"><b>Google!</b></a>');
});
}));
});
}

class AnyUriPolicy implements UriPolicy {
bool allowsUri(String uri) => true;
}

3 comments on commit 47ab48a

@mabounassif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we use this with handlebars?

@zoechi
Copy link
Contributor

@zoechi zoechi commented on 47ab48a Mar 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't inser HTML wit handle bars only text. You need to use ng-bind-html

@mabounassif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant unsafe html. Should I reimplement a NgDirective that will not go through the default sanitization by passing a custom validator?

Please sign in to comment.