From 9d9a530d44bc73d82405a09e3b3129618a420a5d Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Mon, 13 Jul 2015 19:33:04 -0400 Subject: [PATCH] compatibility fixes for IE10 --- src/js/editor/editor.js | 11 ++++++----- src/js/utils/element-utils.js | 22 ++++++++++++++++++++++ src/js/utils/event-emitter.js | 2 +- src/js/utils/string-utils.js | 11 +++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/js/utils/string-utils.js diff --git a/src/js/editor/editor.js b/src/js/editor/editor.js index 4c788b388..3ea0e029e 100644 --- a/src/js/editor/editor.js +++ b/src/js/editor/editor.js @@ -25,6 +25,7 @@ import MobiledocRenderer from '../renderers/mobiledoc'; import { toArray, merge, mergeWithOptions } from 'content-kit-utils'; import { detectParentNode } from '../utils/dom-utils'; +import { getData, setData } from '../utils/element-utils'; var defaults = { placeholder: 'Write here...', @@ -150,11 +151,10 @@ function getNonTextBlocks(blockTypeSet, post) { function clearChildNodes(element) { while (element.childNodes.length) { - element.childNodes[0].remove(); + element.removeChild(element.childNodes[0]); } } - /** * @class Editor * An individual Editor @@ -333,10 +333,11 @@ merge(Editor.prototype, { }, applyPlaceholder() { - var dataset = this.element.dataset; const placeholder = this.placeholder; - if (placeholder && !dataset.placeholder) { - dataset.placeholder = placeholder; + const existingPlaceholder = getData(this.element, 'placeholder'); + + if (placeholder && !existingPlaceholder) { + setData(this.element, 'placeholder', placeholder); } }, diff --git a/src/js/utils/element-utils.js b/src/js/utils/element-utils.js index 15ce55dff..b4903fa9d 100644 --- a/src/js/utils/element-utils.js +++ b/src/js/utils/element-utils.js @@ -1,3 +1,5 @@ +import { dasherize } from 'content-kit-editor/utils/string-utils'; + function createDiv(className) { var div = document.createElement('div'); if (className) { @@ -114,7 +116,27 @@ function positionElementToRightOf(element, rightOfElement) { return positionElementToRect(element, rightOfElementRect, -verticalCenter, -rightOfElement.offsetWidth - elementMargin); } +function getData(element, name) { + if (element.dataset) { + return element.dataset[name]; + } else { + const dataName = dasherize(name); + return element.getAttribute(dataName); + } +} + +function setData(element, name, value) { + if (element.dataset) { + element.dataset[name] = value; + } else { + const dataName = dasherize(name); + return element.setAttribute(dataName, value); + } +} + export { + getData, + setData, createDiv, hideElement, showElement, diff --git a/src/js/utils/event-emitter.js b/src/js/utils/event-emitter.js index 4941add6a..7518555fe 100644 --- a/src/js/utils/event-emitter.js +++ b/src/js/utils/event-emitter.js @@ -25,4 +25,4 @@ var EventEmitter = { } }; -export default EventEmitter; \ No newline at end of file +export default EventEmitter; diff --git a/src/js/utils/string-utils.js b/src/js/utils/string-utils.js new file mode 100644 index 000000000..20b2f88b5 --- /dev/null +++ b/src/js/utils/string-utils.js @@ -0,0 +1,11 @@ +/* + * @param {String} string + * @return {String} a dasherized string. 'modelIndex' -> 'model-index', etc + */ +export function dasherize(string) { + return string.replace(/[A-Z]/g, (match, offset) => { + const lower = match.toLowerCase(); + + return (offset === 0 ? lower : '-' + lower); + }); +}