-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
classList usage in dom.{add|remove|toggle}CssClass + minor changes #1237
Changes from all commits
ebe03bb
0d08edc
bb9f98e
930b5cf
62b7d8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ body { | |
#logo { | ||
padding: 15px; | ||
margin-left: 70px; | ||
border: none; | ||
} | ||
|
||
#editor-container { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,72 +39,86 @@ exports.createElement = function(tag, ns) { | |
document.createElement(tag); | ||
}; | ||
|
||
exports.setText = function(elem, text) { | ||
if (elem.innerText !== undefined) { | ||
elem.innerText = text; | ||
} | ||
if (elem.textContent !== undefined) { | ||
elem.textContent = text; | ||
if (typeof DOMTokenList === 'function') { | ||
exports.hasCssClass = function(el, name) { | ||
return el.classList.contains(name); | ||
}; | ||
|
||
exports.addCssClass = function(el, name) { | ||
el.classList.add(name); | ||
}; | ||
|
||
exports.removeCssClass = function(el, name) { | ||
el.classList.remove(name); | ||
} | ||
}; | ||
|
||
exports.hasCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g); | ||
return classes.indexOf(name) !== -1; | ||
}; | ||
exports.toggleCssClass = function(el, name) { | ||
el.classList.toggle(name); | ||
} | ||
|
||
/* | ||
* Add a CSS class to the list of classes on the given node | ||
*/ | ||
exports.addCssClass = function(el, name) { | ||
if (!exports.hasCssClass(el, name)) { | ||
el.className += " " + name; | ||
exports.setCssClass = function(el, name, include) { | ||
el.classList[include ? 'add' : 'remove'](name); | ||
} | ||
}; | ||
} | ||
else { | ||
exports.hasCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g); | ||
return classes.indexOf(name) !== -1; | ||
}; | ||
|
||
/* | ||
* Remove a CSS class from the list of classes on the given node | ||
*/ | ||
exports.removeCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g); | ||
while (true) { | ||
var index = classes.indexOf(name); | ||
if (index == -1) { | ||
break; | ||
/* | ||
* Add a CSS class to the list of classes on the given node | ||
*/ | ||
exports.addCssClass = function(el, name) { | ||
if (!exports.hasCssClass(el, name)) { | ||
el.className += " " + name; | ||
} | ||
classes.splice(index, 1); | ||
} | ||
el.className = classes.join(" "); | ||
}; | ||
}; | ||
|
||
exports.toggleCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g), add = true; | ||
while (true) { | ||
var index = classes.indexOf(name); | ||
if (index == -1) { | ||
break; | ||
/* | ||
* Remove a CSS class from the list of classes on the given node | ||
*/ | ||
exports.removeCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g); | ||
while (true) { | ||
var index = classes.indexOf(name); | ||
if (index == -1) { | ||
break; | ||
} | ||
classes.splice(index, 1); | ||
} | ||
add = false; | ||
classes.splice(index, 1); | ||
} | ||
if(add) | ||
classes.push(name); | ||
el.className = classes.join(" "); | ||
}; | ||
|
||
el.className = classes.join(" "); | ||
return add; | ||
}; | ||
exports.toggleCssClass = function(el, name) { | ||
var classes = el.className.split(/\s+/g), add = true; | ||
while (true) { | ||
var index = classes.indexOf(name); | ||
if (index == -1) { | ||
break; | ||
} | ||
add = false; | ||
classes.splice(index, 1); | ||
} | ||
if(add) | ||
classes.push(name); | ||
|
||
/* | ||
* Add or remove a CSS class from the list of classes on the given node | ||
* depending on the value of <tt>include</tt> | ||
*/ | ||
exports.setCssClass = function(node, className, include) { | ||
if (include) { | ||
exports.addCssClass(node, className); | ||
} else { | ||
exports.removeCssClass(node, className); | ||
} | ||
}; | ||
el.className = classes.join(" "); | ||
return add; | ||
}; | ||
|
||
/* | ||
* Add or remove a CSS class from the list of classes on the given node | ||
* depending on the value of <tt>include</tt> | ||
*/ | ||
exports.setCssClass = function(node, className, include) { | ||
if (include) { | ||
exports.addCssClass(node, className); | ||
} else { | ||
exports.removeCssClass(node, className); | ||
} | ||
}; | ||
} | ||
|
||
exports.hasCssString = function(id, doc) { | ||
var index = 0, sheets; | ||
|
@@ -143,7 +157,7 @@ exports.importCssString = function importCssString(cssText, id, doc) { | |
if (id) | ||
style.id = id; | ||
|
||
var head = doc.getElementsByTagName("head")[0] || doc.documentElement; | ||
var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; | ||
head.appendChild(style); | ||
} | ||
}; | ||
|
@@ -156,7 +170,7 @@ exports.importCssStylsheet = function(uri, doc) { | |
link.rel = 'stylesheet'; | ||
link.href = uri; | ||
|
||
var head = doc.getElementsByTagName("head")[0] || doc.documentElement; | ||
var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document without a head? Is it possible ? How ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
head.appendChild(link); | ||
} | ||
}; | ||
|
@@ -273,7 +287,7 @@ exports.getInnerText = function(el) { | |
if (document.body && "textContent" in document.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just test el.textContent ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain what do you mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought to replace whole thing with one line, but it can't be done, because el.textContent can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this way: if ("textContent" in document.documentElement) {
exports.setInnerText = function(el, innerText) {
el.textContent = innerText;
};
exports.getInnerText = function(el) {
return el.textContent;
};
}
else {
exports.setInnerText = function(el, innerText) {
el.innerText = innerText;
};
exports.getInnerText = function(el) {
return el.innerText;
};
} or at least: exports.setInnerText = function(el, innerText) {
if ("textContent" in el)
el.textContent = innerText;
else
el.innerText = innerText;
};
exports.getInnerText = function(el) {
if ("textContent" in el)
return el.textContent;
else
return el.innerText;
}; ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, textContent works on Webkit much slower then innerText. But I'm not sure innerText is future-proof:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like the first one if ("textContent" in document.documentElement) {
exports.setInnerText = function(el, innerText) {
.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way: var textProperty = ("innerText" in document.documentElement) ? "innerText" : "textContent";
exports.setInnerText = function(el, innerText) {
el[textProperty] = innerText;
};
exports.getInnerText = function(el) {
return el[textProperty];
}; |
||
return el.textContent; | ||
else | ||
return el.innerText || el.textContent || ""; | ||
return el.innerText; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/ajaxorg/pilot/commit/1b6251c4bc1d24406d041c2949c3d3f99a112425, undefined was coming from textContent right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we already detected that el has innerText, no need to use textContent here. |
||
}; | ||
|
||
exports.getParentWindow = function(document) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,10 +301,10 @@ if (window.postMessage && !useragent.isOldIE) { | |
|
||
|
||
exports.nextFrame = window.requestAnimationFrame || | ||
window.oRequestAnimationFrame || | ||
window.msRequestAnimationFrame || | ||
window.mozRequestAnimationFrame || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huge performance improvement he he )) |
||
window.webkitRequestAnimationFrame; | ||
window.webkitRequestAnimationFrame || | ||
window.msRequestAnimationFrame || | ||
window.oRequestAnimationFrame; | ||
|
||
if (exports.nextFrame) | ||
exports.nextFrame = exports.nextFrame.bind(window); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,9 @@ var DragdropHandler = function(mouseHandler) { | |
event.addListener(mouseTarget, "dragenter", function(e) { | ||
if (editor.getReadOnly()) | ||
return; | ||
var types = e.dataTransfer.types; | ||
if (types && Array.prototype.indexOf.call(types, 'Files') !== -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this check for presence of 'text/plain' instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're half right. Not instead of (anyway we should reject files, even with text/plain mime type), but along with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about cases like |
||
return; | ||
counter++; | ||
if (!dragSelectionMarker) { | ||
range = editor.getSelectionRange(); | ||
|
@@ -59,6 +62,9 @@ var DragdropHandler = function(mouseHandler) { | |
event.addListener(mouseTarget, "dragover", function(e) { | ||
if (editor.getReadOnly()) | ||
return; | ||
var types = e.dataTransfer.types; | ||
if (types && Array.prototype.indexOf.call(types, 'Files') !== -1) | ||
return; | ||
x = e.clientX; | ||
y = e.clientY; | ||
return event.preventDefault(e); | ||
|
@@ -73,6 +79,9 @@ var DragdropHandler = function(mouseHandler) { | |
event.addListener(mouseTarget, "dragleave", function(e) { | ||
if (editor.getReadOnly()) | ||
return; | ||
var types = e.dataTransfer.types; | ||
if (types && Array.prototype.indexOf.call(types, 'Files') !== -1) | ||
return; | ||
counter--; | ||
if (counter > 0) | ||
return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, DOMTokenList in IE10 is object, not function... What detection should be used here?
I think ("classList" in document.documentElement) would be good enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see danyaPostfactum@c93d1e2#lib/ace/lib/dom.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong with classList ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing's wrong, but see https://github.com/ajaxorg/pilot/commit/7a1651f87af08b0523e6c7cf034a1924ab3d48f3#lib/pilot/dom.js, c93d1e2#commitcomment-793749
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks. May be he is right. Ace calls these methods hardly ever ( but every mouse movement on gutter do it) to fight for every split millisecond. Also, we can not drop this code on dropping IE8. Anyway, I'll prepare jsperf test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gutter started to use
hasCssClass
after that discussion, so if classList is significantly faster it might be worth to use it. (I remember it was much faster on ff 3.6, but string performance have improved since then).Should i cherrypick this now, and we could look into
classList
later?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classList is not much faster as I know.