OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview An interface definition of a speech rule. | 6 * @fileoverview An interface definition of a speech rule. |
7 * | 7 * |
8 * A speech rule is a data structure along with supporting methods that | 8 * A speech rule is a data structure along with supporting methods that |
9 * stipulates how to transform a tree structure such as XML, a browser DOM, or | 9 * stipulates how to transform a tree structure such as XML, a browser DOM, or |
10 * HTML into a format (usually strings) suitable for rendering by a | 10 * HTML into a format (usually strings) suitable for rendering by a |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 return output; | 172 return output; |
173 }; | 173 }; |
174 | 174 |
175 | 175 |
176 /** | 176 /** |
177 * @override | 177 * @override |
178 */ | 178 */ |
179 cvox.SpeechRule.Component.prototype.toString = function() { | 179 cvox.SpeechRule.Component.prototype.toString = function() { |
180 var strs = ''; | 180 var strs = ''; |
181 strs += cvox.SpeechRule.Type.toString(this.type); | 181 strs += cvox.SpeechRule.Type.toString(this.type); |
182 strs += this.content ? ' ' + this.content : ''; | 182 strs += this.content ? ' ' + this.content : ''; |
183 var attribs = this.getAttributes(); | 183 var attribs = this.getAttributes(); |
184 if (attribs.length > 0) { | 184 if (attribs.length > 0) { |
185 strs += ' (' + attribs.join(', ') + ')'; | 185 strs += ' (' + attribs.join(', ') + ')'; |
186 } | 186 } |
187 return strs; | 187 return strs; |
188 }; | 188 }; |
189 | 189 |
190 | 190 |
191 /** | 191 /** |
192 * Adds a single attribute to the component. | 192 * Adds a single attribute to the component. |
(...skipping 20 matching lines...) Expand all Loading... |
213 } | 213 } |
214 var attribs = cvox.SpeechRule.splitString_(attrs.slice(1, -1), ','); | 214 var attribs = cvox.SpeechRule.splitString_(attrs.slice(1, -1), ','); |
215 for (var i = 0; i < attribs.length; i++) { | 215 for (var i = 0; i < attribs.length; i++) { |
216 this.addAttribute(attribs[i]); | 216 this.addAttribute(attribs[i]); |
217 } | 217 } |
218 }; | 218 }; |
219 | 219 |
220 | 220 |
221 /** | 221 /** |
222 * Transforms the attributes of an object into a list of strings. | 222 * Transforms the attributes of an object into a list of strings. |
223 * @return {Array.<string>} List of translated attribute:value strings. | 223 * @return {Array<string>} List of translated attribute:value strings. |
224 */ | 224 */ |
225 cvox.SpeechRule.Component.prototype.getAttributes = function() { | 225 cvox.SpeechRule.Component.prototype.getAttributes = function() { |
226 var attribs = []; | 226 var attribs = []; |
227 for (var key in this) { | 227 for (var key in this) { |
228 if (key != 'content' && key != 'type' && typeof(this[key]) != 'function') { | 228 if (key != 'content' && key != 'type' && typeof(this[key]) != 'function') { |
229 attribs.push(key + ':' + this[key]); | 229 attribs.push(key + ':' + this[key]); |
230 } | 230 } |
231 } | 231 } |
232 return attribs; | 232 return attribs; |
233 }; | 233 }; |
234 | 234 |
235 | 235 |
236 /** | 236 /** |
237 * A speech rule is a collection of speech components. | 237 * A speech rule is a collection of speech components. |
238 * @param {Array.<cvox.SpeechRule.Component>} components The input rule. | 238 * @param {Array<cvox.SpeechRule.Component>} components The input rule. |
239 * @constructor | 239 * @constructor |
240 */ | 240 */ |
241 cvox.SpeechRule.Action = function(components) { | 241 cvox.SpeechRule.Action = function(components) { |
242 /** @type {Array.<cvox.SpeechRule.Component>} */ | 242 /** @type {Array<cvox.SpeechRule.Component>} */ |
243 this.components = components; | 243 this.components = components; |
244 }; | 244 }; |
245 | 245 |
246 | 246 |
247 /** | 247 /** |
248 * Parses an input string into a speech rule class object. | 248 * Parses an input string into a speech rule class object. |
249 * @param {string} input The input string. | 249 * @param {string} input The input string. |
250 * @return {cvox.SpeechRule.Action} The resulting object. | 250 * @return {cvox.SpeechRule.Action} The resulting object. |
251 */ | 251 */ |
252 cvox.SpeechRule.Action.fromString = function(input) { | 252 cvox.SpeechRule.Action.fromString = function(input) { |
(...skipping 18 matching lines...) Expand all Loading... |
271 var comps = this.components.map(function(c) { return c.toString(); }); | 271 var comps = this.components.map(function(c) { return c.toString(); }); |
272 return comps.join('; '); | 272 return comps.join('; '); |
273 }; | 273 }; |
274 | 274 |
275 | 275 |
276 // TODO (sorge) Separatation of xpath expressions and custom functions. | 276 // TODO (sorge) Separatation of xpath expressions and custom functions. |
277 // Also test validity of xpath expressions. | 277 // Also test validity of xpath expressions. |
278 /** | 278 /** |
279 * Constructs a valid precondition for a speech rule. | 279 * Constructs a valid precondition for a speech rule. |
280 * @param {string} query A node selector function or xpath expression. | 280 * @param {string} query A node selector function or xpath expression. |
281 * @param {Array.<string>=} opt_constraints A list of constraint functions. | 281 * @param {Array<string>=} opt_constraints A list of constraint functions. |
282 * @constructor | 282 * @constructor |
283 */ | 283 */ |
284 cvox.SpeechRule.Precondition = function(query, opt_constraints) { | 284 cvox.SpeechRule.Precondition = function(query, opt_constraints) { |
285 /** @type {string} */ | 285 /** @type {string} */ |
286 this.query = query; | 286 this.query = query; |
287 | 287 |
288 /** @type {!Array.<string>} */ | 288 /** @type {!Array<string>} */ |
289 this.constraints = opt_constraints || []; | 289 this.constraints = opt_constraints || []; |
290 }; | 290 }; |
291 | 291 |
292 | 292 |
293 /** | 293 /** |
294 * @override | 294 * @override |
295 */ | 295 */ |
296 cvox.SpeechRule.Precondition.prototype.toString = function() { | 296 cvox.SpeechRule.Precondition.prototype.toString = function() { |
297 var constrs = this.constraints.join(', '); | 297 var constrs = this.constraints.join(', '); |
298 return this.query + ', ' + constrs; | 298 return this.query + ', ' + constrs; |
299 }; | 299 }; |
300 | 300 |
301 | 301 |
302 /** | 302 /** |
303 * Split a string wrt. a given separator symbol while not splitting inside of a | 303 * Split a string wrt. a given separator symbol while not splitting inside of a |
304 * double quoted string. For example, splitting | 304 * double quoted string. For example, splitting |
305 * '[t] "matrix; 3 by 3"; [n] ./*[1]' with separators ';' would yield | 305 * '[t] "matrix; 3 by 3"; [n] ./*[1]' with separators ';' would yield |
306 * ['[t] "matrix; 3 by 3"', ' [n] ./*[1]']. | 306 * ['[t] "matrix; 3 by 3"', ' [n] ./*[1]']. |
307 * @param {string} str String to be split. | 307 * @param {string} str String to be split. |
308 * @param {string} sep Separator symbol. | 308 * @param {string} sep Separator symbol. |
309 * @return {Array.<string>} A list of single component strings. | 309 * @return {Array<string>} A list of single component strings. |
310 * @private | 310 * @private |
311 */ | 311 */ |
312 cvox.SpeechRule.splitString_ = function(str, sep) { | 312 cvox.SpeechRule.splitString_ = function(str, sep) { |
313 var strList = []; | 313 var strList = []; |
314 var prefix = ''; | 314 var prefix = ''; |
315 | 315 |
316 while (str != '') { | 316 while (str != '') { |
317 var sepPos = str.search(sep); | 317 var sepPos = str.search(sep); |
318 if (sepPos == -1) { | 318 if (sepPos == -1) { |
319 if ((str.match(/"/g) || []).length % 2 != 0) { | 319 if ((str.match(/"/g) || []).length % 2 != 0) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 */ | 354 */ |
355 cvox.SpeechRule.DynamicCstrAttrib = | 355 cvox.SpeechRule.DynamicCstrAttrib = |
356 { | 356 { |
357 STYLE: 'style' | 357 STYLE: 'style' |
358 }; | 358 }; |
359 | 359 |
360 | 360 |
361 /** | 361 /** |
362 * Dynamic constraints are a means to specialize rules that can be changed | 362 * Dynamic constraints are a means to specialize rules that can be changed |
363 * dynamically by the user, for example by choosing different styles, etc. | 363 * dynamically by the user, for example by choosing different styles, etc. |
364 * @typedef {!Object.<cvox.SpeechRule.DynamicCstrAttrib, string>} | 364 * @typedef {!Object<cvox.SpeechRule.DynamicCstrAttrib, string>} |
365 */ | 365 */ |
366 cvox.SpeechRule.DynamicCstr; | 366 cvox.SpeechRule.DynamicCstr; |
367 | 367 |
368 | 368 |
369 /** | 369 /** |
370 * Error object for signaling parsing errors. | 370 * Error object for signaling parsing errors. |
371 * @param {string} msg The error message. | 371 * @param {string} msg The error message. |
372 * @constructor | 372 * @constructor |
373 * @extends {Error} | 373 * @extends {Error} |
374 */ | 374 */ |
375 cvox.SpeechRule.OutputError = function(msg) { | 375 cvox.SpeechRule.OutputError = function(msg) { |
376 this.name = 'RuleError'; | 376 this.name = 'RuleError'; |
377 this.message = msg || ''; | 377 this.message = msg || ''; |
378 }; | 378 }; |
379 goog.inherits(cvox.SpeechRule.OutputError, Error); | 379 goog.inherits(cvox.SpeechRule.OutputError, Error); |
OLD | NEW |