aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/equation/imagerenderer.js
blob: 7c88dc1c0e773203631f413770f83db89f846ce2 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Functions for rendering the equation images.
 *
 */

goog.provide('goog.ui.equation.ImageRenderer');

goog.require('goog.dom.TagName');
goog.require('goog.dom.classes');
goog.require('goog.string');
goog.require('goog.uri.utils');


/**
 * The server name which renders the equations.
 * We use https as equations may be embedded in https pages
 * and using https prevents mixed content warnings. Note that
 * https equations work only on google.com domains.
 * @type {string}
 * @private
 */
goog.ui.equation.ImageRenderer.SERVER_NAME_ =
    'https://www.google.com';


/**
 * The longest equation which may be displayed, in characters.
 * @type {number}
 */
goog.ui.equation.ImageRenderer.MAX_EQUATION_LENGTH = 200;


/**
 * Class to put on our equations IMG elements.
 * @type {string}
 */
goog.ui.equation.ImageRenderer.EE_IMG_CLASS = 'ee_img';


/**
 * Non-standard to put on our equations IMG elements. Useful when classes need
 * to be scrubbed from the user-generated HTML, but non-standard attributes
 * can be white-listed.
 *
 * @type {string}
 */
goog.ui.equation.ImageRenderer.EE_IMG_ATTR = 'eeimg';


/**
 * Vertical alignment for the equations IMG elements.
 * @type {string}
 */
goog.ui.equation.ImageRenderer.EE_IMG_VERTICAL_ALIGN = 'middle';


/**
 * The default background color as used in the img url, which is fully
 * transparent white.
 * @type {string}
 */
goog.ui.equation.ImageRenderer.BACKGROUND_COLOR = 'FFFFFF00';


/**
 * The default foreground color as used in the img url, which is black.
 * @type {string}
 */
goog.ui.equation.ImageRenderer.FOREGROUND_COLOR = '000000';


/**
 * Class to put on IMG elements to keep the resize property bubble from
 * appearing. This is different from PLACEHOLDER_IMG_CLASS because it's
 * reasonable in some cases to be able to resize a placeholder (which should
 * be reflected when the placeholder is replaced with the other content).
 * @type {string}
 */
goog.ui.equation.ImageRenderer.NO_RESIZE_IMG_CLASS =
    goog.getCssName('tr_noresize');


/**
 * Returns the equation image src url given the equation.
 * @param {string} equation The equation.
 * @return {string} The equation image src url (empty string in case the
 *   equation was empty).
 */
goog.ui.equation.ImageRenderer.getImageUrl = function(equation) {
  if (!equation) {
    return '';
  }

  var url = goog.ui.equation.ImageRenderer.SERVER_NAME_ +
      '/chart?cht=tx' +
      '&chf=bg,s,' +
      goog.ui.equation.ImageRenderer.BACKGROUND_COLOR +
      '&chco=' +
      goog.ui.equation.ImageRenderer.FOREGROUND_COLOR +
      '&chl=' +
      encodeURIComponent(equation);
  return url;
};


/**
 * Returns the equation string src for given image url.
 * @param {string} imageUrl The image url.
 * @return {string?} The equation string, null if imageUrl cannot be parsed.
 */
goog.ui.equation.ImageRenderer.getEquationFromImageUrl = function(imageUrl) {
  return goog.uri.utils.getParamValue(imageUrl, 'chl');
};


/**
 * Gets the equation string from the given equation IMG node. Returns empty
 * string if the src attribute of the is not a valid equation url.
 * @param {Element} equationNode The equation IMG element.
 * @return {string} The equation string.
 */
goog.ui.equation.ImageRenderer.getEquationFromImage = function(equationNode) {
  var url = equationNode.getAttribute('src');
  if (!url) {
    // Should never happen.
    return '';
  }
  return goog.ui.equation.ImageRenderer.getEquationFromImageUrl(
      url) || '';
};


/**
 * Checks whether given node is an equation element.
 * @param {Node} node The node to check.
 * @return {boolean} Whether given node is an equation element.
 */
goog.ui.equation.ImageRenderer.isEquationElement = function(node) {
  return node.nodeName == goog.dom.TagName.IMG &&
      (node.getAttribute(
      goog.ui.equation.ImageRenderer.EE_IMG_ATTR) ||
          goog.dom.classes.has(node,
              goog.ui.equation.ImageRenderer.EE_IMG_CLASS));
};


/**
 * Returns the html for the html image tag for the given equation.
 * @param {string} equation The equation.
 * @return {string} The html code to embed in the document.
 */
goog.ui.equation.ImageRenderer.getHtml = function(equation) {
  var imageSrc =
      goog.ui.equation.ImageRenderer.getImageUrl(equation);
  if (!imageSrc) {
    return '';
  }
  return '<img src="' + imageSrc + '" ' +
      'alt="' + goog.string.htmlEscape(equation) + '" ' +
      'class="' + goog.ui.equation.ImageRenderer.EE_IMG_CLASS +
      ' ' + goog.ui.equation.ImageRenderer.NO_RESIZE_IMG_CLASS +
      '" ' + goog.ui.equation.ImageRenderer.EE_IMG_ATTR + '="1" ' +
      'style="vertical-align: ' +
      goog.ui.equation.ImageRenderer.EE_IMG_VERTICAL_ALIGN + '">';
};


/**
 * Checks whether equation is too long to be displayed.
 * @param {string} equation The equation to test.
 * @return {boolean} Whether the equation is too long.
 */
goog.ui.equation.ImageRenderer.isEquationTooLong = function(equation) {
  return equation.length >
      goog.ui.equation.ImageRenderer.MAX_EQUATION_LENGTH;
};