"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/src/webdriverbackedselenium/Internal/SeleniumEmulation/SeleniumMutator.cs" (17 Feb 2023, 3040 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "SeleniumMutator.cs" see the
Fossies "Dox" file reference documentation.
1 // <copyright file="SeleniumMutator.cs" company="WebDriver Committers">
2 // Licensed to the Software Freedom Conservancy (SFC) under one
3 // or more contributor license agreements. See the NOTICE file
4 // distributed with this work for additional information
5 // regarding copyright ownership. The SFC licenses this file
6 // to you under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // </copyright>
18
19 using System.Globalization;
20 using System.Text;
21 using System.Text.RegularExpressions;
22
23 namespace Selenium.Internal.SeleniumEmulation
24 {
25 /// <summary>
26 /// A mechanism for taking a script meant for Selenium Core
27 /// and converting to something that webdriver can evaluate.
28 /// </summary>
29 public class SeleniumMutator : IScriptMutator
30 {
31 private Regex pattern;
32 private string method;
33 private string atom;
34
35 /// <summary>
36 /// Initializes a new instance of the <see cref="SeleniumMutator"/> class.
37 /// </summary>
38 /// <param name="method">The name of the atom to mutate.</param>
39 /// <param name="atom">The source code of the atom to execute.</param>
40 public SeleniumMutator(string method, string atom)
41 {
42 if (method == null)
43 {
44 method = string.Empty;
45 }
46
47 string raw = ".*" + method.Replace(".", "\\s*\\.\\s*") + ".*";
48 this.pattern = new Regex(raw);
49 this.method = method;
50 this.atom = atom;
51 }
52
53 #region IScriptMutator Members
54 /// <summary>
55 /// Mutate an atom script so that it has the correct scope.
56 /// The original, unmodified script is used to generate a script
57 /// on the StringBuilder, the "ToString" method of which should be
58 /// used to get the result. We make use of a StringBuilder rather than a
59 /// normal String so that we can efficiently chain mutators.
60 /// </summary>
61 /// <param name="script">The original script.</param>
62 /// <param name="outputTo">The mutated script.</param>
63 public void Mutate(string script, StringBuilder outputTo)
64 {
65 if (outputTo == null || !this.pattern.IsMatch(script))
66 {
67 return;
68 }
69
70 // Alias the raw atom and set "this" to be the pre-declared selenium object.
71 outputTo.Append(string.Format(CultureInfo.InvariantCulture, @"{0} = function() {{ return ({1}).apply(null, arguments);}};", this.method, this.atom));
72 }
73
74 #endregion
75 }
76 }