Skip to content

Commit 503e012

Browse files
mxstbrsstur
authored andcommitted
[draft-js-import-markdown] Fix issue with complex image sources (#102)
* [draft-js-import-markdown] replicate issue with complex image sources * [draft-js-import-markdown] fix links with complex sources Makes `href` matching of links and images non-greedy. This fix is copied from markedjs/marked#600, and fixes the failing tests submitted in the previous commit. * Fix linting * make the tooling happy
1 parent 1d2abd9 commit 503e012

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

packages/draft-js-import-element/src/__tests__/stateFromElement-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ describe('stateFromElement', () => {
171171
],
172172
});
173173
});
174+
175+
it('should support images', () => {
176+
let imageNode = new ElementNode('img', [{name: 'src', value: 'imgur.com/asdf.jpg'}]);
177+
let wrapperElement = new ElementNode('div', [], [imageNode]);
178+
let contentState = stateFromElement(wrapperElement);
179+
let rawContentState = removeBlockKeys(convertToRaw(contentState));
180+
expect(rawContentState).toEqual({
181+
blocks: [{
182+
data: {},
183+
depth: 0,
184+
entityRanges: [{
185+
key: 0,
186+
length: 1,
187+
offset: 0,
188+
}],
189+
inlineStyleRanges: [],
190+
text: ' ',
191+
type: 'unstyled',
192+
}],
193+
entityMap: {
194+
// This is necessary due to flow not supporting non-string literal property keys
195+
// eslint-disable-next-line quote-props
196+
'0': {
197+
data: {
198+
src: 'imgur.com/asdf.jpg',
199+
},
200+
mutability: 'MUTABLE',
201+
type: 'IMAGE',
202+
},
203+
},
204+
});
205+
});
174206
});
175207

176208
describe('stateFromHTML', () => {

packages/draft-js-import-element/test/test-cases.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
{"entityMap":{"0":{"type":"IMAGE","mutability":"MUTABLE","data":{"src":"a.jpg", "alt": "a"}}},"blocks":[{"key":"8r91a","text":"\u00A0","type":"unstyled","data":{},"depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":1,"key":0}]}]}
5555
<p><img src="a.jpg" alt="a"/></p>
5656

57+
# Image Entity with complex src
58+
{"entityMap":{"0":{"type":"IMAGE","mutability":"MUTABLE","data":{"src":"https://spectrum.imgix.net/threads/c678032e-68a4-4e14-956d-abfa444a707d/Captura%20de%20pantalla%202017-08-19%20a%20la(s)%2000.14.09.png.0.29802431313299893", "alt": "a"}}},"blocks":[{"key":"8r91a","text":"\u00A0","type":"unstyled","data":{},"depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":1,"key":0}]}]}
59+
<p><img src="https://spectrum.imgix.net/threads/c678032e-68a4-4e14-956d-abfa444a707d/Captura%20de%20pantalla%202017-08-19%20a%20la(s)%2000.14.09.png.0.29802431313299893" alt="a"/></p>
60+
5761
# Entity
5862
{"entityMap":{"0":{"type":"LINK","mutability":"MUTABLE","data":{"url":"/"}}},"blocks":[{"key":"8r91j","text":"a","type":"unstyled","data":{},"depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":1,"key":0}]}]}
5963
<p><a href="/">a</a></p>

packages/draft-js-import-markdown/src/MarkdownParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ var inline = {
384384
};
385385

386386
inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
387-
inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
387+
inline._href = /\s*<?([\s\S]*)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
388388

389389
inline.link = replace(inline.link)('inside', inline._inside)(
390390
'href',

packages/draft-js-import-markdown/src/__tests__/stateFromMarkdown-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ describe('stateFromMarkdown', () => {
4343
},
4444
]);
4545
});
46+
it('should correctly move images with complex srcs', () => {
47+
const src = 'https://spectrum.imgix.net/threads/c678032e-68a4-4e14-956d-abfa444a707d/Captura%20de%20pantalla%202017-08-19%20a%20la(s)%2000.14.09.png.0.29802431313299893';
48+
let input = `![](${src})`;
49+
let contentState = stateFromMarkdown(input);
50+
let rawContentState = convertToRaw(contentState);
51+
let blocks = removeKeys(rawContentState.blocks);
52+
expect({
53+
...rawContentState,
54+
blocks,
55+
}).toEqual({
56+
entityMap: {
57+
// This is necessary due to flow not supporting non-string literal property keys
58+
// eslint-disable-next-line quote-props
59+
'0': {
60+
type: 'IMAGE',
61+
mutability: 'MUTABLE',
62+
data: {
63+
src: src,
64+
},
65+
},
66+
},
67+
blocks: [{
68+
text: ' ',
69+
type: 'unstyled',
70+
depth: 0,
71+
inlineStyleRanges: [],
72+
entityRanges: [{
73+
offset: 0,
74+
length: 1,
75+
key: 0,
76+
}],
77+
data: {},
78+
}],
79+
});
80+
});
4681
});
4782

4883
function removeKeys(blocks) {

0 commit comments

Comments
 (0)