From 04412052176425705a5eb9c64282bfd0e9ca63a0 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Fri, 17 Aug 2018 19:27:57 -0700 Subject: [PATCH] decode language html correctly --- components/Editor.js | 14 ++++++++++---- lib/util.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index 7b08792..a2ca44b 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -32,7 +32,7 @@ import { DEFAULT_LANGUAGE } from '../lib/constants' import { serializeState, getQueryStringState } from '../lib/routing' -import { getState, escapeHtml } from '../lib/util' +import { getState, escapeHtml, unescapeHtml } from '../lib/util' const saveButtonOptions = { button: true, @@ -89,13 +89,19 @@ class Editor extends React.Component { console.log(e) } - // Load from localStorage and then URL params - this.setState({ + const newState = { + // Load from localStorage ...getState(localStorage), + // and then URL params ...initialState, loading: false, online: Boolean(window && window.navigator && window.navigator.onLine) - }) + } + + // Makes sure the slash in encoded in application/X is decoded + newState.language = unescapeHtml(newState.language) + + this.setState(newState) window.addEventListener('offline', this.setOffline) window.addEventListener('online', this.setOnline) diff --git a/lib/util.js b/lib/util.js index 82369b0..a4435a4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -12,12 +12,25 @@ const parse = v => { } } +// https://gist.github.com/alexgibson/1704515 +// TODO use https://github.com/sindresorhus/escape-goat/ export const escapeHtml = s => { if (typeof s === 'string') { return s .replace(//g, '>') .replace(/\//g, '/') + .replace(/&/g, '&') + } +} + +export const unescapeHtml = s => { + if (typeof s === 'string') { + return s + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(///g, '/') + .replace(/&/g, '&'); } }