X Tutup
Skip to content

Commit e988f59

Browse files
committed
fix(html_adapter): Implement hasAttribute and getAttribute.
Fixes the template compiler when running on the server.
1 parent 3810e4b commit e988f59

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

modules/angular2/src/dom/html_adapter.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Html5LibDomAdapter implements DomAdapter {
1313
hasProperty(element, String name) {
1414
// This is needed for serverside compile to generate the right getters/setters.
1515
// TODO: change this once we have property schema support.
16-
// Attention: Keep this in sync with browser_adapter.dart!
16+
// Attention: Keep this in sync with browser_adapter.dart!
1717
return true;
1818
}
1919

@@ -235,10 +235,14 @@ class Html5LibDomAdapter implements DomAdapter {
235235
return map;
236236
}
237237
hasAttribute(element, String attribute) {
238-
throw 'not implemented';
238+
// `attributes` keys can be {@link AttributeName}s.
239+
return element.attributes.keys.any((key) => '$key' == attribute);
239240
}
240241
getAttribute(element, String attribute) {
241-
throw 'not implemented';
242+
// `attributes` keys can be {@link AttributeName}s.
243+
var key = element.attributes.keys.firstWhere(
244+
(key) => '$key' == attribute, orElse: () {});
245+
return element.attributes[key];
242246
}
243247
setAttribute(element, String name, String value) {
244248
element.attributes[name] = value;

modules/angular2/test/dom/html5lib_adapter.server.spec.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,19 @@ main() {
1616
it('should parse HTML', () {
1717
expect(subject.parse('<div>hi</div>'), isNotNull);
1818
});
19+
20+
it('implements hasAttribute', () {
21+
var div = subject.querySelector(
22+
subject.parse('<div foo="bar"></div>'), ('div'));
23+
expect(subject.hasAttribute(div, 'foo')).toBeTrue();
24+
expect(subject.hasAttribute(div, 'bar')).toBeFalse();
25+
});
26+
27+
it('implements getAttribute', () {
28+
var div = subject.querySelector(
29+
subject.parse('<div foo="bar"></div>'), ('div'));
30+
expect(subject.getAttribute(div, 'foo')).toEqual('bar');
31+
expect(subject.getAttribute(div, 'bar')).toBe(null);
32+
});
1933
});
2034
}

0 commit comments

Comments
 (0)
X Tutup