Skip to content

Commit 3989e6d

Browse files
committed
added support for outputing streams to form elements
1 parent fbbb88b commit 3989e6d

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

dist/watson-speech.js

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"eslint": "^1.10.3",
2424
"eslint-config-google": "^0.3.0",
2525
"expect.js": "^0.3.1",
26+
"jquery": "^2.2.1",
2627
"jsdoc": "^3.4.0",
2728
"karma": "^0.13.19",
2829
"karma-browserify": "^5.0.1",

speech-to-text/writable-element-stream.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,40 @@ var defaults = require('defaults');
99
*
1010
* Can show interim results when in objectMode
1111
*
12-
* @todo: consider automatically setting the value attribute on <input> elements
13-
*
1412
* @param options
1513
* @param {String|DOMElement} options.outputElement
14+
* @param {String} [options.property] what property of the element should the text be set to. Defaults to `value` for `<input>`s and `<textarea>`s, `textContent` for everything else
1615
* @param {Boolean} [options.clear=true] delete any previous text
1716
* @constructor
1817
*/
1918
function WritableElementStream(options) {
2019
this.options = options = defaults(options, {
2120
decodeStrings: false, // false = don't convert strings to buffers before passing to _write (only applies in string mode)
21+
property: null,
2222
clear: true
2323
});
2424

2525
this.el = typeof options.outputElement === 'string' ? document.querySelector(options.outputElement) : options.outputElement;
2626

2727
if (!this.el) {
28-
throw new Error('Watson Speech to Text WriteableElementStream: missing outputElement');
28+
throw new Error('Watson Speech to Text WritableElementStream: missing outputElement');
2929
}
3030

3131
Writable.call(this, options);
3232

33+
// for most elements we set the textContent, but for form elements, the value property is probably the expected target
34+
var propMap = {
35+
INPUT: 'value',
36+
TEXTAREA: 'value'
37+
};
38+
this.prop = options.property || propMap[this.el.nodeName] || 'textContent';
39+
3340
if (options.clear) {
34-
this.el.textContent = '';
41+
this.el[this.prop] = '';
3542
}
3643

3744
if (options.objectMode) {
38-
this.finalizedText = this.el.textContent;
45+
this.finalizedText = this.el[this.prop];
3946
this._write = this.writeObject;
4047
} else {
4148
this._write = this.writeString;
@@ -45,16 +52,16 @@ util.inherits(WritableElementStream, Writable);
4552

4653

4754
WritableElementStream.prototype.writeString = function writeString(text, encoding, next) {
48-
this.el.textContent += text;
55+
this.el[this.prop] += text;
4956
next();
5057
};
5158

5259
WritableElementStream.prototype.writeObject = function writeObject(result, encoding, next) {
5360
if (result.final) {
5461
this.finalizedText += result.alternatives[0].transcript;
55-
this.el.textContent = this.finalizedText;
62+
this.el[this.prop] = this.finalizedText;
5663
} else {
57-
this.el.textContent = this.finalizedText + result.alternatives[0].transcript
64+
this.el[this.prop] = this.finalizedText + result.alternatives[0].transcript
5865
}
5966
next();
6067
};

test/writable-element-stream-spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var assert = require('assert');
44

55
var WritableElementStream = require('../speech-to-text/writable-element-stream.js');
66

7+
var $ = require('jquery');
8+
79
describe('WritableElementStream', function() {
810

911
it ('should accept strings/buffers and write out contents when in string mode', function() {
@@ -52,4 +54,21 @@ describe('WritableElementStream', function() {
5254
}]});
5355
assert.equal(el.textContent, 'abcdef');
5456
});
57+
58+
['<textarea/>','<input type="text"/>'].forEach(function(tag) {
59+
it ('should set the correct value for ' + tag, function() {
60+
var $el = $(tag),
61+
el = $el[0];
62+
var s = new WritableElementStream({outputElement: el, objectMode: true});
63+
s.write({final: true, alternatives: [{
64+
transcript: 'abc'
65+
}]});
66+
s.write({final: true, alternatives: [{
67+
transcript: '123'
68+
}]});
69+
assert.equal($el.val(), 'abc123'); // trust jQuery to know what the correct attribute *should* be
70+
});
71+
});
72+
73+
5574
});

0 commit comments

Comments
 (0)