validation for find and findOne methods

This commit is contained in:
lavrton 2015-05-25 13:56:19 +07:00
parent 2640f7d36d
commit 74dab77ad4
4 changed files with 57 additions and 3 deletions

View File

@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Correct `clone()` for custom nodes - Correct `clone()` for custom nodes
- `FastLayer` now can have transforms - `FastLayer` now can have transforms
- `stage.toDataURL()` method now works in synchronous way. So `callback` argument is not required. - `stage.toDataURL()` method now works in synchronous way. So `callback` argument is not required.
- `container.find(selector)` method now have validation step. So if you forgot to add `#` or `.` you will see a warning message in the console.
### Added ### Added
- new `Konva.Image.fromURL` method - new `Konva.Image.fromURL` method

View File

@ -1,5 +1,13 @@
(function() { (function() {
'use strict'; 'use strict';
function isValidSelector(selector) {
if (typeof selector !== 'string') {
return false;
}
var firstChar = selector[0];
return firstChar === '#' || firstChar === '.' || firstChar === firstChar.toUpperCase();
}
/** /**
* Container constructor.  Containers are used to contain nodes or other containers * Container constructor.  Containers are used to contain nodes or other containers
* @constructor * @constructor
@ -174,7 +182,11 @@
for (n = 0; n < len; n++) { for (n = 0; n < len; n++) {
sel = selectorArr[n]; sel = selectorArr[n];
if (!isValidSelector(sel)) {
Konva.Util.warn('Selector "' + sel + '" is invalid. Allowed selectors examples are "#foo", ".bar" or "Group".');
Konva.Util.warn('If you have a custom shape with such className, please change it to start with upper letter like "Triangle".');
Konva.Util.warn('Konva is awesome, right?');
}
// id selector // id selector
if(sel.charAt(0) === '#') { if(sel.charAt(0) === '#') {
node = this._getNodeById(sel.slice(1)); node = this._getNodeById(sel.slice(1));

View File

@ -236,8 +236,8 @@
return this; return this;
}, },
/** /**
* return client rectangle (x, y, width, height) of node. This rectangle also include all styling (strokes, shadows, etc). * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
* This rectangle relative to parent container. * The rectangle position is relative to parent container.
* @method * @method
* @memberof Konva.Node.prototype * @memberof Konva.Node.prototype
* @param {Boolean} [skipTransform] flag should we skip transformation to rectangle * @param {Boolean} [skipTransform] flag should we skip transformation to rectangle

View File

@ -1089,6 +1089,47 @@ suite('Container', function() {
layer.draw(); layer.draw();
}); });
test('warn on invalid selector', function() {
var stage = addStage();
var layer = new Konva.Layer({
name: 'layerName',
id: 'layerId'
});
var group = new Konva.Group({
name: 'groupName',
id: 'groupId'
});
var rect = new Konva.Rect({
x: 200,
y: 20,
width: 100,
height: 50,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
name: 'rectName',
id: 'rectId'
});
stage.add(layer);
layer.add(group);
group.add(rect);
layer.draw();
var counter = 0;
var oldWarn = Konva.Util.warn;
Konva.Util.warn = function() {
counter += 1;
};
// forgot dot
group.find('rectName');
assert.equal(counter > 0, true);
Konva.Util.warn = oldWarn;
});
// ====================================================== // ======================================================
test('add layer then shape', function() { test('add layer then shape', function() {
var stage = addStage(); var stage = addStage();