<html>
<head>
<style>
#editor {
border: 1px solid black;
min-height: 150px;
}
</style>
</head>
<body>
<p>DOM Changes in Editor: <span id="counter">0</span></p>
<div id="editor" contenteditable="true" >
<h1>Composition in Shadow Root</h1>
<p>This is to try the capabilities <b>of</b> using the <i>Shadow</i> DOM for IME so that the
DOM is not updated during character composition.</p>
</div>
<script>
var editor = document.getElementById('editor'),
beforeCompositionRange,
compositionShadowRoot,
selection = window.getSelection(),
getRange = function (range) {
var selectedElement,
pathElement,
rangeDes = {
start: {
path: [],
offset: range.startOffset
},
end: {
path: [],
offset: range.endOffset
}
};
selectedElement = range.startContainer;
pathElement = 0;
while (selectedElement != range.commonAncestorContainer) {
while (selectedElement.previousSibling) {
pathElement += 1;
selectedElement = selectedElement.previousSibling;
}
rangeDes.start.path.push(pathElement);
selectedElement = selectedElement.parentElement;
pathElement = 0;
}
selectedElement = range.endContainer;
pathElement = 0;
while (selectedElement != range.commonAncestorContainer) {
while (selectedElement.previousSibling) {
pathElement += 1;
selectedElement = selectedElement.previousSibling;
}
rangeDes.end.path.push(pathElement);
selectedElement = selectedElement.parentElement;
pathElement = 0;
}
return rangeDes;
},
setRange = function (rangeDes, element) {
var range = document.createRange(),
selectedElement,
findPath;
selectedElement = element;
findPath = rangeDes.start.path.slice();
while(findPath.length > 0) {
selectedElement = selectedElement.childNodes[findPath.pop()];
}
range.setStart(selectedElement, rangeDes.start.offset);
selectedElement = element;
findPath = rangeDes.end.path.slice();
while(findPath.length > 0) {
selectedElement = selectedElement.childNodes[findPath.pop()];
}
range.setEnd(selectedElement, rangeDes.end.offset);
return range;
},
moveCaret = function () {
var beforeCompositionRange = selection.getRangeAt(0),
rangeDes = getRange(beforeCompositionRange),
pathElement = 0,
moveCaretBack = function () {
shadow.innerHTML ='<content>';
selection.removeAllRanges();
selection.addRange(beforeCompositionRange);
},
compositionShadowRoot = beforeCompositionRange.commonAncestorContainer,
shadow, newRange, selectedElement;
if (compositionShadowRoot.nodeType===3) {
// start and end is in the same text node
selectedElement = compositionShadowRoot;
while (selectedElement.previousSibling) {
pathElement += 1;
selectedElement = selectedElement.previousSibling;
}
rangeDes.start.path.push(pathElement);
rangeDes.end.path.push(pathElement);
compositionShadowRoot = compositionShadowRoot.parentElement;
}
shadow = compositionShadowRoot.createShadowRoot(),
cEShadow = document.createElement('span');
cEShadow.setAttribute('contentEditable','true');
cEShadow.innerHTML=compositionShadowRoot.innerHTML;
shadow.appendChild(cEShadow);
newRange = setRange(rangeDes, cEShadow);
selection.removeAllRanges();
selection.addRange(newRange);
cEShadow.addEventListener('compositionend',moveCaretBack);
};
editor.addEventListener('compositionstart',moveCaret);
// Observe changes
var observer = new MutationObserver(function(mutations) {
changeCounter += 1;
changeCounterElement.innerHTML = changeCounter;
}), changeCounter = 0, changeCounterElement = document.getElementById('counter');
observer.observe(editor, { attributes: true, childList: true, characterData: true, subtree: true });
</script>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |