How to use escapeText method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

EncodingUtilTest.js

Source: EncodingUtilTest.js Github

copy
1/*******************************************************************************
2 * Copyright (c) 2011 EclipseSource and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    EclipseSource - initial API and implementation
10 ******************************************************************************/
11
12qx.Class.define( "org.eclipse.rwt.test.tests.EncodingUtilTest", {
13
14  extend : qx.core.Object,
15  
16  members : {
17    
18    //////////////////////////////////////////////////////
19    // Tests ported from WidgetLCAUtil_Test#testEscapeTest
20    
21    testEscapeTextWithNull : function() {
22      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
23      try {
24        EncodingUtil.escapeText( null, true );
25        EncodingUtil.escapeText( null, true );
26        fail();
27      } catch( e ) {
28        // expected
29      }
30    },
31    
32    testEscapeTextNoChanges : function() {
33      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
34      assertEquals( "Test", EncodingUtil.escapeText( "Test", false ) );
35      assertEquals( "Test", EncodingUtil.escapeText( "Test", true ) );
36      assertEquals( "", EncodingUtil.escapeText( "", false ) );
37      assertEquals( "", EncodingUtil.escapeText( "", true ) );
38    },
39
40    testEscapeBrackets : function() {
41      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
42      assertEquals( "&lt;", EncodingUtil.escapeText( "<", false ) );
43      assertEquals( "&gt;", EncodingUtil.escapeText( ">", false ) );
44      assertEquals( "&lt;&lt;&lt;", EncodingUtil.escapeText( "<<<", false ) );
45      var expected = "&lt;File &gt;";
46      assertEquals( expected, EncodingUtil.escapeText( "<File >", false ) );
47      assertEquals( expected, EncodingUtil.escapeText( "<File >", true ) );
48    },
49
50    testEscapeAmps : function() {
51      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
52      assertEquals( "&amp;&amp;&amp;File", EncodingUtil.escapeText( "&&&File", false ) );
53    },
54
55    testEscapeMnemonics : function() {
56      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
57      assertEquals( "Open &amp; Close", EncodingUtil.escapeText( "Open && Close", true ) );
58      assertEquals( "E&lt;s&gt;ca'pe&quot; &amp; me",
59                    EncodingUtil.escapeText( "&E<s>ca'pe\" && me", true ) );
60      // Explicitly call it twice to check that _mnemonicFound is reset
61      assertEquals( "E&lt;s&gt;ca'pe&quot; &amp; me",
62                    EncodingUtil.escapeText( "&E<s>ca'pe\" && me", true ) );
63    },
64
65    testEscapeQuotes : function() {
66      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
67      assertEquals( "&quot;File&quot;", EncodingUtil.escapeText( "\"File\"", false ) );
68      assertEquals( "&quot;&quot;File", EncodingUtil.escapeText( "\"\"File", false ) );
69      assertEquals( "&quot;File&quot;", EncodingUtil.escapeText( "\"File\"", true ) );
70      assertEquals( "&quot;&quot;File", EncodingUtil.escapeText( "\"\"File", true ) );
71    },
72
73    testDontEscapeBackslash: function() {
74      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
75      assertEquals( "Test\\", EncodingUtil.escapeText( "Test\\", false ) );
76    },
77
78    testTruncateAtZero : function() {
79      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
80      assertEquals( String.fromCharCode( 0 ), "\000" );
81      assertEquals( "foo ", EncodingUtil.escapeText( "foo \000 bar", false ) );
82      assertEquals( "foo", EncodingUtil.escapeText( "foo\000", false ) );
83      assertEquals( "", EncodingUtil.escapeText( "\000foo", false ) );
84      assertEquals( "&lt;foo", EncodingUtil.escapeText( "<foo\000>", false ) );
85      assertEquals( "&lt;foo", EncodingUtil.escapeText( "<foo\000>", true ) );
86    },
87    
88    /////////////////////////////////////////////////////////
89    // Tests ported from EncodingUtilTest#testReplaceNewLines
90
91    testReplaceNewLines : function() {
92      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
93      var stringToReplace = "First line.\nSecond line.\nThird line.";
94      var expected = "First line.\\nSecond line.\\nThird line.";
95      assertEquals( expected, EncodingUtil.replaceNewLines( stringToReplace ) );
96    },
97
98    testReplaceCarriageReturns : function() {
99      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
100      var stringToReplace = "First line.\rSecond line.\rThird line.";
101      var expected = "First line.\\nSecond line.\\nThird line.";
102      assertEquals( expected, EncodingUtil.replaceNewLines( stringToReplace ) );
103    },
104
105    testReplaceDOSNewLines : function() {
106      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
107      var stringToReplace = "First line.\r\nSecond line.\r\nThird line.";
108      var expected = "First line.\\nSecond line.\\nThird line.";
109      assertEquals( expected, EncodingUtil.replaceNewLines( stringToReplace ) );
110    },
111
112    testReplaceNewLinesWithBlank : function() {
113      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
114      var stringToReplace = "First line.\r\nSecond line.\r\nThird line.";
115      var expected = "First line. Second line. Third line.";
116      assertEquals( expected, EncodingUtil.replaceNewLines( stringToReplace, " " ) );
117    },
118    
119    ////////////////////////////////////////////////////////////
120    // Tests ported from EncodingUtilTest#testReplaceWhiteSpaces
121
122    testDontReplaceSingleWhiteSpaces : function() {
123      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
124      var stringToEscape = "test1 test2";
125      var expected = "test1 test2";
126      assertEquals( expected, EncodingUtil.replaceWhiteSpaces( stringToEscape ) );
127    },
128
129    testReplaceDoubleWhiteSpaces : function() {
130      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
131      var stringToEscape = "test1  test2";
132      var expected = "test1&nbsp; test2";
133      assertEquals( expected, EncodingUtil.replaceWhiteSpaces( stringToEscape ) );
134    },
135
136    testReplaceMultipleWhiteSpaces : function() {
137      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
138      var stringToEscape = "test1   test2";
139      var expected = "test1&nbsp;&nbsp; test2";
140      assertEquals( expected, EncodingUtil.replaceWhiteSpaces( stringToEscape ) );
141    },
142
143    testReplaceStartingWhiteSpace : function() {
144      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
145      var stringToEscape = " test";
146      var expected = "&nbsp;test";
147      assertEquals( expected, EncodingUtil.replaceWhiteSpaces( stringToEscape ) );
148    },
149
150    testReplaceStartingAndEndingWhiteSpaces : function() {
151      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
152      var stringToEscape = "  test  ";
153      var expected = "&nbsp; test&nbsp;&nbsp;";
154      assertEquals( expected, EncodingUtil.replaceWhiteSpaces( stringToEscape ) );
155    },
156    
157    /////////////////////////////////////////////////////////////////////
158    // Tests ported from EncodingUtilTest#testEscapeLeadingTrailingSpaces
159    
160    testEscapeBothLeadingTrailingSpaces : function() {
161      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
162      var stringToEscape = "    All rights reserved.   ";
163      var expected = "&nbsp;&nbsp;&nbsp;&nbsp;All rights reserved.&nbsp;&nbsp;&nbsp;";
164      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
165    },
166
167    testEscapeOnlyTrailingSpaces : function() {
168      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
169      var stringToEscape = "All rights reserved. ";
170      var expected = "All rights reserved.&nbsp;";
171      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
172    },
173
174    testEscapeOnlyLeadingSpaces : function() {
175      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
176      var stringToEscape = "  All rights reserved.";
177      var expected = "&nbsp;&nbsp;All rights reserved.";
178      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
179    },
180
181    testEscapeNoLeadingTrailingSpaces : function() {
182      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
183      var stringToEscape = "All rights reserved.";
184      var expected = "All rights reserved.";
185      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
186    },
187
188    testEscapeLeadingTrailingSpacesWithNewLines : function() {
189      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
190      var stringToEscape = " \n  All rights reserved. \n ";
191      var expected = "&nbsp;\n  All rights reserved. \n&nbsp;";
192      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
193    },
194
195    testEscapeLeadingTrailingSpacesWithWhitespaceString : function() {
196      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
197      var stringToEscape = "  ";
198      var expected = "&nbsp;&nbsp;";
199      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
200    },
201
202    testEscapeLeadingTrailingSpacesWithEmptyString : function() {
203      var EncodingUtil = org.eclipse.rwt.protocol.EncodingUtil;
204      var stringToEscape = "";
205      var expected = "";
206      assertEquals( expected, EncodingUtil.escapeLeadingTrailingSpaces( stringToEscape ) );
207    }
208
209  }
210  
211} );
Full Screen

escapeText.js

Source: escapeText.js Github

copy
1import escapeText from '../escapeText';
2
3describe('escapeText', () => {
4  it('should escape boolean to string', function() {
5    expect(escapeText(true)).toBe('true');
6    expect(escapeText(false)).toBe('false');
7  });
8
9  it('should escape object to string', function() {
10    let escaped = escapeText({
11      toString: function() {
12        return 'ponys';
13      },
14    });
15
16    expect(escaped).toBe('ponys');
17  });
18
19  it('should escape number to string', function() {
20    expect(escapeText(42)).toBe('42');
21  });
22
23  it('should escape string', function() {
24    let escaped = escapeText('<script type=\'\' src=""></script>');
25    expect(escaped).not.toContain('<');
26    expect(escaped).not.toContain('>');
27    expect(escaped).not.toContain('\'');
28    expect(escaped).not.toContain('\"');
29
30    escaped = escapeText('&');
31    expect(escaped).toBe('&amp;');
32  });
33});
34
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)