Skip to content

Commit

Permalink
[gltf] Sanitize node names.
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Jun 12, 2017
1 parent 31ba90d commit 6324c1f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/js/loaders/GLTF2Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,12 @@ THREE.GLTF2Loader = ( function () {

}

if ( _node.name !== undefined ) {

_node.name = THREE.PropertyBinding.sanitizeTrackName( _node.name );

}

if ( node.extras ) _node.userData = node.extras;

if ( node.matrix !== undefined ) {
Expand Down
13 changes: 13 additions & 0 deletions src/animation/PropertyBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ Object.assign( PropertyBinding, {

},

/**
* Replaces spaces with underscores and removes unsupported characters from
* track names, to ensure compatibility with parseTrackName().
*
* @param {string} trackName Track name to be sanitized.
* @return {string}
*/
sanitizeTrackName: function ( trackName ) {

return trackName.replace( /\s/g, '_' ).replace( /[^\w-]/g, '' );

},

parseTrackName: function () {

// Parent directories, delimited by '/' or ':'. Currently unused, but must
Expand Down
22 changes: 22 additions & 0 deletions test/unit/src/animation/PropertyBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,25 @@ QUnit.test( 'parseTrackName' , function( assert ) {

} );
});

QUnit.test( 'sanitizeTrackName' , function( assert ) {

assert.equal(
THREE.PropertyBinding.sanitizeTrackName( 'valid-name-123_' ),
'valid-name-123_',
'Leaves valid name intact.'
);

assert.equal(
THREE.PropertyBinding.sanitizeTrackName( 'space separated name 123_ -' ),
'space_separated_name_123__-',
'Replaces spaces with underscores.'
);

assert.equal(
THREE.PropertyBinding.sanitizeTrackName( '"invalid" name %123%_' ),
'invalid_name_123_',
'Strips invalid characters.'
);

} );

0 comments on commit 6324c1f

Please sign in to comment.