1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-21 23:53:50 +01:00

fix base64 encoding to handle non utf8 characters

This commit is contained in:
Ian Storm Taylor 2016-07-22 14:12:31 -07:00
parent 2471f7f285
commit 9fc49f9ea5
2 changed files with 34 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import React from 'react'
import Text from './text'
import includes from 'lodash/includes'
import keycode from 'keycode'
import Base64 from '../utils/base64'
import { IS_FIREFOX } from '../utils/environment'
/**
@ -209,7 +210,6 @@ class Content extends React.Component {
*/
onCutCopy = (e) => {
// debugger
const native = window.getSelection()
if (!native.rangeCount) return
@ -217,7 +217,7 @@ class Content extends React.Component {
const { fragment } = state
const raw = Raw.serializeNode(fragment)
const string = JSON.stringify(raw)
const encoded = window.btoa(string)
const encoded = Base64.encode(string)
// Wrap the first character of the selection in a span that has the encoded
// fragment attached as an attribute, so it will show up in the copied HTML.
@ -336,7 +336,7 @@ class Content extends React.Component {
const regexp = /data-fragment="([^\s]+)"/
const matches = regexp.exec(paste.html)
const [ full, encoded ] = matches
const string = window.atob(encoded)
const string = Base64.decode(encoded)
const json = JSON.parse(string)
const fragment = Raw.deserialize(json)
let { state } = this.props

31
lib/utils/base64.js Normal file
View File

@ -0,0 +1,31 @@
/**
* Encode a `string` as Base64.
*
* @param {String} string
* @return {String}
*/
function encode(string) {
return window.btoa(window.unescape(window.encodeURIComponent(string)))
}
/**
* Decode a `string` as Base64.
*
* @param {String} string
* @return {String}
*/
function decode(string) {
return window.decodeURIComponent(window.escape(window.atob(string)))
}
/**
* Export.
*/
export default {
encode,
decode
}