mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(aio): preserve newlines when copying code
Before4f37f8643, 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). With4f37f8643we 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:
committed by
Matias Niemelä
parent
414c7e956b
commit
87206e1986
@@ -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(/</g, '<').replace(/>/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', () => {
|
||||
|
||||
Reference in New Issue
Block a user