Browse Source

Merge pull request #11494 from donmccurdy/feat-gltf2-sanitize-node-names

[gltf] Sanitize node names.
Mr.doob 8 years ago
parent
commit
dde32ca7f1

+ 6 - 0
examples/js/loaders/GLTF2Loader.js

@@ -2540,6 +2540,12 @@ THREE.GLTF2Loader = ( function () {
 
 			}
 
+			if ( _node.name !== undefined ) {
+
+				_node.name = THREE.PropertyBinding.sanitizeNodeName( _node.name );
+
+			}
+
 			if ( node.extras ) _node.userData = node.extras;
 
 			if ( node.matrix !== undefined ) {

+ 13 - 0
src/animation/PropertyBinding.js

@@ -102,6 +102,19 @@ Object.assign( PropertyBinding, {
 
 	},
 
+	/**
+	 * Replaces spaces with underscores and removes unsupported characters from
+	 * node names, to ensure compatibility with parseTrackName().
+	 *
+	 * @param  {string} name Node name to be sanitized.
+	 * @return {string}
+	 */
+	sanitizeNodeName: function ( name ) {
+
+		return name.replace( /\s/g, '_' ).replace( /[^\w-]/g, '' );
+
+	},
+
 	parseTrackName: function () {
 
 		// Parent directories, delimited by '/' or ':'. Currently unused, but must

+ 22 - 0
test/unit/src/animation/PropertyBinding.js

@@ -198,3 +198,25 @@ QUnit.test( 'parseTrackName' , function( assert ) {
 
 	} );
 });
+
+QUnit.test( 'sanitizeNodeName' , function( assert ) {
+
+	assert.equal(
+		THREE.PropertyBinding.sanitizeNodeName( 'valid-name-123_' ),
+		'valid-name-123_',
+		'Leaves valid name intact.'
+	);
+
+	assert.equal(
+		THREE.PropertyBinding.sanitizeNodeName( 'space separated name 123_ -' ),
+		'space_separated_name_123__-',
+		'Replaces spaces with underscores.'
+	);
+
+	assert.equal(
+		THREE.PropertyBinding.sanitizeNodeName( '"invalid" name %123%_' ),
+		'invalid_name_123_',
+		'Strips invalid characters.'
+	);
+
+} );