trim leading/trailing <br> from pasted content
jywarren opened this issue · comments
Following up from bevacqua/domador#7 (comment) -- related to inserted <br>
when pasting content copied from a webpage (even a single word) in at least Chrome.
Looking here: http://stackoverflow.com/questions/6035071/intercept-paste-event-in-javascript
And attempting to insert resulting intercepted text at the current insertion point here: https://github.com/bevacqua/woofmark/blob/master/src/InputHistory.js#L183
Worked on this a bit but got stuck; I modified handlePaste()
in InputHistory.js
like this:
function handlePaste (state, e) {
if (state.inputState && state.inputState.text !== state.surface.read(state.inputMode) && state.refreshing === null) {
var pastedText;
if (window.clipboardData && window.clipboardData.getData) { // IE
pastedText = window.clipboardData.getData('Text');
} else if (e.clipboardData && e.clipboardData.getData) {
pastedText = e.clipboardData.getData('text/plain');
}
var chunks = state.inputState.getChunks();
pastedText = pastedText.replace(/(\A<br\s?\/?\w?>)|(<br\s?\/?\w?>\z)/g, '');
chunks.before += pastedText;
state.inputState.setChunks(chunks);
state.historyMode = 'paste';
state.saveState();
state.refreshState();
}
return false;
}
I successfully intercept the paste, but modifying chunks
is not working; perhaps modifying state.inputState.getChunks()
is not right.
I'll circle back to this later.