dperini / nwmatcher Goto Github PK
View Code? Open in Web Editor NEWFast Javascript CSS Selector Engine and Matcher
Home Page: http://javascript.nwbox.com/NWMatcher/
License: MIT License
Fast Javascript CSS Selector Engine and Matcher
Home Page: http://javascript.nwbox.com/NWMatcher/
License: MIT License
I think
nextProperty = root[next] !== UNDEF ? nextEl : 'nextSibling',
prevProperty = root[prev] !== UNDEF ? prevEl : 'previousSibling';
should be
nextProperty = root[nextEl] !== UNDEF ? nextEl : 'nextSibling',
prevProperty = root[prevEl] !== UNDEF ? prevEl : 'previousSibling';
Here is my test case:
div[a=b] .wrap div { /* [0, 2, 2] */
display: block;
}
I get this error:
Error: The string "div[a=b] .wrap div", is not a valid CSS selector
at emit (/nwmatcher/src/nwmatcher.js:830:37)
at Object.select (/nwmatcher/src/nwmatcher.js:1397:11)
w3c validator says this should be fine.
Please, update NWMatcher to 1.2.4 at your page http://javascript.nwbox.com/NWMatcher/release/benchmarks/ to test for compatibility.
Current tests for NWMatcher 1.2.3 shows errors in IE6-IE7 (0 elements found):
selectors jquery-1.5.1 prototype-1.7 mootools-1.3.1 nwmatcher-1.2.3
a[href][lang][class] 25 ms | 1 found 23 ms | 1 found 24 ms | 1 found 12 ms | 0 found
div[class] 6 ms | 51 found 12 ms | 51 found 11 ms | 51 found 5 ms | 0 found
t( "Isolated position", ":last", ["fx-tests"] );
Hello
I just tried the new version, and now I have another problem:
In Firefox 3.6.6 Win Vista 32 with Firebug 1.5x.4, the following test is blocking everything (script not responding)
/^(?:*|[.#]?(?:-?[a-zA-Z]{1,}[-\w]{0,}|[^\x00-\xa0]{1,}|.)+)$/.test('#pgtypetransport_pg_form *disabled]')
it's RE_SIMPLE_SELECTOR.test(selector) al line 1407
The same works ok in Safari/Chrome
BUT the same is also blocking everything in The Regex Coach
This bug is revealed by the following jsdom test case:
"use strict";
var assert = require("assert");
var jsdom = require("jsdom").jsdom;
var document = jsdom('<html><head></head><body><select><option value="">Nothing</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select><img src="3" /></body></html>');
var window = document.createWindow();
assert(document.querySelectorAll("select option[value='']").length === 1);
instead the length is 4, i.e. all four <option>
elements match.
This occurs because, in jsdom, NATIVE_GET_ATTRIBUTE
is false
(since getAttribute
doesn't toString
as [native code]
), which in turn means BUGGY_GET_ATTRIBUTE
is true
, which means the implementation uses your ATTR_DEFAULT
map.
Since "value"
is in ATTR_DEFAULT
, it goes to look at the <option>
element's defaultValue
property.
But, as per the HTML5 spec, <option>
elements do not have a defaultValue
property. Only HTMLInputElement
, HTMLOutputElement
, and HTMLTextAreaElement
do. So it returns node["defaultValue"] || ''
, i.e. returns the empty string, so the selector matches!
I could try putting together a pull request, but I thought you'd probably have a better idea on how to fix this, given how you intend ATTR_DEFAULT
to work. I think it needs a list of tag names that each default applies to?
There is a sibling-selector bug in FF 3.5.1 with qsa.
Shared dropbox ./sibling-selector.html
If IE doesn't support an element like some HTML 5 elements or IE6 and the ABBR element it will include their tag in the gEBTN('*') results. We should filter them out.
http://github.com/subtleGradient/slick/blob/htmlbugs/slick.js#L29-32
I can't reopen #68 so I am making a new issue.
IE<9 fails to match selectors that contain HTML5 tags (actually, any non-standard tags) anywhere other than as the selector subject node. For example:
"nav:first-of-type" < works (nav is the subject)
"nav li:first-of-type" < will not match (li is the subject)
"body nav li:first-of-type" < will not match (li is the subject)
I've done some debugging and it seems the issue is related to case sensitivity. When using an HTML5 element it's nodeName
property is lower case but is tested against an upper case string. Here's how I diagnosed it:
On line 524 The TO_UPPER_CASE
variable is set to .toUpperCase()
if XML_DOCUMENT
evaluates to true.
When compiling the selector on line 938 TO_UPPER_CASE
is applied to the compiled source if the XML_DOCUMENT
evaluates to false. In this case TO_UPPER_CASE
will alway be an empty string:
// *** Type selector
// Foo Tag (case insensitive)
else if ((match = selector.match(Patterns.tagName))) {
// both tagName and nodeName properties may be upper/lower case
// depending on their creation NAMESPACE in createElementNS()
source = 'if(e.nodeName' + (XML_DOCUMENT ?
'=="' + match[1] + '"' : TO_UPPER_CASE +
'=="' + match[1].toUpperCase() + '"') +
'){' + source + '}';
}
Should the TO_UPPER_CASE
strings be switched to the following?
TO_UPPER_CASE = XML_DOCUMENT ? '' : '.toUpperCase()' ;
Although I noticed this in older versions of IE it may not be IE specific.
I was trying to get querySelector in jsdom to work on DocumentFragments, but I think I have come across an issue in this library.
To rule out anything in jsdom I have tried using this library in chrome and firefox 27
NW.Dom.configure( { USE_QSAPI: false, VERBOSITY: true, CACHING: false } );
var frag = document.createDocumentFragment();
var div = document.createElement('div');
frag.appendChild(div);
var p = document.createElement('p');
div.appendChild(p);
console.log(frag.querySelector('div')); // Firebug / chrome dev tools shows <div>
console.log(NW.Dom.first('div', frag)); // <div>
console.log(frag.querySelector('p')); // <p>
console.log(NW.Dom.first('p', frag)); // null
I think the issue is in nwmatches-noqsa.js:725
If I replace that line with this:
elements = [];
var node = from.firstChild;
while(node) {
if (node.nodeType === 1) {
elements.push(node);
}
if (node.firstChild) {
node = node.firstChild;
} else {
while (!node.nextSibling) {
node = node.parentNode;
if (node === from) {
break;
}
}
node = node.nextSibling;
}
}
it seems to work properly. That code should give identical results as getElementsByTagName('*')
I'll send a pull request in a moment. I will try to match the code style used (2 spaces per indent is hurting my eyes haha).
t( "Hidden inputs should be treated as enabled. See QSA test.", "#hidden1:enabled", ["hidden1"] );
NW.Dom.select is working, NW.Dom.match is not
Test case:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src='nwmatcher.js' type='text/javascript' charset='utf-8'></script>
<script src='nwevents.js' type='text/javascript' charset='utf-8'></script>
<script type="text/javascript">
NW.Event.appendListener(window, 'load', function() {
var e1 = NW.Dom.byId('div1'),
e2 = NW.Dom.byId('span1'),
e3 = NW.Dom.byId('span2');
console.log("NW.Dom.match(e1,'div') => " +NW.Dom.match(e1,'div'));
console.log("NW.Dom.match(e1,'div.test1') => " +NW.Dom.match(e1,'div.test1'));
console.log("NW.Dom.match(e1,'div.test1.test2') => " +NW.Dom.match(e1,'div.test1.test2'));
console.log("NW.Dom.match(e1,'.test1.test2') => " +NW.Dom.match(e1,'.test1.test2'));
console.log("NW.Dom.match(e2,'div > span') => " +NW.Dom.match(e2,'div > span'));
console.log("NW.Dom.match(e2,'div.test1 > span') => " +NW.Dom.match(e2,'div.test1 > span'));
console.log("NW.Dom.match(e2,'div.test1 > span:not(.test5)') => "+NW.Dom.match(e2,'div.test1 > span:not(.test5)'));
}, false);
</script>
<title>NWMatcher Tests</title>
</head>
<body>
<div id='div1' class='test1 test2'>
<span id='span1' class='test3 test4'>123</span>
<span id='span2' class='test3 test4'>123</span>
</div>
</body>
</html>
The following tests fail if inserted into test/jquery/unit/selector.js after the rest of the broken
calls:
broken( "Nth-child", ":nth-child(2n+-0)", [] );
broken( "Nth-child", ":nth-child(- 1n)", [] );
broken( "Nth-child", ":nth-child(-1 n)", [] );
broken( "First-child", ":first-child(n)", [] );
broken( "Last-child", ":last-child(n)", [] );
broken( "Only-child", ":only-child(n)", [] );
In general updating the jQuery test suite to the latest version would be helpful.
input[checked]
fails to match while input:checked
works.
http://dl.getdropbox.com/u/513327/bug/nwmatcher_checked.html
Hello,
If I have an input with name='className', when I write in another field the first field is replaced by
'/[\x20\t\n\r\f]+/g'.
The problem is I cannot create a test case, as my application uses prototype.js, nwmatcher and nwevents.
What I could find out is that it happens on executing the source created on line 734(reWhiteSpace):
it goes up e = e.parentNode, then n=e.className and n.replace, but when it gets to the form element, n.className is the input name='className' instead of the className of the form element, and it's replaced.
//cdn.jsdelivr.net/nwmatcher/1.2.5/nwmatcher.min.js
Once that's done, jsdom can depend on it!
I was double checking feature tests in RE_BUGGY_QSAP
and couldn't find a browser that failed the :link
test. Can you provide a basic failing test. Maybe the comment has the problem browsers mislabeled.
This page https://github.com/dperini/nwmatcher/wiki/CSS-supported-selectors is very hard to read as there are supposed to be 3 background colors but I only see 2, and the gray one is barely visible.
Hello,
It seems that after the latest changes, multiple selectors separeted by comma are not working any more.
NW.Dom.match($1,'input,button')
SYNTAX_ERR: (Selectors) Invalid syntax in selector ",button"
Would you please help me "translate" the following javascript function using NWMatcher:
function toggle_visibility(item) {
var sr = NW.Dom.byId('vue_simple-'+item);
var dr = NW.Dom.byId('vue_detaillee-'+item);
if (sr.style.display != 'none') {
sr.setAttribute('style','display: none');
dr.setAttribute('style','display: block !important');
}
else {
dr.setAttribute('style','display: none');
sr.setAttribute('style','display: block');
}
}
Above, I already replaced document.getElementById with NW.Dom.byId
Is this correct? What is meant by 'context' here: NW.Dom.byId(element, context);
Thkx for your help.
If you turn off USE_QSAPI
and add this line to the jquery tests, it fails:
t( "Parent Element", "div\tp", ["firstp","ap","sndp","en","sap","first"] );
https://github.com/dperini/nwmatcher/blob/master/src/nwmatcher.js#L383
// matches simple id, tag & class selectors
RE_SIMPLE_SELECTOR = new global.RegExp(
!(BUGGY_GEBTN && BUGGY_GEBCN) ? !OPERA ?
'^(?:\\*|[.#]?-?[_a-zA-Z]{1}' + encoding + '*)$' :
'^(?:\\*|#-?[_a-zA-Z]{1}' + encoding + '*)$' :
'^#?-?[_a-zA-Z]{1}' + encoding + '*$'),
RE_SIMPLE_SELECTOR
uses pattern '^#?-?[_a-zA-Z]{1}' + encoding + '*$'
(AFAIK for id or tag selector) only if expression BUGGY_GEBTN && BUGGY_GEBCN
is true (both gEBTN and gEBCN are buggy).
In other words, RE_SIMPLE_SELECTOR
could use pattern '^(?:\\*|[.#]?-?[_a-zA-Z]{1}' + encoding + '*)$'
(simple id|class|tag selector) even if one of gEBTN/gEBCN is buggy (i.e. could use simple class selector if getElementsByClassName()
does not exist at all but gEBTN works fine).
Is it OK or am I completely wrong?
If existence and correctness of the native methods does not matter for simple selector, then this logic can be omitted at all:
RE_SIMPLE_SELECTOR = new global.RegExp(
!OPERA ?
'^(?:\\*|[.#]?-?[_a-zA-Z]{1}' + encoding + '*)$' :
'^(?:\\*|#-?[_a-zA-Z]{1}' + encoding + '*)$'),
Otherwise, if correct native implementation is important for RE_SIMPLE_SELECTOR
, this code may be rewritten with either !(BUGGY_GEBTN || BUGGY_GEBCN)
or (!BUGGY_GEBTN && !BUGGY_GEBCN)
:
RE_SIMPLE_SELECTOR = new global.RegExp(
(!BUGGY_GEBTN && !BUGGY_GEBCN) ? !OPERA ?
'^(?:\\*|[.#]?-?[_a-zA-Z]{1}' + encoding + '*)$' :
'^(?:\\*|#-?[_a-zA-Z]{1}' + encoding + '*)$' :
'^#?-?[_a-zA-Z]{1}' + encoding + '*$'),
P.S. Anyways, this is a good illustration to the rule: code with negations in ternary operator or in if()
is hard to understand.
Local variable i
in function select
(declared at line 1352) is never used.
I noticed you don't have a compiled string fork of className when it is case-insensitive. We should add it.
Emulated hasAttribute
method has attribute mapping for IE
// older IE engines requires DOM mapping
// see NetFront/Playstation as an example
attribute = attribute in ATTR_MAPPING ?
ATTR_MAPPING[attribute] : attribute;
while getAttribute
has not. I think code for both methods should look similar.
http://github.com/subtleGradient/nwmatcher/blob/master/test/css3-compat/css3-compat.html#L118
This test is checking to make sure that the CSS3 Selector implementation fails if it supports complex selectors.
Supporting complex selectors is an additional feature not a bug.
Including extra features would not cause a CSS3 Selectors implementation to fail the CSS3 Selectors Recommendation.
var d = document.createElement('div');
d.setAttribute('data-x', '"');
var sel = '[data-x="""]';
alert(NW.Dom.match(d, sel));
Here are the failing sizzle tests, when used with nwmatcher instead of Sizzle
attrbad = jQuery(
"<input type='hidden' id='attrbad_dot' value='2' name='foo.baz'/>" +
"<input type='hidden' id='attrbad_brackets' value='2' name='foo[baz]'/>" +
"<input type='hidden' id='attrbad_injection' data-attr='foo_baz']'/>" +
"<input type='hidden' id='attrbad_quote' data-attr='''/>" +
"<input type='hidden' id='attrbad_backslash' data-attr='\'/>" +
"<input type='hidden' id='attrbad_backslash_quote' data-attr='\''/>" +
"<input type='hidden' id='attrbad_backslash_backslash' data-attr='\\'/>" +
"<input type='hidden' id='attrbad_unicode' data-attr='一'/>"
).appendTo("#qunit-fixture");
t( "Quoted backslash", "input[data-attr='\\\\']", ["attrbad_backslash"] );
t( "Quoted backslash quote", "input[data-attr='\\\\\\'']", ["attrbad_backslash_quote"] );
t( "Quoted backslash backslash", "input[data-attr='\\\\\\\\']", ["attrbad_backslash_backslash"] );
t( "Quoted backslash backslash (numeric escape)", "input[data-attr='\\5C\\\\']", ["attrbad_backslash_backslash"] );
t( "Quoted backslash backslash (numeric escape with trailing space)", "input[data-attr='\\5C \\\\']", ["attrbad_backslash_backslash"] );
t( "Quoted backslash backslash (numeric escape with trailing tab)", "input[data-attr='\\5C\t\\\\']", ["attrbad_backslash_backslash"] );
t( "Long numeric escape (BMP)", "input[data-attr='\\04e00']", ["attrbad_unicode"] );
deepEqual( Sizzle( "> span", Sizzle("#fiddle\\\\Foo")[0] ), q([ "fiddleSpan" ]), "Escaped ID as context" );
t( "ID Selector contains backslash", "#backslash\\\\foo", ["backslash\\foo"] );
e.g. old IEs don't interpret rgba() colors
Is there a way to filter out CSS rules containing rgba in attribute?
like border-right: 1px solid rgba(255,255,255, 0.50);
and print those into a file by node.js? (then I manually edit them)
It seems Opera 9.25 doesn't give elements an id attribute node by default you will need to check the existence of one via if ((node = element.getAttributeNode('id')) && node.value == id) {
Well, I was doing some testing with NWMatcher using a slightly modified version of your Matchspeed test (that thing is pretty nifty) and 1.2.4 seems to have some serious performance issues.
Here's 1.2.3:
http://bit.ly/qxRgv2
And here's 1.2.4:
http://bit.ly/qkMbtE
This is on Chrome 14.0.822.0 Canary. It seems to suffer in other browsers also.
I also ran it on a modified Slickspeed that tests match() and allows for variable iterations. 1.2.3 ran in about 0.27ms and 1.2.4 ran in 0.6ms. Each of 22 tests was run 5000 times, again under Chrome.
Both versions are minifed with the Closure Compiler.
Hello
NW.Dom.select('h1 img.title,h2 img.title') works
NW.Dom.select('h1 img.title, h2 img.title') works (space after comma)
NW.Dom.match(element,'h1 img.title, h2 img.title') works (space after comma)
NW.Dom.match(element,'h1 img.title,h2 img.title') does not work (no space after comma)
I think it's related to the fact that h2 has digits in tag
Thank you,
PDan
seems, native in Chrome is buggy
Via http://javascript.nwbox.com/NWMatcher/ I also posted this bug at https://code.google.com/p/nwevents/issues/detail?id=19
What steps will reproduce the problem?
What version of the product are you using? On what operating system?
nwmatcher 1.2.5; IE8 at Win7
Here, the favicon works:
<!DOCTYPE html>
<html>
<head>
<title>:)</title>
<link rel="shortcut icon" type="image/x-icon" href="http://www.microsoft.com/favicon.ico?v2">
</head>
<body>
<h1>:)</h1>
</body>
</html>
Here, it doesn't:
<!DOCTYPE html>
<html>
<head>
<title>:(</title>
<link rel="shortcut icon" type="image/x-icon" href="http://www.microsoft.com/favicon.ico?v2">
<script src="http://s3.amazonaws.com/nwapi/nwmatcher/nwmatcher-1.2.5-min.js"></script>
</head>
<body>
<h1>:(</h1>
</body>
</html>
NWMatcher can create a Feature test method that caches FT per context in a data object. This can be resolved when NWMatcher resolves how it wants to identify different contexts in a data object.
NWMatcher.Dom.Data[id].BUGGY_QSAPI
Hey!
Was wonder if you'd be open to including nwmatcher and/or nwevents on npm?
It's pretty easy to add a package.json file and module.exports functionality (i could even do it for you and submit a pull request if you'd like).
NWmatcher could then be used on the server with jsdom and other projects -- and projects like ender could pull them down for client development.
thoughts?
Sometimes IE8 (maybe other versions too) returns true from hasAttribute() in fragment
if (ATTR_DEFAULT[attribute] in node) {
return !!node[ATTR_DEFAULT[attribute]];
}
Although ATTR_DEFAULT[attribute]
is undefined
, ATTR_DEFAULT[attribute] in node
may return true (i.e. undefined in node === true
). Screenshot is at https://picasaweb.google.com/lh/photo/F3tUHxGRrWyYfWsql5XWGA?feat=directlink Maybe you should previously check if ATTR_DEFAULT[attribute]
is not empty
if (ATTR_DEFAULT[attribute] && ATTR_DEFAULT[attribute] in node) {
return !!node[ATTR_DEFAULT[attribute]];
}
I cannot use selectors such as "div.foo:nth-child(3)" and "div.foo:nth-of-type(3)". There is no method "parseInt" on the global object. This issue surfaced for me in zombie 2.0.0-alpha31, which uses jsdom 0.8.11, which uses nwmatcher "~1.3.1". This could be a bug in the way that jsdom invokes (or invoked) nwmatcher, but there should still be guards (or possibly a fallback) in nwmatcher.
The offending line of code:
https://github.com/dperini/nwmatcher/blob/master/src/nwmatcher-noqsa.js#L522
This commit appears to be relevant:
3ba3c84
NWMatcher should pick a unitesting framework, qunit, jsspec, or something and make its own unit tests.
Current version is 1.2.5beta, but version in package.json is still 1.2.4beta1.
I would like to use nwmatcher as jsdom's selector engine. To do so, I need two things:
global
object. Currently it uses this
which means you can't pick up a jsdom window's document
, DOMError
, etc. properties.in the speed test, div[class!=made_up] selects 55 elements (all divs with class set)
in prototype and jdalton branch, it selects 59 elements (all divs)
Some browsers like Chrome.latest don't seem to recognize option:checked yet, so NWMatcher needs a FT or a flag to fallback to manual DOM iteration when using that flag.
It seems the :hover pseudo fallback does not actually do anything. It's missing the necessary accompaniment of a mouseover event.
Test case:
http://jsfiddle.net/timmywil/G3gSs/
in the shared dropbox NWMatcher fails a few tests on disconnected nodes.
speed/index.php?css=yahoo&special=loose fails "#page #e2econtent"
speed/index.php?special=loose failse "#title"
Good afternoon!
Please add a tag for 1.2.5 as it will be on cdnjs.com
cdnjs/cdnjs#943 (comment)
I think this is it:
4909033
Thank you!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.