Changeset 27981 in webkit


Ignore:
Timestamp:
Nov 22, 2007 8:45:26 PM (16 years ago)
Author:
timothy@apple.com
Message:

Reviewed by Dan Bernstein.

Fix the Element.hasStyleClass and Element.removeStyleClass helpers
to not find and replace substrings, but whole class names at the
beginning or end of the string or surrounded by whitespace.

  • page/inspector/utilities.js:
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r27980 r27981  
     12007-11-22  Timothy Hatcher  <timothy@apple.com>
     2
     3        Reviewed by Dan Bernstein.
     4
     5        Fix the Element.hasStyleClass and Element.removeStyleClass helpers
     6        to not find and replace substrings, but whole class names at the
     7        beginning or end of the string or surrounded by whitespace.
     8
     9        * page/inspector/utilities.js:
     10
    1112007-11-22  Timothy Hatcher  <timothy@apple.com>
    212
  • trunk/WebCore/page/inspector/utilities.js

    r27877 r27981  
    9090Element.prototype.removeStyleClass = function(className)
    9191{
    92     if (this.hasStyleClass(className))
    93         this.className = this.className.replace(className, "");
     92    // Test for the simple case before using a RegExp.
     93    if (this.className === className) {
     94        this.className = "";
     95        return;
     96    }
     97
     98    var regex = new RegExp("(^|\\s+)" + className.escapeForRegExp() + "($|\\s+)");
     99    if (regex.test(this.className))
     100        this.className = this.className.replace(regex, " ");
    94101}
    95102
    96103Element.prototype.addStyleClass = function(className)
    97104{
    98     if (!this.hasStyleClass(className))
     105    if (className && !this.hasStyleClass(className))
    99106        this.className += (this.className.length ? " " + className : className);
    100107}
     
    102109Element.prototype.hasStyleClass = function(className)
    103110{
    104     return this.className.indexOf(className) !== -1;
     111    if (!className)
     112        return false;
     113    // Test for the simple case before using a RegExp.
     114    if (this.className === className)
     115        return true;
     116    var regex = new RegExp("(^|\\s)" + className.escapeForRegExp() + "($|\\s)");
     117    return regex.test(this.className);
    105118}
    106119
Note: See TracChangeset for help on using the changeset viewer.