Skip to content

Commit

Permalink
Merge pull request #11314 from meeseeksmachine/auto-backport-of-pr-11…
Browse files Browse the repository at this point in the history
…310-on-3.1.x

Backport PR #11310 on branch 3.1.x (Added handling of '\r' ended files)
  • Loading branch information
jasongrout committed Oct 19, 2021
2 parents 6469e81 + 7e18aac commit a30d116
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/docregistry/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ export class Context<
content = model.toJSON();
} else {
content = model.toString();
if (this._useCRLF) {
content = content.replace(/\n/g, '\r\n');
if (this._lineEnding) {
content = content.replace(/\n/g, this._lineEnding);
}
}

Expand Down Expand Up @@ -672,11 +672,14 @@ export class Context<
let content = contents.content;
// Convert line endings if necessary, marking the file
// as dirty.
if (content.indexOf('\r') !== -1) {
this._useCRLF = true;
if (content.indexOf('\r\n') !== -1) {
this._lineEnding = '\r\n';
content = content.replace(/\r\n/g, '\n');
} else if (content.indexOf('\r') !== -1) {
this._lineEnding = '\r';
content = content.replace(/\r/g, '\n');
} else {
this._useCRLF = false;
this._lineEnding = null;
}
model.fromString(content);
if (initializeModel) {
Expand Down Expand Up @@ -871,7 +874,7 @@ or load the version on disk (revert)?`,
private _model: T;
private _modelDB: IModelDB;
private _path = '';
private _useCRLF = false;
private _lineEnding: string | null = null;
private _factory: DocumentRegistry.IModelFactory<T>;
private _contentsModel: Contents.IModel | null = null;
private _readyPromise: Promise<void>;
Expand Down
18 changes: 18 additions & 0 deletions packages/docregistry/test/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ describe('docregistry/context', () => {
expect(model.content).toBe('foo\nbar');
});

it('should should preserve CR line endings upon save', async () => {
await context.initialize(true);
await manager.contents.save(context.path, {
type: factory.contentType,
format: factory.fileFormat,
content: 'foo\rbar'
});
await context.revert();
await context.save();
const opts: Contents.IFetchOptions = {
format: factory.fileFormat,
type: factory.contentType,
content: true
};
const model = await manager.contents.get(context.path, opts);
expect(model.content).toBe('foo\rbar');
});

it('should should preserve CRLF line endings upon save', async () => {
await context.initialize(true);
await manager.contents.save(context.path, {
Expand Down

0 comments on commit a30d116

Please sign in to comment.