summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/speech_rules/speech_rule_functions.js
blob: f4edabb8255ec18e7d2dd5b17179c6881d5d57ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Classes for custom functions for the speech rule engine.
 *
 */

goog.provide('cvox.SpeechRuleFunctions');
goog.provide('cvox.SpeechRuleFunctions.ContextFunctions');
goog.provide('cvox.SpeechRuleFunctions.CustomQueries');
goog.provide('cvox.SpeechRuleFunctions.CustomStrings');



/**
 * @constructor
 */
cvox.SpeechRuleFunctions = function() { };


/**
 * Private superclass of all the custom function stores.
 * @constructor
 * @param {string} prefix A prefix string for the function names.
 * @param {Object<string, Function>} store Storage object.
 * @private
 */
cvox.SpeechRuleFunctions.Store_ = function(prefix, store) {
  /** @private */
  this.prefix_ = prefix;
  /** @private */
  this.store_ = store;
};


/**
 * Adds a new function for the function store.
 * @param {string} name A name.
 * @param {!Function} func A function.
 */
cvox.SpeechRuleFunctions.Store_.prototype.add = function(name, func) {
  if (this.checkCustomFunctionSyntax_(name)) {
    this.store_[name] = func;
  }
};


/**
 * Retrieves a function with the given name if one exists.
 * @param {string} name A name.
 * @return {Function} The function if it exists.
 */
cvox.SpeechRuleFunctions.Store_.prototype.lookup = function(name) {
  return this.store_[name];
};


/**
 * Context function for use in speech rules.
 * @typedef {function(!Node): Array<Node>}
 */
cvox.SpeechRuleFunctions.CustomQuery;


/**
 * @constructor
 * @extends {cvox.SpeechRuleFunctions.Store_}
 */
cvox.SpeechRuleFunctions.CustomQueries = function() {
  var store =
    /** @type {Object<string, cvox.SpeechRuleFunctions.CustomQuery>} */ ({});
  goog.base(this, 'CQF', store);
};
goog.inherits(cvox.SpeechRuleFunctions.CustomQueries,
              cvox.SpeechRuleFunctions.Store_);


/**
 * Context function for use in speech rules.
 * @typedef {function(!Node): string}
 */
cvox.SpeechRuleFunctions.CustomString;


/**
 * @constructor
 * @extends {cvox.SpeechRuleFunctions.Store_}
 */
cvox.SpeechRuleFunctions.CustomStrings = function() {
  var store =
    /** @type {Object<string, cvox.SpeechRuleFunctions.CustomString>} */ ({});
  goog.base(this, 'CSF', store);
};
goog.inherits(cvox.SpeechRuleFunctions.CustomStrings,
              cvox.SpeechRuleFunctions.Store_);


/**
 * Context function for use in speech rules.
 * @typedef {function(Array<Node>, ?string): (function(): string)}
 */
cvox.SpeechRuleFunctions.ContextFunction;


/**
 * @constructor
 * @extends {cvox.SpeechRuleFunctions.Store_}
 */
cvox.SpeechRuleFunctions.ContextFunctions = function() {
  var store =
    /** @type {Object<string, cvox.SpeechRuleFunctions.ContextFunction>} */
  ({});
  goog.base(this, 'CTXF', store);
};
goog.inherits(cvox.SpeechRuleFunctions.ContextFunctions,
              cvox.SpeechRuleFunctions.Store_);


/**
 * Checks validity for a custom function name.
 * @param {string} name The name of the custom function.
 * @return {!boolean} True if the name is valid.
 * @private
 */
cvox.SpeechRuleFunctions.Store_.prototype.
    checkCustomFunctionSyntax_ = function(name) {
      var reg = new RegExp('^' + this.prefix_);
      if (!name.match(reg)) {
        console.log(
            'FunctionError: Invalid function name. Expected prefix' +
                this.prefix_);
        return false;
      }
      return true;
    };