fix(aio): preserve newlines when copying code

Before 4f37f8643, we were using `innerText` to retrieved the code content for
copying. This preserved the text layout (including newlines), but suffered from
other issues (browser support, performance). With 4f37f8643 we switched to
`textContent`, which works well except in the following case:
When `prettify` formats the code to have line numbers, it removes the newlines
and uses `<li>` elements instead. This affects `textContent`.

This commit fixes this by keeping a reference of the code as text and using that
for copying.

Fixes #17659
This commit is contained in:
Georgios Kalpakas
2017-06-22 00:56:11 +03:00
committed by Matias Niemelä
parent 414c7e956b
commit 87206e1986
2 changed files with 37 additions and 3 deletions
@@ -218,7 +218,29 @@ describe('CodeComponent', () => {
const copierService: CopierService = TestBed.get(CopierService);
const spy = spyOn(copierService, 'copyText');
getButton().click();
expect(spy.calls.argsFor(0)[0]).toEqual(oneLineCode, 'after click');
expect(spy.calls.argsFor(0)[0]).toBe(oneLineCode, 'after click');
});
it('should preserve newlines in the copied code', () => {
const copierService: CopierService = TestBed.get(CopierService);
const spy = spyOn(copierService, 'copyText');
const expectedCode = smallMultiLineCode.trim().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
let actualCode;
hostComponent.code = smallMultiLineCode;
[false, true, 42].forEach(linenums => {
hostComponent.linenums = linenums;
fixture.detectChanges();
codeComponent.ngOnChanges();
getButton().click();
actualCode = spy.calls.mostRecent().args[0];
expect(actualCode).toBe(expectedCode, `when linenums=${linenums}`);
expect(actualCode.match(/\r?\n/g).length).toBe(5);
spy.calls.reset();
});
});
it('should display a message when copy succeeds', () => {