1 /** 2 * @fileoverview A class implementing the xpath 1.0 subset of the 3 * KindTest construct. 4 */ 5 6 goog.provide('xrx.xpath.KindTest'); 7 8 goog.require('xrx.node'); 9 goog.require('xrx.xpath.NodeTest'); 10 11 12 13 /** 14 * Constructs a subset of KindTest based on the XPath grammar: 15 * http://www.w3.org/TR/xpath20/#prod-xpath-KindTest 16 * 17 * @param {string} typeName Type name to be tested. 18 * @param {xrx.xpath.Literal=} opt_literal Optional literal for 19 * processing-instruction nodes. 20 * @constructor 21 * @implements {xrx.xpath.NodeTest} 22 */ 23 xrx.xpath.KindTest = function(typeName, opt_literal) { 24 25 /** 26 * @type {string} 27 * @private 28 */ 29 this.typeName_ = typeName; 30 31 /** 32 * @type {xrx.xpath.Literal} 33 * @private 34 */ 35 this.literal_ = goog.isDef(opt_literal) ? opt_literal : null; 36 37 /** 38 * @type {?xrx.node} 39 * @private 40 */ 41 this.type_ = null; 42 switch (typeName) { 43 case 'comment': 44 this.type_ = xrx.node.COMMENT; 45 break; 46 case 'text': 47 this.type_ = xrx.node.TEXT; 48 break; 49 case 'processing-instruction': 50 this.type_ = xrx.node.PI; 51 break; 52 case 'node': 53 break; 54 default: 55 throw Error('Unexpected argument'); 56 } 57 }; 58 59 60 /** 61 * Checks if a type name is a valid KindTest parameter. 62 * 63 * @param {string} typeName The type name to be checked. 64 * @return {boolean} Whether the type name is legal. 65 */ 66 xrx.xpath.KindTest.isValidType = function(typeName) { 67 return typeName == 'comment' || typeName == 'text' || 68 typeName == 'processing-instruction' || typeName == 'node'; 69 }; 70 71 72 /** 73 * @override 74 */ 75 xrx.xpath.KindTest.prototype.matches = function(node) { 76 return goog.isNull(this.type_) || this.type_ == node.type(); 77 }; 78 79 80 /** 81 * Returns the type of the node. 82 * 83 * @return {?number} The type of the node, or null if any type. 84 */ 85 xrx.xpath.KindTest.prototype.getType = function() { 86 return this.type_; 87 }; 88 89 90 /** 91 * @override 92 */ 93 xrx.xpath.KindTest.prototype.getName = function() { 94 return this.typeName_; 95 }; 96 97 98 /** 99 * @override 100 */ 101 xrx.xpath.KindTest.prototype.toString = function() { 102 var text = 'Kind Test: ' + this.typeName_; 103 if (!goog.isNull(this.literal_)) { 104 text += xrx.xpath.Expr.indent(this.literal_); 105 } 106 return text; 107 }; 108