Skip to content

Commit

Permalink
use a better regexp for character escaping in selectors (id, name, cl…
Browse files Browse the repository at this point in the history
…ass, attributes), fixed ID optimizations, added missing quotes for some keyword
  • Loading branch information
dperini committed Nov 29, 2012
1 parent caee2c4 commit 91db52f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/nwmatcher-base.js
Expand Up @@ -259,13 +259,13 @@

_byId = !('fileSize' in doc) ?
function(id, from) {
id = id.replace(/\\/g, '');
id = id.replace(/\\([^\\]{1})/g, '$1');
return from.getElementById && from.getElementById(id) ||
byIdRaw(id, from.getElementsByTagName('*'));
} :
function(id, from) {
var element = null;
id = id.replace(/\\/g, '');
id = id.replace(/\\([^\\]{1})/g, '$1');
if (XML_DOCUMENT || from.nodeType != 9) {
return byIdRaw(id, from.getElementsByTagName('*'));
}
Expand Down Expand Up @@ -367,6 +367,7 @@
name = match[1].toLowerCase();
if (match[2] && match[4] && (type = Operators[match[2]])) {
test = name in INSENSITIVE_MAP;
match[4] = match[4].replace(/(\x22|\x27)/g, '\\$1');
match[4] = match[4].replace(/\\([0-9a-f]{2,2})/, '\\x$1');
type = type.replace(/\%m/g, test ? match[4].toLowerCase() : match[4]);
expr = 'n=s.getAttr(e,"' + name + '")' + (test ? '.toLowerCase()' : '') + ';';
Expand All @@ -381,6 +382,7 @@
}
} else if (match[2] == '!=' || match[2] == '=') {
expr = 'n=e.attributes["' + name + '"];';
match[4] = match[4].replace(/(\x22|\x27)/g, '\\$1');
type = 'n&&n.value' + match[2] + '="' + match[4] + '"';
} else {
expr = '';
Expand Down Expand Up @@ -552,12 +554,9 @@
if ('#' + token == selector) {
callback && callback(element);
elements = [ element ];
}
if (/[>+~]/.test(selector)) {
} else if (/[>+~]/.test(selector)) {
from = element.parentNode;
} else {
selector = selector.replace('#' + token, '*');
lastPosition -= token.length + 1;
from = element;
}
} else elements = [ ];
Expand Down
25 changes: 12 additions & 13 deletions src/nwmatcher.js
Expand Up @@ -398,9 +398,9 @@

// dynamic attributes that needs to be checked against original HTML value
ATTR_DEFAULT = {
value: 'defaultValue',
checked: 'defaultChecked',
selected: 'defaultSelected'
'value': 'defaultValue',
'checked': 'defaultChecked',
'selected': 'defaultSelected'
},

// attribute referencing URI data values need special treatment in IE
Expand Down Expand Up @@ -591,13 +591,13 @@
// @return reference or null
_byId = !BUGGY_GEBID ?
function(id, from) {
id = id.replace(/\\/g, '');
id = id.replace(/\\([^\\]{1})/g, '$1');
return from.getElementById && from.getElementById(id) ||
byIdRaw(id, from.getElementsByTagName('*'));
} :
function(id, from) {
var element = null;
id = id.replace(/\\/g, '');
id = id.replace(/\\([^\\]{1})/g, '$1');
if (XML_DOCUMENT || from.nodeType != 9) {
return byIdRaw(id, from.getElementsByTagName('*'));
}
Expand Down Expand Up @@ -669,15 +669,15 @@
// @return array
byName =
function(name, from) {
return select('[name="' + name.replace(/\\/g, '') + '"]', from);
return select('[name="' + name.replace(/\\([^\\]{1})/g, '$1') + '"]', from);
},

// elements by class (raw)
// @return array
byClassRaw =
function(name, from) {
var i = -1, j = i, data = [ ], element, elements = _byTag('*', from), n;
name = ' ' + (QUIRKS_MODE ? name.toLowerCase() : name).replace(/\\/g, '') + ' ';
name = ' ' + (QUIRKS_MODE ? name.toLowerCase() : name).replace(/\\([^\\]{1})/g, '$1') + ' ';
while ((element = elements[++i])) {
n = XML_DOCUMENT ? element.getAttribute('class') : element.className;
if (n && n.length && (' ' + (QUIRKS_MODE ? n.toLowerCase() : n).
Expand All @@ -693,7 +693,7 @@
_byClass =
function(name, from) {
return (BUGGY_GEBCN || BUGGY_QUIRKS_GEBCN || XML_DOCUMENT || !from.getElementsByClassName) ?
byClassRaw(name, from) : slice.call(from.getElementsByClassName(name.replace(/\\/g, '')), 0);
byClassRaw(name, from) : slice.call(from.getElementsByClassName(name.replace(/\\([^\\]{1})/g, '$1')), 0);
},

// publicly exposed byClass
Expand Down Expand Up @@ -990,10 +990,12 @@
// case treatment depends on document
HTML_TABLE['class'] = QUIRKS_MODE ? 1 : 0;
// replace escaped values and HTML entities
match[4] = match[4].replace(/(\x22|\x27)/g, '\\$1');
match[4] = match[4].replace(/\\([0-9a-f]{2,2})/, '\\x$1');
test = (XML_DOCUMENT ? XHTML_TABLE : HTML_TABLE)[expr.toLowerCase()];
type = type.replace(/\%m/g, test ? match[4].toLowerCase() : match[4]);
} else if (match[2] == '!=' || match[2] == '=') {
match[4] = match[4].replace(/(\x22|\x27)/g, '\\$1');
type = 'n' + match[2] + '="' + match[4] + '"';
}

Expand Down Expand Up @@ -1137,7 +1139,7 @@
return '';
} else {
if ('compatMode' in doc) {
source = 'if(!' + compile([expr], '', false) + '(e,s,r,d,h,g)){' + source + '}';
source = 'if(!' + compile(expr, '', false) + '(e,s,r,d,h,g)){' + source + '}';
} else {
source = 'if(!s.match(e, "' + expr.replace(/\x22/g, '\\"') + '",g)){' + source +'}';
}
Expand Down Expand Up @@ -1432,12 +1434,9 @@
if ('#' + token == selector) {
callback && callback(element);
elements = [ element ];
}
if (/[>+~]/.test(selector)) {
} else if (/[>+~]/.test(selector)) {
from = element.parentNode;
} else {
selector = selector.replace('#' + token, '*');
lastPosition -= token.length + 1;
from = element;
}
} else elements = [ ];
Expand Down

0 comments on commit 91db52f

Please sign in to comment.