提交 fbb77a8e 编写于 作者: J Jesse Glick

Changed meaning of priority in Behaviour.specify.

Callers seem to expect that priority behaviors are applied before any behaviors in other selectors.
上级 81bdb4be
......@@ -61,4 +61,10 @@ public class BehaviorTest extends HudsonTestCase {
assertEquals("initial and appended yet different", r.getJavaScriptResult().toString());
}
public void testSelectorOrdering() throws Exception {
HtmlPage p = createWebClient().goTo("self/testSelectorOrdering");
ScriptResult r = p.executeJavaScript("document.getElementsBySelector('DIV.a')[0].innerHTML");
assertEquals("initial early counted! generic weevils! late", r.getJavaScriptResult().toString());
}
}
<!--
The MIT License
Copyright (c) 2012, CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout title="">
<l:main-panel>
<script>
Behaviour.specify("DIV", 'generic', 0, function (e) {
e.innerHTML += ' generic';
});
Behaviour.specify(".a", 'early', -100, function (e) {
e.innerHTML += ' early';
});
Behaviour.specify(".a", 'late', 100, function (e) {
e.innerHTML += ' late';
});
Behaviour.specify("DIV", 'zyzzyva', 0, function (e) {
e.innerHTML += ' weevils!';
});
Behaviour.specify("DIV", 'abacus', 0, function (e) {
e.innerHTML += ' counted!';
});
</script>
<div class="a">initial</div>
</l:main-panel>
</l:layout>
</j:jelly>
......@@ -33,30 +33,27 @@
*/
var storage = {};
var storage = [];
var Behaviour = {
/**
* Specifies something to do when an element matching a CSS selector is encountered.
* @param {String} selector a CSS selector triggering your behavior
* @param {String} id uniquely identifies this behavior among all behaviors for the selector; prevents duplicate registrations
* @param {Number} priority relative position of this behavior in case multiple apply; lower numbers applied first; alphabetical by id in case of tie; choose 0 if you do not care
* @param {String} id combined with selector, uniquely identifies this behavior; prevents duplicate registrations
* @param {Number} priority relative position of this behavior in case multiple apply to a given element; lower numbers applied first (sorted by id then selector in case of tie); choose 0 if you do not care
* @param {Function} behavior callback function taking one parameter, a (DOM) {@link Element}, and returning void
*/
specify : function(selector, id, priority, behavior) {
var forSelector = storage[selector];
if (forSelector == null) {
storage[selector] = forSelector = [];
}
for (var i = 0; i < forSelector.length; i++) {
if (forSelector[i].id == id) {
forSelector.splice(i, 1);
for (var i = 0; i < storage.length; i++) {
if (storage[i].selector == selector && storage[i].id == id) {
storage.splice(i, 1);
break;
}
}
forSelector.push({id: id, priority: priority, behavior: behavior});
forSelector.sort(function(a, b) {
storage.push({selector: selector, id: id, priority: priority, behavior: behavior});
storage.sort(function(a, b) {
var location = a.priority - b.priority;
return location != 0 ? location : a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
return location != 0 ? location : a.id < b.id ? -1 : a.id > b.id ? 1 : a.selector < b.selector ? -1 : a.selector > b.selector ? 1 : 0;
});
},
......@@ -103,16 +100,14 @@ var Behaviour = {
}
});
// Now those using specify:
for (var selector in storage) {
storage[selector]._each(function (registration) {
startNode._each(function (node) {
var list = findElementsBySelector(node, selector, includeSelf);
if (list.length > 0) {
list._each(registration.behavior);
}
});
storage._each(function (registration) {
startNode._each(function (node) {
var list = findElementsBySelector(node, registration.selector, includeSelf);
if (list.length > 0) {
list._each(registration.behavior);
}
});
}
});
},
addLoadEvent : function(func){
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册