/[suikacvs]/www/2005/xpointer-js/xpointer.js
Suika

Contents of /www/2005/xpointer-js/xpointer.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download) (as text)
Sun Mar 13 10:24:13 2005 UTC (21 years, 4 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
File MIME type: application/javascript
New

1 wakaba 1.1 /* xpointer.js - XPointer implementation written in JavaScript */
2    
3    
4     /* Regular Expressions */
5     /* BUG: These expressions are less strict than the formal definition of XPointer */
6     var ncname = '[A-Za-z_\u00C0-\u02FF\u0370-\u203E\u2041-\uFFFD]' +
7     '[A-Za-z_\u00C0-\uFFFD.0-9\u00B7-]*';
8     var isNCName = new RegExp ('^' + ncname + '$');
9     var pointerPart = new RegExp ('^(' + ncname + ')(?:' + ':(' + ncname + '))?' +
10     '\\(' + '(' + '[^()^]*(?:[^()^]|\\^[()^]' +
11     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
12     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
13     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
14     //
15     ')*\\)' +
16     ')*\\)' +
17     ')*\\)' +
18     ')*' + ')' + '\\)' +
19     '[\x09\x0A\x0D\x20]*');
20    
21     /** Implemenetations of XPointer schemes */
22     function XPointerSchemeProcessor (evaluateFunction) {
23     this.evaluate = evaluateFunction;
24     }
25    
26     /** XPointer Evaluation Engine */
27     function XPointerEvaluator () {
28     return this;
29     }
30    
31     /** Evaluates an XPointer pointer in the context of a document.
32    
33     @param aDocument A document in which the pointer is evaluated.
34     @param aExpression An XPointer pointer.
35     @raise XPointerShorthandNoMatchError
36     A shorthand pointer is specified but it does not
37     identify any element.
38     @raise XPointerSchemeBasedNoMatchError
39     A scheme-based pointer is specified but it does not
40     identify any subresources.
41     @raise XPointerSyntaxError
42     An illegal pointer is specified.
43     @return XPointerResult that contains subresources identified by the poitner.
44     */
45     XPointerEvaluator.prototype.evaluate = function (aDocument, aExpression) {
46     var ptr = aExpression;
47     var aContext = new XPointerSchemeContext ();
48     if (isNCName.test (aExpression)) {
49     var result = this.getXPointerSchemeProcessor (null, null)
50     .evaluate (aDocument, aContext, ptr);
51     if (!result.hasSubresources) {
52     throw new XPointerShorthandNoMatchError (aExpression);
53     }
54     return result;