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

Commit

Permalink
multinode-770213: Add NodeListType
Browse files Browse the repository at this point in the history
  • Loading branch information
joewalker committed Aug 8, 2012
1 parent da2c3b3 commit ad3182a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/gcli/types/node.js
Expand Up @@ -23,6 +23,7 @@ var types = require('gcli/types');
var Type = require('gcli/types').Type;
var Status = require('gcli/types').Status;
var Conversion = require('gcli/types').Conversion;
var BlankArgument = require('gcli/argument').BlankArgument;


/**
Expand All @@ -45,11 +46,20 @@ if (typeof document !== 'undefined') {
doc = document;
}

/**
* For testing only.
* The fake empty NodeList used when there are no matches, we replace this with
* something that looks better as soon as we have a document, so not only
* should you not use this, but you shouldn't cache it either.
*/
exports._empty = [];

/**
* Setter for the document that contains the nodes we're matching
*/
exports.setDocument = function(document) {
doc = document;
exports._empty = doc.querySelectorAll('x>:root');
};

/**
Expand Down Expand Up @@ -120,4 +130,64 @@ NodeType.prototype.parse = function(arg) {
NodeType.prototype.name = 'node';



/**
* A CSS expression that refers to a node list.
*
* The 'allowEmpty' option ensures that we do not complain if the entered CSS
* selector is valid, but does not match any nodes. There is some overlap
* between this option and 'defaultValue'. What the user wants, in most cases,
* would be to use 'defaultText' (i.e. what is typed rather than the value that
* it represents). However this isn't a concept that exists yet and should
* probably be a part of GCLI if/when it does.
* All NodeListTypes have an automatic defaultValue of an empty NodeList so
* they can easily be used in named parameters.
*/
function NodeListType(typeSpec) {
if ('allowEmpty' in typeSpec && typeof typeSpec.allowEmpty !== 'boolean') {
throw new Error('Legal values for allowEmpty are [true|false]');
}

this.allowEmpty = typeSpec.allowEmpty;
}

NodeListType.prototype = Object.create(Type.prototype);

NodeListType.prototype.getBlank = function() {
return new Conversion(exports._empty, new BlankArgument(), Status.VALID);
};

NodeListType.prototype.stringify = function(value) {
if (value == null) {
return '';
}
return value.__gcliQuery || 'Error';
};

NodeListType.prototype.parse = function(arg) {
if (arg.text === '') {
return new Conversion(undefined, arg, Status.INCOMPLETE);
}

var nodes;
try {
nodes = doc.querySelectorAll(arg.text);
}
catch (ex) {
return new Conversion(undefined, arg, Status.ERROR,
l10n.lookup('nodeParseSyntax'));
}

if (nodes.length === 0 && !this.allowEmpty) {
return new Conversion(undefined, arg, Status.INCOMPLETE,
l10n.lookup('nodeParseNone'));
}

host.flashNodes(nodes, false);
return new Conversion(nodes, arg, Status.VALID, '');
};

NodeListType.prototype.name = 'nodelist';


});

1 comment on commit ad3182a

@campd
Copy link

@campd campd commented on ad3182a Aug 25, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.