X Tutup
Skip to content

Commit 693d9dc

Browse files
jeffbcrossvsavkin
authored andcommitted
fix(parse5): support comment nodes with getText and setText
In the browser, calling element.textContent causes child comment nodes to be ignored, while getting textContent directly on a comment node will return the comment. This change makes parse5Adapter consistent with this behavior by adding a 2nd argument to getText telling if it's being called recursively. Closes #5805
1 parent 194dc7d commit 693d9dc

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

modules/angular2/src/platform/dom/dom_renderer.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {Inject, Injectable, OpaqueToken} from 'angular2/src/core/di';
22
import {AnimationBuilder} from 'angular2/src/animate/animation_builder';
3-
4-
import {StringMapWrapper} from 'angular2/src/facade/collection';
53
import {
64
isPresent,
75
isBlank,
@@ -42,9 +40,8 @@ import {
4240
DefaultProtoViewRef
4341
} from 'angular2/src/core/render/view';
4442
import {ViewEncapsulation} from 'angular2/src/core/metadata';
45-
46-
// TODO move it once DdomAdapter is moved
4743
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
44+
import {camelCaseToDashCase} from './util';
4845

4946
const NAMESPACE_URIS =
5047
CONST_EXPR({'xlink': 'http://www.w3.org/1999/xlink', 'svg': 'http://www.w3.org/2000/svg'});
@@ -162,11 +159,7 @@ export abstract class DomRenderer extends Renderer implements NodeFactory<Node>
162159
var existingBindings = RegExpWrapper.firstMatch(
163160
TEMPLATE_BINDINGS_EXP, StringWrapper.replaceAll(DOM.getText(element), /\n/g, ''));
164161
var parsedBindings = Json.parse(existingBindings[1]);
165-
if (isPresent(propertyValue)) {
166-
parsedBindings[dashCasedPropertyName] = propertyValue;
167-
} else {
168-
StringMapWrapper.delete(parsedBindings, dashCasedPropertyName);
169-
}
162+
parsedBindings[dashCasedPropertyName] = propertyValue;
170163
DOM.setText(element, StringWrapper.replace(TEMPLATE_COMMENT_TEXT, '{}',
171164
Json.stringify(parsedBindings)));
172165
} else {

modules/angular2/src/platform/server/parse5_adapter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,25 @@ export class Parse5DomAdapter extends DomAdapter {
239239
treeAdapter.appendChild(el, content.childNodes[i]);
240240
}
241241
}
242-
getText(el): string {
242+
getText(el, isRecursive?: boolean): string {
243243
if (this.isTextNode(el)) {
244244
return el.data;
245+
} else if (this.isCommentNode(el)) {
246+
// In the DOM, comments within an element return an empty string for textContent
247+
// However, comment node instances return the comment content for textContent getter
248+
return isRecursive ? '' : el.data;
245249
} else if (isBlank(el.childNodes) || el.childNodes.length == 0) {
246250
return "";
247251
} else {
248252
var textContent = "";
249253
for (var i = 0; i < el.childNodes.length; i++) {
250-
textContent += this.getText(el.childNodes[i]);
254+
textContent += this.getText(el.childNodes[i], true);
251255
}
252256
return textContent;
253257
}
254258
}
255259
setText(el, value: string) {
256-
if (this.isTextNode(el)) {
260+
if (this.isTextNode(el) || this.isCommentNode(el)) {
257261
el.data = value;
258262
} else {
259263
this.clearNodes(el);

modules/angular2/test/core/linker/integration_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ export function main() {
16131613

16141614
it('should reflect property values on template comments',
16151615
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
1616-
var tpl = '<template [ng-if]="ctxBoolProp"></template>';
1616+
var tpl = '<template [ngIf]="ctxBoolProp"></template>';
16171617
tcb.overrideView(MyComp, new ViewMetadata({template: tpl, directives: [NgIf]}))
16181618

16191619
.createAsync(MyComp)

modules/angular2/test/web_workers/worker/renderer_integration_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function main() {
189189
it('should update any template comment property/attributes',
190190
inject([TestComponentBuilder, Renderer, AsyncTestCompleter],
191191
(tcb: TestComponentBuilder, renderer: Renderer, async) => {
192-
var tpl = '<template [ng-if]="ctxBoolProp"></template>';
192+
var tpl = '<template [ngIf]="ctxBoolProp"></template>';
193193
tcb.overrideView(MyComp, new ViewMetadata({template: tpl, directives: [NgIf]}))
194194

195195
.createAsync(MyComp)

0 commit comments

Comments
 (0)
X Tutup