blob: de2f1d898f676a26e6df23e418606b8254e641c9 [file] [log] [blame]
<html><head><style>
.KEYW {color: #933;}
.COMM {color: #bbb; font-style: italic;}
.NUMB {color: #393;}
.STRN {color: #393;}
.REGX {color: #339;}
.linenumber {border-right: 1px dotted #666; color: #666; font-style: normal;}
</style></head><body><pre><span class='linenumber'> 1</span> <span class="COMM">// Copyright 2009 Google Inc. All Rights Reserved.</span><span class="WHIT">
<span class='linenumber'> 2</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='linenumber'> 3</span> </span><span class="COMM">// Licensed under the Apache License, Version 2.0 (the "License");</span><span class="WHIT">
<span class='linenumber'> 4</span> </span><span class="COMM">// you may not use this file except in compliance with the License.</span><span class="WHIT">
<span class='linenumber'> 5</span> </span><span class="COMM">// You may obtain a copy of the License at</span><span class="WHIT">
<span class='linenumber'> 6</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='linenumber'> 7</span> </span><span class="COMM">// http://www.apache.org/licenses/LICENSE-2.0</span><span class="WHIT">
<span class='linenumber'> 8</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='linenumber'> 9</span> </span><span class="COMM">// Unless required by applicable law or agreed to in writing, software</span><span class="WHIT">
<span class='linenumber'> 10</span> </span><span class="COMM">// distributed under the License is distributed on an "AS-IS" BASIS,</span><span class="WHIT">
<span class='linenumber'> 11</span> </span><span class="COMM">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span><span class="WHIT">
<span class='linenumber'> 12</span> </span><span class="COMM">// See the License for the specific language governing permissions and</span><span class="WHIT">
<span class='linenumber'> 13</span> </span><span class="COMM">// limitations under the License.</span><span class="WHIT">
<span class='linenumber'> 14</span>
<span class='linenumber'> 15</span> </span><span class="COMM">/**
<span class='linenumber'> 16</span> * @fileoverview Testing utilities to check a web page for BiDi-related errors.
<span class='linenumber'> 17</span> * This file has the top-level entry points for use as a Javascript API, along
<span class='linenumber'> 18</span> * with JSON-based interface functions meant to be called from an external
<span class='linenumber'> 19</span> * testing framework like WebDriver.
<span class='linenumber'> 20</span> */</span><span class="WHIT">
<span class='linenumber'> 21</span>
<span class='linenumber'> 22</span>
<span class='linenumber'> 23</span> </span><span class="NAME">goog.provide</span><span class="PUNC">(</span><span class="STRN">'bidichecker'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 24</span>
<span class='linenumber'> 25</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'bidichecker.Error'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 26</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'bidichecker.ErrorGui'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 27</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'bidichecker.Filter'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 28</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'bidichecker.FilterFactory'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 29</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'bidichecker.TextErrorScanner'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 30</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'goog.array'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 31</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'goog.i18n.bidi'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 32</span> </span><span class="NAME">goog.require</span><span class="PUNC">(</span><span class="STRN">'goog.json'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 33</span>
<span class='linenumber'> 34</span>
<span class='linenumber'> 35</span> </span><span class="COMM">/**
<span class='linenumber'> 36</span> * Checks the contents of the current web page, including all nested frames, for
<span class='linenumber'> 37</span> * BiDi-related errors. If <code>opt_element</code> is specified, checks only within
<span class='linenumber'> 38</span> * the given element of the DOM (including any nested frames). This is the main
<span class='linenumber'> 39</span> * entry point for the Javascript API.
<span class='linenumber'> 40</span> * @param {boolean} shouldBeRtl Is the root expected to be right-to-left?
<span class='linenumber'> 41</span> * @param {Element=} opt_element The root element of the DOM subtree to be
<span class='linenumber'> 42</span> * checked; if null, checks the entire current web page.
<span class='linenumber'> 43</span> * @param {Array.&lt;!bidichecker.Filter>=} opt_filters Error suppression filters.
<span class='linenumber'> 44</span> * @return {!Array.&lt;!bidichecker.Error>} Array of error objects for all failing
<span class='linenumber'> 45</span> * checks.
<span class='linenumber'> 46</span> * @export
<span class='linenumber'> 47</span> */</span><span class="WHIT">
<span class='linenumber'> 48</span> </span><span class="NAME">bidichecker.checkPage</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">shouldBeRtl</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_element</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_filters</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='linenumber'> 49</span> </span><span class="NAME">bidichecker.Error.clearHighlightableAreas</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 50</span> </span><span class="COMM">// {@top.document.body} always finds the top level of the current page, even</span><span class="WHIT">
<span class='linenumber'> 51</span> </span><span class="COMM">// if we started within a frame.</span><span class="WHIT">
<span class='linenumber'> 52</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">element</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="COMM">/** @type {!Element} */</span><span class="WHIT"> </span><span class="NAME">opt_element</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="NAME">top.document.body</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 53</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">expectedDir</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT">
<span class='linenumber'> 54</span> </span><span class="NAME">shouldBeRtl</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">goog.i18n.bidi.Dir.RTL</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">goog.i18n.bidi.Dir.LTR</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 55</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">scanner</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">bidichecker.TextErrorScanner</span><span class="PUNC">(</span><span class="NAME">opt_filters</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 56</span> </span><span class="NAME">scanner.scan</span><span class="PUNC">(</span><span class="NAME">element</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">expectedDir</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 57</span> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">scanner.getErrors</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 58</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 59</span>
<span class='linenumber'> 60</span>
<span class='linenumber'> 61</span> </span><span class="COMM">/**
<span class='linenumber'> 62</span> * JSON interface to <code>checkPage</code>.
<span class='linenumber'> 63</span> * @param {boolean} shouldBeRtl Is the element/page expected to be
<span class='linenumber'> 64</span> * right-to-left?
<span class='linenumber'> 65</span> * @param {Element=} opt_element The root element of the DOM subtree to be
<span class='linenumber'> 66</span> * checked; if null, checks the entire current web page.
<span class='linenumber'> 67</span> * @param {string=} opt_filtersJson Error suppression filters in a JSON string.
<span class='linenumber'> 68</span> * @return {string} Array of <code>bidichecker.Error</code> objects for all failing
<span class='linenumber'> 69</span> * checks, in JSON format. If no errors were found, returns an empty array
<span class='linenumber'> 70</span> * ("[]").
<span class='linenumber'> 71</span> * @export
<span class='linenumber'> 72</span> */</span><span class="WHIT">
<span class='linenumber'> 73</span> </span><span class="NAME">bidichecker.checkPageToJson</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">shouldBeRtl</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_element</span><span class="PUNC">,</span><span class="WHIT">
<span class='linenumber'> 74</span> </span><span class="NAME">opt_filtersJson</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='linenumber'> 75</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">filters</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">bidichecker.FilterFactory.readFiltersFromJson</span><span class="PUNC">(</span><span class="NAME">opt_filtersJson</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 76</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">errors</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">bidichecker.checkPage</span><span class="PUNC">(</span><span class="NAME">shouldBeRtl</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_element</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">filters</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 77</span> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">bidichecker.Error.serialize</span><span class="PUNC">(</span><span class="NAME">errors</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 78</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 79</span>
<span class='linenumber'> 80</span>
<span class='linenumber'> 81</span> </span><span class="COMM">/**
<span class='linenumber'> 82</span> * Runs the interactive visual error browsing mode.
<span class='linenumber'> 83</span> * @param {Array.&lt;!bidichecker.Error>} errors An array of errors to be
<span class='linenumber'> 84</span> * displayed.
<span class='linenumber'> 85</span> * @export
<span class='linenumber'> 86</span> */</span><span class="WHIT">
<span class='linenumber'> 87</span> </span><span class="NAME">bidichecker.runGui</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">errors</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='linenumber'> 88</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">gui</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">bidichecker.ErrorGui</span><span class="PUNC">(</span><span class="NAME">errors</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 89</span> </span><span class="NAME">gui.launch</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 90</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'> 91</span>
<span class='linenumber'> 92</span>
<span class='linenumber'> 93</span> </span><span class="COMM">/**
<span class='linenumber'> 94</span> * JSON interface to <code>runGui</code>.
<span class='linenumber'> 95</span> * @param {string} errorsJson An array of errors to be displayed, in JSON
<span class='linenumber'> 96</span> * format (similar to that produced by <code>checkPageJson()</code>).
<span class='linenumber'> 97</span> * @export
<span class='linenumber'> 98</span> */</span><span class="WHIT">
<span class='linenumber'> 99</span> </span><span class="NAME">bidichecker.runGuiFromJson</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">errorsJson</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='linenumber'>100</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">bareErrors</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="COMM">/** @type {Array.&lt;!Object>} */</span><span class="WHIT"> </span><span class="NAME">goog.json.parse</span><span class="PUNC">(</span><span class="NAME">errorsJson</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'>101</span>
<span class='linenumber'>102</span> </span><span class="COMM">/** @type {Array.&lt;!bidichecker.Error>} */</span><span class="WHIT">
<span class='linenumber'>103</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">errors</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">goog.array.map</span><span class="PUNC">(</span><span class="NAME">bareErrors</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">bareError</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='linenumber'>104</span> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">bidichecker.Error</span><span class="PUNC">(</span><span class="NAME">bareError</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'>105</span> </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'>106</span> </span><span class="NAME">bidichecker.runGui</span><span class="PUNC">(</span><span class="NAME">errors</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'>107</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='linenumber'>108</span> </span></pre></body></html>