From fa17e9ad9da41440905c0fc4982ecf2cd2a84a5c Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Thu, 7 Jun 2018 10:17:08 -0400 Subject: [PATCH] Fixed dev-mode error relating to adjacent images (#1731) --- app/components/markdown/transform.js | 12 +++- app/components/markdown/transform.test.js | 78 +++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/app/components/markdown/transform.js b/app/components/markdown/transform.js index 0b01ddd4e..a7e5aa898 100644 --- a/app/components/markdown/transform.js +++ b/app/components/markdown/transform.js @@ -1,6 +1,8 @@ // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. +import {Node} from 'commonmark'; + /* eslint-disable no-underscore-dangle */ // Add indices to the items of every list @@ -185,7 +187,15 @@ function pullOutImage(image) { // Copies a Node without its parent, children, or siblings function copyNodeWithoutNeighbors(node) { // commonmark uses classes so it takes a bit of work to copy them - const copy = Object.assign(Object.create(Reflect.getPrototypeOf(node)), node); + const copy = new Node(); + + for (const key in node) { + if (!node.hasOwnProperty(key)) { + continue; + } + + copy[key] = node[key]; + } copy._parent = null; copy._firstChild = null; diff --git a/app/components/markdown/transform.test.js b/app/components/markdown/transform.test.js index 78152c016..8284e8611 100644 --- a/app/components/markdown/transform.test.js +++ b/app/components/markdown/transform.test.js @@ -1977,6 +1977,84 @@ describe('Components.Markdown.transform', () => { assert.equal(astToString(actual), astToString(expected)); assert.deepStrictEqual(actual, expected); }); + + it('adjacent images and text', () => { + const input = makeAst({ + type: 'document', + children: [{ + type: 'paragraph', + children: [{ + type: 'text', + literal: 'First:', + }, { + type: 'softbreak', + }, { + type: 'image', + destination: 'http://example.com/image', + children: [{ + type: 'text', + literal: 'Image', + }], + }, { + type: 'softbreak', + }, { + type: 'text', + literal: 'Second:', + }, { + type: 'softbreak', + }, { + type: 'image', + destination: 'http://example.com/image', + children: [{ + type: 'text', + literal: 'Image', + }], + }], + }], + }); + const expected = makeAst({ + type: 'document', + children: [{ + type: 'paragraph', + children: [{ + type: 'text', + literal: 'First:', + }, { + type: 'softbreak', + }], + }, { + type: 'image', + destination: 'http://example.com/image', + children: [{ + type: 'text', + literal: 'Image', + }], + }, { + type: 'paragraph', + continue: true, + children: [{ + type: 'softbreak', + }, { + type: 'text', + literal: 'Second:', + }, { + type: 'softbreak', + }], + }, { + type: 'image', + destination: 'http://example.com/image', + children: [{ + type: 'text', + literal: 'Image', + }], + }], + }); + const actual = pullOutImages(input); + + assert.ok(verifyAst(actual)); + assert.equal(astToString(actual), astToString(expected)); + assert.deepStrictEqual(actual, expected); + }); }); });