Browse files

use a better regexp for character escaping in selectors (id, name, cl…

…ass, attributes), fixed ID optimizations, added missing quotes for some keyword
  • Loading branch information...
1 parent caee2c4 commit 91db52f9df2860c181b9da5746ac2bcfc87e844f @dperini committed Nov 29, 2012
Showing with 17 additions and 19 deletions.
  1. +5 −6 src/nwmatcher-base.js
  2. +12 −13 src/nwmatcher.js
View
11 src/nwmatcher-base.js
@@ -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('*'));
}
@@ -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()' : '') + ';';
@@ -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 = '';
@@ -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 = [ ];
View
25 src/nwmatcher.js
@@ -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
@@ -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('*'));
}
@@ -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).
@@ -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
@@ -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] + '"';
}
@@ -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 +'}';
}
@@ -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 = [ ];

0 comments on commit 91db52f

Please sign in to comment.