forked from nhn/tui.editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: encode link url unnecessarily (nhn#1641)
* fix: decode url when executing addImage, addLink command * chore: add test case(addImage, addLink) * chore: apply code review * refactor: assertToContainHTML function * chore: add test case(converting the html block which has "=" attr value)
- Loading branch information
Showing
9 changed files
with
106 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,45 @@ | ||
const reURL = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})(\/([^\s]*))?$/g; | ||
const encodingRegExps = [/\(/g, /\)/g, /\[/g, /\]/g, /</g, />/g]; | ||
const encodedList = ['%28', '%29', '%5B', '%5D', '%3C', '%3E']; | ||
const escapedList = ['\\(', '\\)', '\\[', '\\]', '\\<', '\\>']; | ||
const encoderList = [ | ||
{ | ||
regExp: /\(/g, | ||
encoded: '%28', | ||
escaped: '\\(', | ||
}, | ||
{ | ||
regExp: /\)/g, | ||
encoded: '%29', | ||
escaped: '\\)', | ||
}, | ||
{ | ||
regExp: /\[/g, | ||
encoded: '%5B', | ||
escaped: '\\[', | ||
}, | ||
{ | ||
regExp: /\]/g, | ||
encoded: '%5D', | ||
escaped: '\\]', | ||
}, | ||
{ | ||
regExp: /</g, | ||
encoded: '%3C', | ||
escaped: '\\<', | ||
}, | ||
{ | ||
regExp: />/g, | ||
encoded: '%3E', | ||
escaped: '\\>', | ||
}, | ||
{ | ||
regExp: / /g, | ||
encoded: '%20', | ||
escaped: ' ', | ||
}, | ||
]; | ||
|
||
export function decodeURIGraceful(uri: string) { | ||
const uriList = uri.split(' '); | ||
|
||
return uriList | ||
.reduce<string[]>((decodedURIList, targetUri) => { | ||
let decodedURI = ''; | ||
|
||
try { | ||
decodedURI = decodeURIComponent(targetUri); | ||
} catch (e) { | ||
decodedURI = targetUri; | ||
} | ||
|
||
return decodedURIList.concat(decodedURI); | ||
}, []) | ||
.join('%20'); | ||
} | ||
|
||
export function encodeMarkdownText(text: string, encode: boolean) { | ||
const expectedValues = encode ? encodedList : escapedList; | ||
|
||
return encodingRegExps.reduce( | ||
(result, regExp, index) => result.replace(regExp, expectedValues[index]), | ||
text | ||
); | ||
export function escapeMarkdownText(text: string) { | ||
return encoderList.reduce((result, { regExp, escaped }) => result.replace(regExp, escaped), text); | ||
} | ||
|
||
export function decodeURL(text: string) { | ||
return text.replace(reURL, (matched) => encodeMarkdownText(decodeURIGraceful(matched), true)); | ||
export function encodeMarkdownText(text: string) { | ||
return encoderList.reduce((result, { regExp, encoded }) => result.replace(regExp, encoded), text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters