I want to use regexp namespace in my XPath expressions when searching elements in browser console, but get SyntaxError: The expression is not a legal expression. trying to do so.
I followed http://help.dottoro.com/ljspsvcs.php as a tutorial for creating a namespace resolver.
Here's my code:
function nsResolver (nsPrefix) {
if (nsPrefix == "regexp") {
return "http://exslt.org/regular-expressions";
}
return null;
}
document.evaluate('//a[regexp:test(#href, "qwerty-[\d]+$")]', document.documentElement, nsResolver, XPathResult.ANY_TYPE, null);
What am I doing wrong here?
The fact that someone has defined a set of extension functions in a particular namespace does not mean that every XSLT processor supports those functions. What is wrong here is that you are using an ancient XSLT processor that has not been upgraded in years (because the browser vendors lost interest in the XML user community).
Consider installing Saxon-JS, which provides XSLT 3.0 running in the browser, with built-in regular expression support according to W3C specifications. (Disclaimer: it's my company's product).
Related
Check this jsfiddle, and have a look at the console. $$ is not defined. Now, open a completely new window, and enter $$ into a console. It defines a function for getting a (jquery-like) array of all the dom elements which match the selector:
> $$
bound: function () {
return document.querySelectorAll.apply(document, arguments)
}
Is this being added by Dev tools? It is also present when using Firebug in Firefox. Is it used internally by the tools themselves?
Firstly, everything in ziesemer's answer is correct.
This is all about JavaScript history
There are a number of functions that are available in various browser's devtools consoles. Collectively, the methods are known as the Command Line API(off-line) (new link) and they all originate from Firebug. Nowadays we just have parity across browsers because Firebug did things (mostly) right.
But back when Firebug was being created (2006), the JavaScript library that was all the rage was Prototype.js. $ was grabbed by Prototype for some getElementById() syntactic sugar as that was certainly the quickest way to be grabbing elements and most common element acquisition technique at the time. It was such a timesaver, folks used the whole library just for the $ sugar.
In early 2006, jQuery then debuted and used $() for selecting any element based on css selector. As my old CSS Selector Engine Timeline post shows, Prototype then followed up four days later with their own, but as $ was already taken in their library they just went to $$(), which is now known as the bling-bling function.
So Firebug was leveraging Prototype's API as it was still ruling the roost in 2006. Now, in the days of jQuery and post-jQuery aliasing like window.$ = document.querySelectorAll.bind(document), we see it as quite backwards. Interestingly, when Opera revolutionized Dragonfly, their browser dev tools, they chose $ as their querySelectorAll alias, to better match present day practices, which IMO makes a bit more sense.
Oh you meant the code source..
Now, you asked about the "source" of the $$ in DevTools and I explained the history. Whoops! As to why it's available in your console... all of the Command Line API(off-line) (new link) methods are available only within the context of your console, just as convenience methods.
Chrome DevTools'/WebKit Inspector's cmd line API source
Firebug's cmd line API source
Opera Dragonfly's cmd line API source
copy() is one of my favorites; I cover it and others in this JavaScript Console for Power Users video.
Well, Firebug Lite defines this as:
this.$$=function(selector,doc){if(doc||!FBL.Firebug.chrome){return FBL.Firebug.Selector(selector,doc)
(See the source.)
The full version of Firebug defines this as
this.$$ = function(selector)
{
return FBL.getElementsBySelector(baseWindow.document, selector);
};
This is actually documented and yes, it is used internally as well.
So I assume that Google Chrome is doing something similar.
I'm working on a Node.js app with and I would like to use String.raw() which is part of the ES 6 standard.
However, when using it as in the documentation:
text = String.raw`Hi\n${2+3}!` + text.slice(2);
It returns SyntaxError: Unexpected token ILLEGAL for the character after String.raw.
I think that there is a problem because String.raw() is a new technology only available for Chrome and Firefox yet. However, can I use it in Node.js and how?
The grave character after raw denotes template strings, which is a feature in ES6 Harmony. You can invoke node with --harmony flag, but this feature is not yet implemented. This is the reason of the syntax error. Raw strings are unsupported too.
If you want experimenting with this feature in server side, check out io.js, which is a fork of node, but with many ES6 features implemented and enabled by default.
Is there a function to test if a snippet is valid JavaScript without actually evaluating it? That is, the equivalent of
function validate(code){
try { eval(code); }
catch(err) { return false; }
return true;
};
without side effects.
Yes, there is.
new Function(code);
throws a SyntaxError if code isn't valid Javascript. (ECMA-262, edition 5.1, ยง15.3.2.1 guarantees that it will throw an exception if code isn't parsable).
Notice: this snippet only checks syntax validity. Code can still throw exceptions because of undefined references, for example. It is a way harder to check it: you either should evaluate code (and get all its side effects) or parse code and emulate its execution (that is write a JS virtual machine in JS).
You could use esprima.
Esprima (esprima.org) is a high performance, standard-compliant ECMAScript parser written in ECMAScript (also popularly known as JavaScript).
Features
Full support for ECMAScript 5.1 (ECMA-262)
Sensible syntax tree format, compatible with Mozilla Parser AST
Heavily tested (> 550 unit tests with solid 100% statement coverage)
Optional tracking of syntax node location (index-based and line-column)
Experimental support for ES6/Harmony (module, class, destructuring, ...)
You can use the online syntax validator or install it as npm package and run it locally from the command line. There are two commands: esparse and esvalidate. esvalidate yields (given the example from the online syntax validator above):
$ esvalidate foo.js
foo.js:1: Illegal return statement
foo.js:7: Octal literals are not allowed in strict mode.
foo.js:10: Duplicate data property in object literal not allowed in strict mode
foo.js:10: Strict mode code may not include a with statement
For the sake of completeness esparse produces an AST.
Check this jsfiddle, and have a look at the console. $$ is not defined. Now, open a completely new window, and enter $$ into a console. It defines a function for getting a (jquery-like) array of all the dom elements which match the selector:
> $$
bound: function () {
return document.querySelectorAll.apply(document, arguments)
}
Is this being added by Dev tools? It is also present when using Firebug in Firefox. Is it used internally by the tools themselves?
Firstly, everything in ziesemer's answer is correct.
This is all about JavaScript history
There are a number of functions that are available in various browser's devtools consoles. Collectively, the methods are known as the Command Line API(off-line) (new link) and they all originate from Firebug. Nowadays we just have parity across browsers because Firebug did things (mostly) right.
But back when Firebug was being created (2006), the JavaScript library that was all the rage was Prototype.js. $ was grabbed by Prototype for some getElementById() syntactic sugar as that was certainly the quickest way to be grabbing elements and most common element acquisition technique at the time. It was such a timesaver, folks used the whole library just for the $ sugar.
In early 2006, jQuery then debuted and used $() for selecting any element based on css selector. As my old CSS Selector Engine Timeline post shows, Prototype then followed up four days later with their own, but as $ was already taken in their library they just went to $$(), which is now known as the bling-bling function.
So Firebug was leveraging Prototype's API as it was still ruling the roost in 2006. Now, in the days of jQuery and post-jQuery aliasing like window.$ = document.querySelectorAll.bind(document), we see it as quite backwards. Interestingly, when Opera revolutionized Dragonfly, their browser dev tools, they chose $ as their querySelectorAll alias, to better match present day practices, which IMO makes a bit more sense.
Oh you meant the code source..
Now, you asked about the "source" of the $$ in DevTools and I explained the history. Whoops! As to why it's available in your console... all of the Command Line API(off-line) (new link) methods are available only within the context of your console, just as convenience methods.
Chrome DevTools'/WebKit Inspector's cmd line API source
Firebug's cmd line API source
Opera Dragonfly's cmd line API source
copy() is one of my favorites; I cover it and others in this JavaScript Console for Power Users video.
Well, Firebug Lite defines this as:
this.$$=function(selector,doc){if(doc||!FBL.Firebug.chrome){return FBL.Firebug.Selector(selector,doc)
(See the source.)
The full version of Firebug defines this as
this.$$ = function(selector)
{
return FBL.getElementsBySelector(baseWindow.document, selector);
};
This is actually documented and yes, it is used internally as well.
So I assume that Google Chrome is doing something similar.
I'm trying to run an xpath-expression over an svg which is embedded in html. I just cannot figure out how to set up the parameters. I want find elements that have an arbitary attribute from a given namespace. I use the following xpath expression:
var xpathexp = "//*[#*[namespace-uri()='"+this.typo7namespace+"']]";
I tested this expression and it worked as expected.
this is the code to find the result set:
var result = this.svgdocument.contentDocument.evaluate( xpathexp, this.svgdocument.documentElement, null, XPathResult.ANY_TYPE, null );
Could anybody tell me, or post a link to a tutorial, how to deal with the namspaces, the namespace resolvers??
Greetings...
Here's the Mozilla tutorial on using XPath:
https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript
Here's one on writing custom namespace resolvers:
https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver
I found these interfaces to be rather clunky, though, so I wrote an abstraction layer that would take the xpath string and a context node, and would return a regular js array. It works inside the browser and embedded in Java under Mozilla Rhino:
https://svn.apache.org/repos/asf/commons/sandbox/gsoc/2010/scxml-js/trunk/src/javascript/scxml/cgf/util/xpath.js
All of the above should work in all browsers except for IE6-9.
IE6-8 does not support SVG natively, so this should be less important to your question. For completeness, though, here's a good article describing XPath support in earlier IE8, including support for resolving namespaces:
http://www.nczonline.net/blog/2009/04/04/xpath-in-javascript-part-3/
Apparently, IE9 also does not include support for XPath in the browser, which is more problematic, as it does support SVG natively. Probably the best approach here is to use ActiveX to work with MSXML APIs:
IE9 selectSingleNode missing from beta, how to overcome this in JavaScript?