Permalink
Browse files

add experimental region and insertRule

  • Loading branch information...
1 parent d7f66f0 commit d5a96c93a87042c1502c8aabb3556e6b81aed296 kangax committed Jul 19, 2008
Showing with 51 additions and 4 deletions.
  1. +4 −4 element.methods.js
  2. +23 −0 experimental/element_region.js
  3. +24 −0 insertRule.js
View
@@ -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);
@@ -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));
+ }
+});
View
@@ -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.