php-technolog / wicked-good-xpath

Automatically exported from code.google.com/p/wicked-good-xpath
0 stars 0 forks source link

While evaluating xpath having element names with .(period) character fail with an error *Bad Token .* #19

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The library throws error when the element names in xpath has period (.) in it.

To reproduce the problem
1. Create an XML Document with period in element names. Using jQuery you can 
create the document using the code
     var xml = "<abc><a.d>12</a.d></abc>";
     var xmlDoc = $.parseXML(xml);
     wgxpath.install();
     var result = document.evaluate("/abc/a.d",xmlDoc, null, XPathResult.ANY_TYPE, null);

2. The same script runs on Chrome which provides native support for 
document.evaluate but not on IE11 using this library. It fails with an error

  SCRIPT5022: Bad token: .
  File: wgxpath.install.js, Line: 47, Column: 609

3. I have created a fiddle http://jsfiddle.net/r0gruz30/ which reproduces the 
problem. Open the fiddle in chrome and the output will be *Result is 12* but in 
IE the output is *Result is* and there is an error in the console

  SCRIPT5022: Bad token: .
  File: wgxpath.install.js, Line: 47, Column: 609

I am using the latest wgxpath library and seeing the issue on IE11 windows 7.

I debugged the code and found that the reason for the issue is in lexer.js. The 
regular expression for _TOKEN does not takes care of periods in element names. 
So if I change the regex 

wgxpath.Lexer.TOKEN_ = new RegExp(
    '\\$?(?:(?![0-9-])[\\w-]+:)?(?![0-9-])[\\w-]+' +
    ...
    .....,
   'g')

to 

wgxpath.Lexer.TOKEN_ = new RegExp(
    '\\$?(?:(?![0-9-\\.])[\\w\\.-]+:)?(?![0-9-\\.])[\\w-\\.]+' +
    ...
    .....,
   'g')

it works. 

Original issue reported on code.google.com by varundua...@gmail.com on 24 Feb 2015 at 10:57