mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
fix base64 encoding to handle non utf8 characters
This commit is contained in:
@@ -6,6 +6,7 @@ import React from 'react'
|
|||||||
import Text from './text'
|
import Text from './text'
|
||||||
import includes from 'lodash/includes'
|
import includes from 'lodash/includes'
|
||||||
import keycode from 'keycode'
|
import keycode from 'keycode'
|
||||||
|
import Base64 from '../utils/base64'
|
||||||
import { IS_FIREFOX } from '../utils/environment'
|
import { IS_FIREFOX } from '../utils/environment'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -209,7 +210,6 @@ class Content extends React.Component {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
onCutCopy = (e) => {
|
onCutCopy = (e) => {
|
||||||
// debugger
|
|
||||||
const native = window.getSelection()
|
const native = window.getSelection()
|
||||||
if (!native.rangeCount) return
|
if (!native.rangeCount) return
|
||||||
|
|
||||||
@@ -217,7 +217,7 @@ class Content extends React.Component {
|
|||||||
const { fragment } = state
|
const { fragment } = state
|
||||||
const raw = Raw.serializeNode(fragment)
|
const raw = Raw.serializeNode(fragment)
|
||||||
const string = JSON.stringify(raw)
|
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
|
// 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.
|
// 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 regexp = /data-fragment="([^\s]+)"/
|
||||||
const matches = regexp.exec(paste.html)
|
const matches = regexp.exec(paste.html)
|
||||||
const [ full, encoded ] = matches
|
const [ full, encoded ] = matches
|
||||||
const string = window.atob(encoded)
|
const string = Base64.decode(encoded)
|
||||||
const json = JSON.parse(string)
|
const json = JSON.parse(string)
|
||||||
const fragment = Raw.deserialize(json)
|
const fragment = Raw.deserialize(json)
|
||||||
let { state } = this.props
|
let { state } = this.props
|
||||||
|
31
lib/utils/base64.js
Normal file
31
lib/utils/base64.js
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user