Skip to content

Commit

Permalink
Merge pull request #1612 from kawmra/fix-title-decoding
Browse files Browse the repository at this point in the history
Properly decoding for the title of pasted website
  • Loading branch information
Rokt33r authored Mar 5, 2018
2 parents 96fbd75 + 4c99429 commit 7d3d96a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
28 changes: 27 additions & 1 deletion browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import copyImage from 'browser/main/lib/dataApi/copyImage'
import { findStorage } from 'browser/lib/findStorage'
import fs from 'fs'
import eventEmitter from 'browser/main/lib/eventEmitter'
import iconv from 'iconv-lite'
const { ipcRenderer } = require('electron')

CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
Expand Down Expand Up @@ -324,7 +325,7 @@ export default class CodeEditor extends React.Component {
fetch(pastedTxt, {
method: 'get'
}).then((response) => {
return (response.text())
return this.decodeResponse(response)
}).then((response) => {
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
const value = editor.getValue()
Expand All @@ -342,6 +343,31 @@ export default class CodeEditor extends React.Component {
})
}

decodeResponse (response) {
const headers = response.headers
const _charset = headers.has('content-type')
? this.extractContentTypeCharset(headers.get('content-type'))
: undefined
return response.arrayBuffer().then((buff) => {
return new Promise((resolve, reject) => {
try {
const charset = _charset !== undefined && iconv.encodingExists(_charset) ? _charset : 'utf-8'
resolve(iconv.decode(new Buffer(buff), charset).toString())
} catch (e) {
reject(e)
}
})
})
}

extractContentTypeCharset (contentType) {
return contentType.split(';').filter((str) => {
return str.trim().toLowerCase().startsWith('charset')
}).map((str) => {
return str.replace(/['"]/g, '').split('=')[1]
})[0]
}

render () {
const { className, fontSize } = this.props
let fontFamily = this.props.fontFamily
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"electron-gh-releases": "^2.0.2",
"flowchart.js": "^1.6.5",
"font-awesome": "^4.3.0",
"iconv-lite": "^0.4.19",
"immutable": "^3.8.1",
"js-sequence-diagrams": "^1000000.0.6",
"katex": "^0.8.3",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3414,6 +3414,10 @@ iconv-lite@0.4.13, iconv-lite@~0.4.13:
version "0.4.13"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"

iconv-lite@^0.4.19:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"

iconv-lite@~0.2.11:
version "0.2.11"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.2.11.tgz#1ce60a3a57864a292d1321ff4609ca4bb965adc8"
Expand Down

0 comments on commit 7d3d96a

Please sign in to comment.