Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Latest commit

 

History

History
165 lines (113 loc) · 4.53 KB

CAVEATS.md

File metadata and controls

165 lines (113 loc) · 4.53 KB

XPathJS - Caveats

While we strive to make XPathJS conform as closely as possible to XPath 1.0 and DOM Level 3 XPath specifications, we have to work within the technical limits of the browser.

We strongly recommend that anyone using XPathJS take a quick glance at the notes and issues below as it's good to keep them in mind while using the library.

Namespaces and id()

In order to find an element by id, that element must belong to a namespace.

For example:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<script src="../dist/xpathjs.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="myDiv"></div>
		
		<script type="text/javascript">
			XPathJS.bindDomLevel3XPath();
			
			if (document.evaluate("id('myDiv')", document, null, 3).booleanValue)
			{
				// we will find it
				alert("Found myDiv!");
			}
		</script>
	</body>
</html>

Now, if we remove xmlns="http://www.w3.org/1999/xhtml" in the example above, id('myDiv') will not find the div element. This is because the id attribute now belongs to the empty namespace.

By default, the following attributes are treated as an id in respective namespaces:

{
	"http://www.w3.org/XML/1998/namespace" => "id"
	"http://www.w3.org/1999/xhtml" => "id"
}

You can also add your own id definitions like in the following example:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:myns="http://www.pokret.org/example">
	<head>
		<script src="../dist/xpathjs.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="myDiv" myns:myid="mySpecialDiv"></div>
		
		<script type="text/javascript">
			XPathJS.bindDomLevel3XPath( // bind XPathJS
				
				XPathJS.createDomLevel3XPathBindings({ // create XPathJS functions
					
					'unique-ids': { // configure attributes to be used as IDs
						'http://www.pokret.org/example': 'myid'
					}
				})
			);
			
			if (document.evaluate("id('mySpecialDiv')", document, null, 3).booleanValue)
			{
				// we will find it
				alert("Found mySpecialDiv!");
			}
		</script>
	</body>
</html>

Suggested reading:

Getting Checkbox and Radio Button Values

Expressions such as option[@selected] or input[@checked] should not be used to check whether a checkbox or radio button are currently selected. Use the corresponding javascript properties on the option or input object to check if selected.

Example:

// get all options
var result = document.evaluate('option', document, null, 7, null);

for (var j = 0; j < result.snapshotLength; j++) {
	var item = result.snapshotItem(j);

	// check option property
	if (item.selected)
	{
		alert('Selected:' + item.value);
		break;
	}
}

This also applies to @value and @disabled. All of these should be treated as node properties, not attributes.

See how jquery deals with element properties vs attributes using the prop() instead of attr() function.

Only Strict Mode Supported

Please ensure that a doctype is declared at the top of the document when using XPathJS as we only support strict mode.

Only Specified Attribute Supported

Specified attributes are those that are explicitly set in HTML or via Javascript. IE8 and below tend to set default attribute values on nodes, so that each node may have 80 or more attributes, all of which have the value null.

XPathJS only works with specified attributes in order to improve performance and avoid including meaningless attribute nodes in the result.

Order of Attributes

The order of attributes can vary from browser to browser.

Example:

<div class="asdf" style="color:blue;"></div>

Call XPathJS:

document.evaluate('//div/attribute::*', document, null, 7);

May return class then style attribute in Chrome, while it returns style, then class in Firefox.

The order of attributes usually doesn't matter, but this is something to keep in mind.

No DTD Support

Any DTD declaration beyond the DOCTYPE will be ignored.

Example:

<!DOCTYPE html [
	<!ATTLIST img myid ID #REQUIRED>
]>

The ATTLIST declaration will have no effect on XPathJS.