summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chromeos/chromevox/testing/common.js
blob: 83570fc8fae521ba915a017f084b35b6c6c24655 (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
// 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.

// Common testing utilities.

/**
 * Shortcut for document.getElementById.
 * @param {string} id of the element.
 * @return {HTMLElement} with the id.
 */
function $(id) {
  return document.getElementById(id);
}

/**
 * @constructor
 */
var TestUtils = function() {};

/**
 * Extracts some inlined html encoded as a comment inside a function,
 * so you can use it like this:
 *
 * this.appendDoc(function() {/*!
 *     <p>Html goes here</p>
 * * /});
 *
 * @param {Function} commentEncodedHtml The html , embedded as a
 *     comment inside an anonymous function - see example, above.
 * @param {!Array=} opt_args Optional arguments to be substituted in the form
 *     $0, ... within the code block.
 * @return {string} The html text.
*/
TestUtils.extractHtmlFromCommentEncodedString =
    function(commentEncodedHtml, opt_args) {
  var stringified = commentEncodedHtml.toString();
  if (opt_args) {
    for (var i = 0; i < opt_args.length; i++)
      stringified = stringified.replace('$' + i, opt_args[i]);
  }
  return stringified.replace(/^[^\/]+\/\*!?/, '').replace(/\*\/[^\/]+$/, '');
};