Skip to content

Commit

Permalink
add experimental region and insertRule
Browse files Browse the repository at this point in the history
  • Loading branch information
kangax authored and kangax committed Jul 19, 2008
1 parent d7f66f0 commit d5a96c9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
8 changes: 4 additions & 4 deletions element.methods.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Element#contains(@element, pattern) -> Boolean
* Element#containsText(@element, pattern) -> Boolean
* - @element(Element): element which content will be tested
* - pattern(String|RegExp): pattern to test element's content against
*
* Tests whether element's content contains specified string (or matches agains regular expression)
*
* $("myElement").contains("some text...");
* $("otherElement").contains(/(foo|bar)/i)
* $("myElement").containsText("some text...");
* $("otherElement").containsText(/(foo|bar)/i)
**/
Element.Methods.contains = function(element, pattern) {
Element.Methods.containsText = function(element, pattern) {
element = $(element);
if (!pattern) return false;
pattern = pattern.constructor == RegExp ? pattern : RegExp.escape(pattern);
Expand Down
23 changes: 23 additions & 0 deletions experimental/element_region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Element.Region = Class.create({
initialize: function(element) {
this.element = $(element);
var offset = this.element.cumulativeOffset();
var dim = this.element.getDimensions();
this.top = offset.top;
this.left = offset.left;
this.bottom = this.top + dim.height;
this.right = this.left + dim.width;
},
contains: function(other) {
return other.left >= this.left &&
other.right <= this.right &&
other.top >= this.top &&
other.bottom <= this.bottom;
},
intersectsWith: function(other) {
return ((other.left >= this.left && other.left <= this.right) ||
(other.right >= this.left && other.right <= this.right)) &&
((other.top >= this.top && other.top <= this.bottom) ||
(other.bottom >= this.top && other.bottom <= this.bottom));
}
});
24 changes: 24 additions & 0 deletions insertRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var insertRule = (function(){
var ss = document.styleSheets[0],
reSelectorRule = /([^\{]*)\{([^\}]*)\}/;
if (!ss) {
var el = document.createElement('style');
el.type = 'text/css';
el.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(el);
ss = document.styleSheets[0];
}
if (ss.addRule) {
return function(rule) { // IE
var match = rule.match(reSelectorRule);
if (!match) return;
var ss = document.styleSheets[document.styleSheets.length-1];
ss.addRule(match[1], match[2], ss.rules.length);
}
}
return function(rule) { // Others
if (!rule.match(reSelectorRule)) return;
var ss = document.styleSheets[document.styleSheets.length-1];
ss.insertRule(rule, ss.cssRules.length);
}
})();

0 comments on commit d5a96c9

Please sign in to comment.