Code Monkey home page Code Monkey logo

nwmatcher's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nwmatcher's Issues

traversal.js

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';

Error: The string "div[a=b] .wrap div", is not a valid CSS selector

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.

IE errors in http://javascript.nwbox.com/NWMatcher/release/benchmarks/

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

Firefox problem with new version

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

`option[value='']` matches too much if BUGGY_GET_ATTRIBUTE is set

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?

HTML5 elements fail to match IE<9 when they are not the selector subject

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.

DocumentFragment only matches childNodes

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).

NW.Dom.match not working in IE8 and IE9(in browser mode IE8 and below)

NW.Dom.select is working, NW.Dom.match is not
Test case:

  • all logs should be true
  • in IE8, only the first one is true
<!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>

:nth-child/:first-child/:last-child/:only-child does not reject broken selectors

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 field problem

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.

+1 CDN

//cdn.jsdelivr.net/nwmatcher/1.2.5/nwmatcher.min.js

Multiple selectors separated by comma not working

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"

How to use NWMatcher?

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.

`div\tp` does not match

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"] );

Suspicious logic in RE_SIMPLE_SELECTOR

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.

IE DOM mapping

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.

escape

var d = document.createElement('div');
d.setAttribute('data-x', '"');
var sel = '[data-x="""]';
alert(NW.Dom.match(d, sel));

Attributes containing backslashes

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&#39;]'/>" +
"<input type='hidden' id='attrbad_quote' data-attr='&#39;'/>" +
"<input type='hidden' id='attrbad_backslash' data-attr='&#92;'/>" +
"<input type='hidden' id='attrbad_backslash_quote' data-attr='&#92;&#39;'/>" +
"<input type='hidden' id='attrbad_backslash_backslash' data-attr='&#92;&#92;'/>" +
"<input type='hidden' id='attrbad_unicode' data-attr='&#x4e00;'/>"
).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"] );

Sizzle test suite failures: id

deepEqual( Sizzle( "> span", Sizzle("#fiddle\\\\Foo")[0] ), q([ "fiddleSpan" ]), "Escaped ID as context" );
t( "ID Selector contains backslash", "#backslash\\\\foo", ["backslash\\foo"] );

What is the optimal way to filter out specific CCS rules?

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)

Opera 9.25 has an issue inside byId()

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) {

Performance issues in 1.2.4

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.

problem with selector split in parts in NW.Dom.match (not in NW.Dom.select)

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

including nwmatcher script breaks IE8 favicon

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?

  1. create a clean html template
  2. include a ie8 favorite icon with link rel="shortcut icon" in head
  3. favorite icon is visible in IE8
  4. add nwmatcher (1.2.5) to head
  5. favorite icon is not visible in IE8

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>

commonjs

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?

IE8 hasAttribute bug

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]];
  }

No "parseInt" on global object

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

Use with jsdom

I would like to use nwmatcher as jsdom's selector engine. To do so, I need two things:

  • A commitment that this project will be maintained. There are currently a few outstanding issues that I'm surprised having been merged in. I'm afraid that if I come across other bugs you won't be responsive to fixing them.
  • An interface for feeding you a global object. Currently it uses this which means you can't pick up a jsdom window's document, DOMError, etc. properties.

speed test - div[class!=made_up]

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)

USE_HTML5 doesn't work when QSA is buggy.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.