Skip to content

Commit 4f37f86

Browse files
gkalpakhansl
authored andcommitted
fix(aio): switch from innerText to textContent to support older browsers
`innerText` is not supported in Firefox prior to v45. In most cases (at least the ones we are interested in), `innerText` and `textContent` work equally well, but `textContent` is more performant (as it doesn't require a reflow). From [MDN][1] on the differences of `innerText` vs `textContent`: > - [...] > - `innerText` is aware of style and will not return the text of hidden > elements, whereas `textContent` will. > - As `innerText` is aware of CSS styling, it will trigger a reflow, whereas > `textContent` will not. > - [...] [1]: https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent#Differences_from_innerText Fixes #17585
1 parent 3093c55 commit 4f37f86

13 files changed

Lines changed: 32 additions & 29 deletions

File tree

‎aio/content/examples/setup/src/app/app.component.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('AppComponent', function () {
2727
it('should have expected <h1> text', () => {
2828
fixture.detectChanges();
2929
const h1 = de.nativeElement;
30-
expect(h1.innerText).toMatch(/angular/i,
30+
expect(h1.textContent).toMatch(/angular/i,
3131
'<h1> should say something about "Angular"');
3232
});
3333
});

‎aio/content/examples/template-syntax/src/app/app.component.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class AppComponent implements AfterViewInit, OnInit {
111111
}
112112

113113
onSave(event: KeyboardEvent) {
114-
let evtMsg = event ? ' Event target is ' + (<HTMLElement>event.target).innerText : '';
114+
let evtMsg = event ? ' Event target is ' + (<HTMLElement>event.target).textContent : '';
115115
this.alert('Saved.' + evtMsg);
116116
if (event) { event.stopPropagation(); }
117117
}

‎aio/src/app/app.component.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,19 @@ describe('AppComponent', () => {
398398
it('should display a guide page (guide/pipes)', () => {
399399
locationService.go('guide/pipes');
400400
fixture.detectChanges();
401-
expect(docViewer.innerText).toMatch(/Pipes/i);
401+
expect(docViewer.textContent).toMatch(/Pipes/i);
402402
});
403403

404404
it('should display the api page', () => {
405405
locationService.go('api');
406406
fixture.detectChanges();
407-
expect(docViewer.innerText).toMatch(/API/i);
407+
expect(docViewer.textContent).toMatch(/API/i);
408408
});
409409

410410
it('should display a marketing page', () => {
411411
locationService.go('features');
412412
fixture.detectChanges();
413-
expect(docViewer.innerText).toMatch(/Features/i);
413+
expect(docViewer.textContent).toMatch(/Features/i);
414414
});
415415

416416
it('should update the document title', () => {
@@ -632,7 +632,7 @@ describe('AppComponent', () => {
632632
describe('footer', () => {
633633
it('should have version number', () => {
634634
const versionEl: HTMLElement = fixture.debugElement.query(By.css('aio-footer')).nativeElement;
635-
expect(versionEl.innerText).toContain(TestHttp.versionFull);
635+
expect(versionEl.textContent).toContain(TestHttp.versionFull);
636636
});
637637
});
638638

‎aio/src/app/embedded/code/code-example.component.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('CodeExampleComponent', () => {
6262
TestBed.overrideComponent(HostComponent, {
6363
set: {template: '<code-example title="Great Example"></code-example>'}});
6464
createComponent(oneLineCode);
65-
const actual = codeExampleDe.query(By.css('header')).nativeElement.innerText;
65+
const actual = codeExampleDe.query(By.css('header')).nativeElement.textContent;
6666
expect(actual).toBe('Great Example');
6767
});
6868

‎aio/src/app/embedded/code/code.component.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ describe('CodeComponent', () => {
116116
hostComponent.linenums = false;
117117
hostComponent.code = ' abc\n let x = text.split(\'\\n\');\n ghi\n\n jkl\n';
118118
fixture.detectChanges();
119-
const codeContent = codeComponentDe.nativeElement.querySelector('code').innerText;
119+
const codeContent = codeComponentDe.nativeElement.querySelector('code').textContent;
120120
expect(codeContent).toEqual('abc\n let x = text.split(\'\\n\');\nghi\n\njkl');
121121
});
122122

123123
it('should trim whitespace from the code before rendering', () => {
124124
hostComponent.linenums = false;
125125
hostComponent.code = '\n\n\n' + smallMultiLineCode + '\n\n\n';
126126
fixture.detectChanges();
127-
const codeContent = codeComponentDe.nativeElement.querySelector('code').innerText;
127+
const codeContent = codeComponentDe.nativeElement.querySelector('code').textContent;
128128
expect(codeContent).toEqual(codeContent.trim());
129129
});
130130

@@ -141,7 +141,7 @@ describe('CodeComponent', () => {
141141

142142
function getErrorMessage() {
143143
const missing: HTMLElement = codeComponentDe.nativeElement.querySelector('.code-missing');
144-
return missing ? missing.innerText : null;
144+
return missing ? missing.textContent : null;
145145
}
146146

147147
it('should not display "code-missing" class when there is some code', () => {

‎aio/src/app/embedded/code/code.component.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class CodeComponent implements OnChanges {
108108

109109
if (!this.code) {
110110
const src = this.path ? this.path + (this.region ? '#' + this.region : '') : '';
111-
const srcMsg = src ? ` for<br>${src}` : '.';
111+
const srcMsg = src ? ` for\n${src}` : '.';
112112
this.setCodeHtml(`<p class="code-missing">The code sample is missing${srcMsg}</p>`);
113113
return;
114114
}
@@ -129,8 +129,8 @@ export class CodeComponent implements OnChanges {
129129
}
130130

131131
doCopy() {
132-
// We take the innerText because we don't want it to be HTML encoded
133-
const code = this.codeContainer.nativeElement.innerText;
132+
// We take the textContent because we don't want it to be HTML encoded
133+
const code = this.codeContainer.nativeElement.textContent.trim();
134134
if (this.copier.copyText(code)) {
135135
this.logger.log('Copied code to clipboard:', code);
136136
// success snackbar alert

‎aio/src/app/embedded/current-location.component.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ describe('CurrentLocationComponent', () => {
2525

2626
it('should render the current location', () => {
2727
fixture.detectChanges();
28-
expect(element.innerText).toEqual('initial/url');
28+
expect(element.textContent).toEqual('initial/url');
2929

3030
locationService.urlSubject.next('next/url');
3131

3232
fixture.detectChanges();
33-
expect(element.innerText).toEqual('next/url');
33+
expect(element.textContent).toEqual('next/url');
3434
});
3535
});

‎aio/src/app/embedded/live-example/live-example.component.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('LiveExampleComponent', () => {
183183
testComponent(() => {
184184
const expectedTitle = 'live example';
185185
const anchor = getLiveExampleAnchor();
186-
expect(anchor.innerText).toBe(expectedTitle, 'anchor content');
186+
expect(anchor.textContent).toBe(expectedTitle, 'anchor content');
187187
expect(anchor.getAttribute('title')).toBe(expectedTitle, 'title');
188188
});
189189
});
@@ -193,7 +193,7 @@ describe('LiveExampleComponent', () => {
193193
setHostTemplate(`<live-example title="${expectedTitle}"></live-example>`);
194194
testComponent(() => {
195195
const anchor = getLiveExampleAnchor();
196-
expect(anchor.innerText).toBe(expectedTitle, 'anchor content');
196+
expect(anchor.textContent).toBe(expectedTitle, 'anchor content');
197197
expect(anchor.getAttribute('title')).toBe(expectedTitle, 'title');
198198
});
199199
});
@@ -203,7 +203,7 @@ describe('LiveExampleComponent', () => {
203203
setHostTemplate('<live-example title="ignore this title"></live-example>');
204204
testComponent(() => {
205205
const anchor = getLiveExampleAnchor();
206-
expect(anchor.innerText).toBe(liveExampleContent, 'anchor content');
206+
expect(anchor.textContent).toBe(liveExampleContent, 'anchor content');
207207
expect(anchor.getAttribute('title')).toBe(liveExampleContent, 'title');
208208
});
209209
});

‎aio/src/app/embedded/toc/toc.component.spec.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ describe('TocComponent', () => {
8383
it('should only display H2 and H3 TocItems', () => {
8484
tocService.tocList.next([tocItem('Heading A', 'h1'), tocItem('Heading B'), tocItem('Heading C', 'h3')]);
8585
fixture.detectChanges();
86-
const items = tocComponentDe.queryAllNodes(By.css('li'));
87-
expect(items.length).toBe(2);
88-
expect(items.find(item => item.nativeNode.innerText === 'Heading A')).toBeFalsy();
89-
expect(items.find(item => item.nativeNode.innerText === 'Heading B')).toBeTruthy();
90-
expect(items.find(item => item.nativeNode.innerText === 'Heading C')).toBeTruthy();
86+
87+
const tocItems = tocComponentDe.queryAllNodes(By.css('li'));
88+
const textContents = tocItems.map(item => item.nativeNode.textContent.trim());
89+
90+
expect(tocItems.length).toBe(2);
91+
expect(textContents.find(text => text === 'Heading A')).toBeFalsy();
92+
expect(textContents.find(text => text === 'Heading B')).toBeTruthy();
93+
expect(textContents.find(text => text === 'Heading C')).toBeTruthy();
9194
expect(setPage().tocH1Heading).toBeFalsy();
9295
});
9396

‎aio/src/app/layout/doc-viewer/doc-viewer.component.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
9797
// Only create TOC for docs with an <h1> title
9898
// If you don't want a TOC, add "no-toc" class to <h1>
9999
if (titleEl) {
100-
title = titleEl.innerText.trim();
100+
title = titleEl.textContent.trim();
101101
if (!/(no-toc|notoc)/i.test(titleEl.className)) {
102102
this.tocService.genToc(this.hostElement, docId);
103103
titleEl.insertAdjacentHTML('afterend', '<aio-toc class="embedded"></aio-toc>');

0 commit comments

Comments
 (0)