Browse Source

Merge pull request #11313 from meeseeksmachine/auto-backport-of-pr-11310-on-3.2.x

Backport PR #11310 on branch 3.2.x (Added handling of '\r' ended files)
Jason Grout 3 years ago
parent
commit
76211464d6
2 changed files with 27 additions and 6 deletions
  1. 9 6
      packages/docregistry/src/context.ts
  2. 18 0
      packages/docregistry/test/context.spec.ts

+ 9 - 6
packages/docregistry/src/context.ts

@@ -599,8 +599,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);
       }
     }
 
@@ -686,11 +686,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) {
@@ -887,7 +890,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>;

+ 18 - 0
packages/docregistry/test/context.spec.ts

@@ -339,6 +339,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, {