Best Python code snippet using slash
json_test.js
Source:json_test.js  
...36  assertSerialize('"\\u001f"', '\x1f');37  assertSerialize('"\\u20ac"', '\u20AC');38  assertSerialize('"\\ud83d\\ud83d"', '\ud83d\ud83d');39  var str = allChars(0, 10000);40  assertEquals(str, eval(goog.json.serialize(str)));41}42function testNullSerialize() {43  assertSerialize('null', null);44  assertSerialize('null', undefined);45  assertSerialize('null', NaN);46  assertSerialize('0', 0);47  assertSerialize('""', '');48  assertSerialize('false', false);49}50function testNullPropertySerialize() {51  assertSerialize('{"a":null}', {'a': null});52  assertSerialize('{"a":null}', {'a': undefined});53}54function testNumberSerialize() {55  assertSerialize('0', 0);56  assertSerialize('12345', 12345);57  assertSerialize('-12345', -12345);58  assertSerialize('0.1', 0.1);59  // the leading zero may not be omitted60  assertSerialize('0.1', .1);61  // no leading +62  assertSerialize('1', +1);63  // either format is OK64  var s = goog.json.serialize(1e50);65  assertTrue(66      '1e50', s == '1e50' || s == '1E50' || s == '1e+50' || s == '1E+50');67  // either format is OK68  s = goog.json.serialize(1e-50);69  assertTrue('1e50', s == '1e-50' || s == '1E-50');70  // These numbers cannot be represented in JSON71  assertSerialize('null', NaN);72  assertSerialize('null', Infinity);73  assertSerialize('null', -Infinity);74}75function testBooleanSerialize() {76  assertSerialize('true', true);77  assertSerialize('"true"', 'true');78  assertSerialize('false', false);79  assertSerialize('"false"', 'false');80}81function testArraySerialize() {82  assertSerialize('[]', []);83  assertSerialize('[1]', [1]);84  assertSerialize('[1,2]', [1, 2]);85  assertSerialize('[1,2,3]', [1, 2, 3]);86  assertSerialize('[[]]', [[]]);87  assertSerialize('[null,null]', [function() {}, function() {}]);88  assertNotEquals('{length:0}', goog.json.serialize({length: 0}), '[]');89}90function testFunctionSerialize() {91  assertSerialize('null', function() {});92}93function testObjectSerialize_emptyObject() {94  assertSerialize('{}', {});95}96function testObjectSerialize_oneItem() {97  assertSerialize('{"a":"b"}', {a: 'b'});98}99function testObjectSerialize_twoItems() {100  assertEquals(101      '{"a":"b","c":"d"}', goog.json.serialize({a: 'b', c: 'd'}),102      '{"a":"b","c":"d"}');103}104function testObjectSerialize_whitespace() {105  assertSerialize('{" ":" "}', {' ': ' '});106}107function testSerializeSkipFunction() {108  var object =109      {s: 'string value', b: true, i: 100, f: function() { var x = 'x'; }};110  assertSerialize('null', object.f);111  assertSerialize('{"s":"string value","b":true,"i":100}', object);112}113function testObjectSerialize_array() {114  assertNotEquals('[0,1]', goog.json.serialize([0, 1]), '{"0":"0","1":"1"}');115}116function testObjectSerialize_recursion() {117  if (goog.userAgent.WEBKIT) {118    return;  // this makes safari 4 crash.119  }120  var anObject = {};121  anObject.thisObject = anObject;122  assertThrows('expected recursion exception', function() {123    goog.json.serialize(anObject);124  });125}126function testObjectSerializeWithHasOwnProperty() {127  var object = {'hasOwnProperty': null};128  if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {129    assertEquals('{}', goog.json.serialize(object));130  } else {131    assertEquals('{"hasOwnProperty":null}', goog.json.serialize(object));132  }133}134function testWrappedObjects() {135  assertSerialize('"foo"', new String('foo'));136  assertSerialize('42', new Number(42));137  assertSerialize('null', new Number('a NaN'));138  assertSerialize('true', new Boolean(true));139}140// parsing141function testStringParse() {142  assertEquals('Empty string', goog.json.parse('""'), '');143  assertEquals('whitespace string', goog.json.parse('" "'), ' ');144  // unicode without the control characters 0x00 - 0x1f, 0x7f - 0x9f145  var str = allChars(32, 1000);146  var jsonString = goog.json.serialize(str);147  var a = eval(jsonString);148  assertEquals('unicode string', goog.json.parse(jsonString), a);149  assertEquals('true as a string', goog.json.parse('"true"'), 'true');150  assertEquals('false as a string', goog.json.parse('"false"'), 'false');151  assertEquals('null as a string', goog.json.parse('"null"'), 'null');152  assertEquals('number as a string', goog.json.parse('"0"'), '0');153}154function testNullParse() {155  assertEquals('null', goog.json.parse(null), null);156  assertEquals('null', goog.json.parse('null'), null);157  assertNotEquals('0', goog.json.parse('0'), null);158  assertNotEquals('""', goog.json.parse('""'), null);159  assertNotEquals('false', goog.json.parse('false'), null);160}161function testNumberParse() {162  assertEquals('0', goog.json.parse('0'), 0);163  assertEquals('12345', goog.json.parse('12345'), 12345);164  assertEquals('-12345', goog.json.parse('-12345'), -12345);165  assertEquals('0.1', goog.json.parse('0.1'), 0.1);166  // either format is OK167  assertEquals(1e50, goog.json.parse('1e50'));168  assertEquals(1e50, goog.json.parse('1E50'));169  assertEquals(1e50, goog.json.parse('1e+50'));170  assertEquals(1e50, goog.json.parse('1E+50'));171  // either format is OK172  assertEquals(1e-50, goog.json.parse('1e-50'));173  assertEquals(1e-50, goog.json.parse('1E-50'));174}175function testBooleanParse() {176  assertEquals('true', goog.json.parse('true'), true);177  assertEquals('false', goog.json.parse('false'), false);178  assertNotEquals('0', goog.json.parse('0'), false);179  assertNotEquals('"false"', goog.json.parse('"false"'), false);180  assertNotEquals('null', goog.json.parse('null'), false);181  assertNotEquals('1', goog.json.parse('1'), true);182  assertNotEquals('"true"', goog.json.parse('"true"'), true);183  assertNotEquals('{}', goog.json.parse('{}'), true);184  assertNotEquals('[]', goog.json.parse('[]'), true);185}186function testArrayParse() {187  assertArrayEquals([], goog.json.parse('[]'));188  assertArrayEquals([1], goog.json.parse('[1]'));189  assertArrayEquals([1, 2], goog.json.parse('[1,2]'));190  assertArrayEquals([1, 2, 3], goog.json.parse('[1,2,3]'));191  assertArrayEquals([[]], goog.json.parse('[[]]'));192  // Note that array-holes are not valid json. However, goog.json.parse193  // supports them so that clients can reap the security benefits of194  // goog.json.parse even if they are using this non-standard format.195  assertArrayEquals([1, /* hole */, 3], goog.json.parse('[1,,3]'));196  // make sure we do not get an array for something that looks like an array197  assertFalse('{length:0}', 'push' in goog.json.parse('{"length":0}'));198}199function testObjectParse() {200  function objectEquals(a1, a2) {201    for (var key in a1) {202      if (a1[key] != a2[key]) {203        return false;204      }205    }206    return true;207  }208  assertTrue('{}', objectEquals(goog.json.parse('{}'), {}));209  assertTrue('{"a":"b"}', objectEquals(goog.json.parse('{"a":"b"}'), {a: 'b'}));210  assertTrue(211      '{"a":"b","c":"d"}',212      objectEquals(goog.json.parse('{"a":"b","c":"d"}'), {a: 'b', c: 'd'}));213  assertTrue(214      '{" ":" "}', objectEquals(goog.json.parse('{" ":" "}'), {' ': ' '}));215  // make sure we do not get an Object when it is really an array216  assertTrue('[0,1]', 'length' in goog.json.parse('[0,1]'));217}218function testForValidJson() {219  function error_(msg, s) {220    assertThrows(221        msg + ', Should have raised an exception: ' + s,222        goog.partial(goog.json.parse, s));223  }224  error_('Non closed string', '"dasdas');225  error_('undefined is not valid json', 'undefined');226  // These numbers cannot be represented in JSON227  error_('NaN cannot be presented in JSON', 'NaN');228  error_('Infinity cannot be presented in JSON', 'Infinity');229  error_('-Infinity cannot be presented in JSON', '-Infinity');230}231function testIsNotValid() {232  assertFalse(goog.json.isValid('t'));233  assertFalse(goog.json.isValid('r'));234  assertFalse(goog.json.isValid('u'));235  assertFalse(goog.json.isValid('e'));236  assertFalse(goog.json.isValid('f'));237  assertFalse(goog.json.isValid('a'));238  assertFalse(goog.json.isValid('l'));239  assertFalse(goog.json.isValid('s'));240  assertFalse(goog.json.isValid('n'));241  assertFalse(goog.json.isValid('E'));242  assertFalse(goog.json.isValid('+'));243  assertFalse(goog.json.isValid('-'));244  assertFalse(goog.json.isValid('t++'));245  assertFalse(goog.json.isValid('++t'));246  assertFalse(goog.json.isValid('t--'));247  assertFalse(goog.json.isValid('--t'));248  assertFalse(goog.json.isValid('-t'));249  assertFalse(goog.json.isValid('+t'));250  assertFalse(goog.json.isValid('"\\"'));  // "\"251  assertFalse(goog.json.isValid('"\\'));   // "\252  // multiline string using \ at the end is not valid253  assertFalse(goog.json.isValid('"a\\\nb"'));254  assertFalse(goog.json.isValid('"\n"'));255  assertFalse(goog.json.isValid('"\r"'));256  assertFalse(goog.json.isValid('"\r\n"'));257  // Disallow the unicode newlines258  assertFalse(goog.json.isValid('"\u2028"'));259  assertFalse(goog.json.isValid('"\u2029"'));260  assertFalse(goog.json.isValid(' '));261  assertFalse(goog.json.isValid('\n'));262  assertFalse(goog.json.isValid('\r'));263  assertFalse(goog.json.isValid('\r\n'));264  assertFalse(goog.json.isValid('t.r'));265  assertFalse(goog.json.isValid('1e'));266  assertFalse(goog.json.isValid('1e-'));267  assertFalse(goog.json.isValid('1e+'));268  assertFalse(goog.json.isValid('1e-'));269  assertFalse(goog.json.isValid('"\\\u200D\\"'));270  assertFalse(goog.json.isValid('"\\\0\\"'));271  assertFalse(goog.json.isValid('"\\\0"'));272  assertFalse(goog.json.isValid('"\\0"'));273  assertFalse(goog.json.isValid('"\x0c"'));274  assertFalse(goog.json.isValid('"\\\u200D\\", alert(\'foo\') //"\n'));275  // Disallow referencing variables with names built up from primitives276  assertFalse(goog.json.isValid('truefalse'));277  assertFalse(goog.json.isValid('null0'));278  assertFalse(goog.json.isValid('null0.null0'));279  assertFalse(goog.json.isValid('[truefalse]'));280  assertFalse(goog.json.isValid('{"a": null0}'));281  assertFalse(goog.json.isValid('{"a": null0, "b": 1}'));282}283function testIsValid() {284  assertTrue(goog.json.isValid('\n""\n'));285  assertTrue(goog.json.isValid('[1\n,2\r,3\u2028\n,4\u2029]'));286  assertTrue(goog.json.isValid('"\x7f"'));287  assertTrue(goog.json.isValid('"\x09"'));288  // Test tab characters in json.289  assertTrue(goog.json.isValid('{"\t":"\t"}'));290}291function testDoNotSerializeProto() {292  function F() {}293  F.prototype = {c: 3};294  var obj = new F;295  obj.a = 1;296  obj.b = 2;297  assertEquals(298      'Should not follow the prototype chain', '{"a":1,"b":2}',299      goog.json.serialize(obj));300}301function testEscape() {302  var unescaped = '1a*/]';303  assertEquals(304      'Should not escape', '"' + unescaped + '"',305      goog.json.serialize(unescaped));306  var escaped = '\n\x7f\u1049';307  assertEquals(308      'Should escape', '',309      findCommonChar(escaped, goog.json.serialize(escaped)));310  assertEquals(311      'Should eval to the same string after escaping', escaped,312      goog.json.parse(goog.json.serialize(escaped)));313}314function testReplacer() {315  assertSerialize('[null,null,0]', [, , 0]);316  assertSerialize('[0,0,{"x":0}]', [, , {x: 0}], function(k, v) {317    if (v === undefined && goog.isArray(this)) {318      return 0;319    }320    return v;321  });322  assertSerialize('[0,1,2,3]', [0, 0, 0, 0], function(k, v) {323    var kNum = Number(k);324    if (k && !isNaN(kNum)) {325      return kNum;326    }327    return v;328  });329  var f = function(k, v) { return typeof v == 'number' ? v + 1 : v; };330  assertSerialize('{"a":1,"b":{"c":2}}', {'a': 0, 'b': {'c': 1}}, f);331}332function testDateSerialize() {333  assertSerialize('{}', new Date(0));334}335function testToJSONSerialize() {336  assertSerialize('{}', {toJSON: goog.functions.constant('serialized')});337  assertSerialize('{"toJSON":"normal"}', {toJSON: 'normal'});338}339function testTryNativeJson() {340  goog.json.TRY_NATIVE_JSON = true;341  var error;342  goog.json.setErrorLogger(function(message, ex) {343    error = message;344  });345  error = undefined;346  goog.json.parse('{"a":[,1]}');347  assertEquals('Invalid JSON: {"a":[,1]}', error);348  goog.json.TRY_NATIVE_JSON = false;349  goog.json.setErrorLogger(goog.nullFunction);350}351/**352 * Asserts that the given object serializes to the given value.353 * If the current browser has an implementation of JSON.serialize,354 * we make sure our version matches that one.355 */356function assertSerialize(expected, obj, opt_replacer) {357  assertEquals(expected, goog.json.serialize(obj, opt_replacer));358  // goog.json.serialize escapes non-ASCI characters while JSON.stringify359  // doesnât.  This is expected so do not compare the results.360  if (typeof obj == 'string' && obj.charCodeAt(0) > 0x7f) return;361  // I'm pretty sure that the goog.json.serialize behavior is correct by the ES5362  // spec, but JSON.stringify(undefined) is undefined on all browsers.363  if (obj === undefined) return;364  // Browsers don't serialize undefined properties, but goog.json.serialize does365  if (goog.isObject(obj) && ('a' in obj) && obj['a'] === undefined) return;366  // Replacers are broken on IE and older versions of firefox.367  if (opt_replacer && !goog.userAgent.WEBKIT) return;368  // goog.json.serialize does not stringify dates the same way.369  if (obj instanceof Date) return;370  // goog.json.serialize does not stringify functions the same way.371  if (obj instanceof Function) return;...test.js
Source:test.js  
...11      node = null;12    }13  });14  it('should return an empty string on invalid input', function () {15    assert.strictEqual('', serialize(null));16  });17  it('should serialize a SPAN element', function () {18    node = document.createElement('span');19    assert.equal('<span></span>', serialize(node));20  });21  it('should serialize a BR element', function () {22    node = document.createElement('br');23    assert.equal('<br>', serialize(node));24  });25  it('should serialize a text node', function () {26    node = document.createTextNode('test');27    assert.equal('test', serialize(node));28  });29  it('should serialize a text node with special HTML characters', function () {30    node = document.createTextNode('<>\'"&');31    assert.equal('<>\'"&', serialize(node));32  });33  it('should serialize a DIV element with child nodes', function () {34    node = document.createElement('div');35    node.appendChild(document.createTextNode('hello '));36    var b = document.createElement('b');37    b.appendChild(document.createTextNode('world'));38    node.appendChild(b);39    node.appendChild(document.createTextNode('!'));40    node.appendChild(document.createElement('br'));41    assert.equal('<div>hello <b>world</b>!<br></div>', serialize(node));42  });43  it('should serialize a DIV element with attributes', function () {44    node = document.createElement('div');45    node.setAttribute('foo', 'bar');46    node.setAttribute('escape', '<>&"\'');47    assert.equal('<div foo="bar" escape="<>&"'"></div>', serialize(node));48  });49  it('should serialize an Attribute node', function () {50    var div = document.createElement('div');51    div.setAttribute('foo', 'bar');52    node = div.attributes[0];53    assert.equal('foo="bar"', serialize(node));54  });55  it('should serialize a Comment node', function () {56    node = document.createComment('test');57    assert.equal('<!--test-->', serialize(node));58  });59  it('should serialize a Document node', function () {60    node = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);61    assert.equal('<html></html>', serialize(node));62  });63  it('should serialize a Doctype node', function () {64    node = document.implementation.createDocumentType(65      'html',66      '-//W3C//DTD XHTML 1.0 Strict//EN',67      'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'68    );69    // Some older browsers require the DOCTYPE to be within a Document,70    // otherwise the "serialize" custom event is considered "cancelled".71    // See: https://travis-ci.org/webmodules/dom-serialize/builds/4730774972    var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', node);73    assert.equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', serialize(node));74    assert.equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html></html>', serialize(doc));75  });76  it('should serialize a Doctype node with systemId', function () {77    node = document.implementation.createDocumentType(78      'root-element',79      '',80      'http://www.w3.org/1999/xhtml'81    );82    document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'root-element', node);83    assert.equal('<!DOCTYPE root-element SYSTEM "http://www.w3.org/1999/xhtml">', serialize(node));84  });85  it('should serialize an HTML5 Doctype node', function () {86    node = document.implementation.createDocumentType(87      'html',88      '',89      ''90    );91    document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'node', node);92    assert.equal('<!DOCTYPE html>', serialize(node));93  });94  it('should serialize a DocumentFragment node', function () {95    node = document.createDocumentFragment();96    node.appendChild(document.createElement('b'));97    node.appendChild(document.createElement('i'));98    node.lastChild.appendChild(document.createTextNode('foo'));99    assert.equal('<b></b><i>foo</i>', serialize(node));100  });101  it('should serialize an Array of nodes', function () {102    var array = [];103    array.push(document.createTextNode('foo'));104    array.push(document.createElement('div'));105    array[1].appendChild(document.createTextNode('bar'));106    assert.equal('foo<div>bar</div>', serialize(array));107  });108  describe('serializeText()', function () {109    it('should serialize an Attribute node', function () {110      var d = document.createElement('div');111      d.setAttribute('foo', '<>"&');112      assert.equal('foo="<>"&"', serialize.serializeAttribute(d.attributes[0]));113    });114    it('should allow an "options" object to be passed in', function () {115      var d = document.createElement('div');116      d.setAttribute('foo', '<>"&');117      assert.equal('foo="<>"&"', serialize.serializeAttribute(d.attributes[0], { named: false }));118    });119  });120  describe('serializeText()', function () {121    it('should serialize a TextNode instance', function () {122      node = document.createTextNode('<b>&');123      assert.equal('<b>&', serialize.serializeText(node));124    });125    it('should allow an "options" object to be passed in', function () {126      node = document.createTextNode('<b>&');127      assert.equal('<b>&', serialize.serializeText(node, { named: false }));128    });129  });130  describe('"serialize" event', function () {131    it('should emit a "serialize" event on a DIV node', function () {132      node = document.createElement('div');133      var count = 0;134      node.addEventListener('serialize', function (e) {135        count++;136        e.detail.serialize = 'MEOW';137      });138      assert.equal(0, count);139      assert.equal('MEOW', serialize(node));140      assert.equal(1, count);141    });142    it('should emit a "serialize" event on a Text node', function () {143      node = document.createTextNode('whaaaaa!!!!!!');144      var count = 0;145      node.addEventListener('serialize', function (e) {146        count++;147        e.detail.serialize = 'MEOW';148      });149      assert.equal(0, count);150      assert.equal('MEOW', serialize(node));151      assert.equal(1, count);152    });153    it('should output an empty string when event is cancelled', function () {154      node = document.createElement('div');155      node.appendChild(document.createTextNode('!'));156      var count = 0;157      node.firstChild.addEventListener('serialize', function (e) {158        count++;159        e.preventDefault();160      });161      assert.equal(0, count);162      assert.equal('<div></div>', serialize(node));163      assert.equal(1, count);164    });165    it('should render a Number when set as `e.detail.serialize`', function () {166      node = document.createTextNode('whaaaaa!!!!!!');167      var count = 0;168      node.addEventListener('serialize', function (e) {169        count++;170        e.detail.serialize = 123;171      });172      assert.equal(0, count);173      assert.equal('123', serialize(node));174      assert.equal(1, count);175    });176    it('should render a Node when set as `e.detail.serialize`', function () {177      node = document.createTextNode('whaaaaa!!!!!!');178      var count = 0;179      node.addEventListener('serialize', function (e) {180        count++;181        if (count === 1) {182          e.detail.serialize = document.createTextNode('foo');183        }184      });185      assert.equal(0, count);186      assert.equal('foo', serialize(node));187      assert.equal(2, count);188    });189    it('should render a Node when set as `e.detail.serialize` and event is cancelled', function () {190      node = document.createTextNode('whaaaaa!!!!!!');191      var count = 0;192      node.addEventListener('serialize', function (e) {193        count++;194        if (count === 1) {195          e.preventDefault();196          e.detail.serialize = document.createTextNode('foo');197        }198      });199      assert.equal(0, count);200      assert.equal('foo', serialize(node));201      assert.equal(2, count);202    });203    it('should have `context` set on the event', function () {204      node = document.createTextNode('');205      var count = 0;206      node.addEventListener('serialize', function (e) {207        count++;208        e.detail.serialize = e.detail.context;209      });210      assert.equal(0, count);211      assert.equal('foo', serialize(node, 'foo'));212      assert.equal(1, count);213      assert.equal('bar', serialize(node, 'bar'));214      assert.equal(2, count);215      assert.equal('baz', serialize(node, 'baz'));216      assert.equal(3, count);217    });218    it('should bubble', function () {219      node = document.createElement('div');220      node.appendChild(document.createTextNode('foo'));221      node.appendChild(document.createTextNode(' '));222      node.appendChild(document.createTextNode('bar'));223      // `node` must be inside the DOM for the "serialize" event to bubble224      document.body.appendChild(node);225      var count = 0;226      node.addEventListener('serialize', function (e) {227        count++;228        assert.equal('foo', e.detail.context);229        if (e.serializeTarget === node)230          return;231        else if (e.serializeTarget.nodeValue === 'foo')232          e.detail.serialize = 'â¦';233        else234          e.preventDefault();235      }, false);236      assert.equal(0, count);237      assert.equal('<div>â¦</div>', serialize(node, 'foo'));238      assert.equal(4, count);239    });240    it('should support one-time callback function on Elements', function () {241      node = document.createElement('div');242      var count = 0;243      function callback (e) {244        count++;245        e.detail.serialize = count;246      }247      assert.equal(0, count);248      assert.equal('1', serialize(node, callback));249      assert.equal(1, count);250      assert.equal('<div></div>', serialize(node));251      assert.equal(1, count);252    });253    it('should support one-time callback function on NodeLists', function () {254      node = document.createElement('div');255      node.appendChild(document.createElement('strong'));256      node.appendChild(document.createTextNode('foo'));257      node.appendChild(document.createElement('em'));258      node.appendChild(document.createTextNode('bar'));259      var count = 0;260      function callback (e) {261        count++;262        e.detail.serialize = count;263      }264      assert.equal(0, count);265      assert.equal('1234', serialize(node.childNodes, callback));266      assert.equal(4, count);267      assert.equal('<strong></strong>foo<em></em>bar', serialize(node.childNodes));268      assert.equal(4, count);269    });270    it('should support one-time callback function on Nodes set in `e.detail.serialize`', function () {271      node = document.createElement('div');272      node.appendChild(document.createTextNode('foo'));273      // `node` must be inside the DOM for the "serialize" event to bubble274      document.body.appendChild(node);275      var count = 0;276      function callback (e) {277        count++;278        if (2 === count) {279          assert.equal('foo', e.serializeTarget.nodeValue);280          var text = document.createTextNode('bar');281          e.detail.serialize = text;282        } else if (3 === count) {283          assert.equal('bar', e.serializeTarget.nodeValue);284          var text = document.createTextNode('baz');285          e.detail.serialize = text;286        }287      }288      assert.equal(0, count);289      assert.equal('<div>baz</div>', serialize(node, callback));290      assert.equal(4, count);291    });292    it('should support one-time callback function on complex Nodes set in `e.detail.serialize`', function () {293      node = document.createElement('div');294      node.appendChild(document.createTextNode('foo'));295      // `node` must be inside the DOM for the "serialize" event to bubble296      document.body.appendChild(node);297      var count = 0;298      function callback (e) {299        count++;300        if (e.serializeTarget.nodeValue === 'foo') {301          var el = document.createElement('p');302          el.appendChild(document.createTextNode('x '));303          el.appendChild(document.createElement('i'));304          el.lastChild.appendChild(document.createTextNode('bar'));305          e.detail.serialize = el;306        }307      }308      assert.equal(0, count);309      assert.equal('<div><p>x <i>bar</i></p></div>', serialize(node, callback));310      assert.equal(6, count);311    });312  });...StylesTest.js
Source:StylesTest.js  
...11    var failure = arguments[arguments.length - 1];12    var suite = LegacyUnit.createSuite();13    suite.test('Basic parsing/serializing', function () {14      var styles = new Styles();15      LegacyUnit.equal(styles.serialize(styles.parse('FONT-SIZE:10px')), "font-size: 10px;");16      LegacyUnit.equal(styles.serialize(styles.parse('FONT-SIZE:10px;COLOR:red')), "font-size: 10px; color: red;");17      LegacyUnit.equal(styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  red   ')), "font-size: 10px; color: red;");18      LegacyUnit.equal(styles.serialize(styles.parse('key:"value"')), "key: 'value';");19      LegacyUnit.equal(styles.serialize(styles.parse('key:"value1" \'value2\'')), "key: 'value1' 'value2';");20      LegacyUnit.equal(styles.serialize(styles.parse('key:"val\\"ue1" \'val\\\'ue2\'')), "key: 'val\"ue1' 'val\\'ue2';");21      LegacyUnit.equal(styles.serialize(styles.parse('width:100%')), 'width: 100%;');22      LegacyUnit.equal(styles.serialize(styles.parse('value:_; value2:"_"')), 'value: _; value2: \'_\';');23      LegacyUnit.equal(styles.serialize(styles.parse('value: "&"')), "value: '&';");24      LegacyUnit.equal(styles.serialize(styles.parse('value: "&"')), "value: '&';");25      LegacyUnit.equal(styles.serialize(styles.parse('value: ')), "");26      LegacyUnit.equal(27        styles.serialize(styles.parse("background: url('http://www.site.com/(foo)');")),28        "background: url('http://www.site.com/(foo)');"29      );30    });31    suite.test('Colors force hex and lowercase', function () {32      var styles = new Styles();33      LegacyUnit.equal(styles.serialize(styles.parse('color: rgb(1,2,3)')), "color: #010203;");34      LegacyUnit.equal(styles.serialize(styles.parse('color: RGB(1,2,3)')), "color: #010203;");35      LegacyUnit.equal(styles.serialize(styles.parse('color: #FF0000')), "color: #ff0000;");36      LegacyUnit.equal(styles.serialize(styles.parse('  color:   RGB  (  1  ,  2  ,  3  )  ')), "color: #010203;");37      LegacyUnit.equal(38        styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  RGB  (  1  ,  2  ,  3  )   ')),39        "font-size: 10px; color: #010203;"40      );41      LegacyUnit.equal(styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  RED   ')), "font-size: 10px; color: red;");42    });43    suite.test('Urls convert urls and force format', function () {44      var styles = new Styles({45        url_converter: function (url) {46          return '|' + url + '|';47        }48      });49      LegacyUnit.equal(styles.serialize(styles.parse('background: url(a)')), "background: url('|a|');");50      LegacyUnit.equal(styles.serialize(styles.parse('background: url("a")')), "background: url('|a|');");51      LegacyUnit.equal(styles.serialize(styles.parse("background: url('a')")), "background: url('|a|');");52      LegacyUnit.equal(styles.serialize(styles.parse('background: url(   a   )')), "background: url('|a|');");53      LegacyUnit.equal(styles.serialize(styles.parse('background: url(   "a"   )')), "background: url('|a|');");54      LegacyUnit.equal(styles.serialize(styles.parse("background: url(    'a'    )")), "background: url('|a|');");55      LegacyUnit.equal(56        styles.serialize(styles.parse('background1: url(a); background2: url("a"); background3: url(\'a\')')),57        "background1: url('|a|'); background2: url('|a|'); background3: url('|a|');"58      );59      LegacyUnit.equal(60        styles.serialize(styles.parse("background: url('http://www.site.com/a?a=b&c=d')")),61        "background: url('|http://www.site.com/a?a=b&c=d|');"62      );63      LegacyUnit.equal(64        styles.serialize(styles.parse("background: url('http://www.site.com/a_190x144.jpg');")),65        "background: url('|http://www.site.com/a_190x144.jpg|');"66      );67    });68    suite.test('Compress styles', function () {69      var styles = new Styles();70      LegacyUnit.equal(71        styles.serialize(72          styles.parse('border-top: 1px solid red; border-left: 1px solid red; border-bottom: 1px solid red; border-right: 1px solid red;')73        ),74        'border: 1px solid red;'75      );76      LegacyUnit.equal(77        styles.serialize(78          styles.parse('border-width: 1pt 1pt 1pt 1pt; border-style: none none none none; border-color: black black black black;')79        ),80        'border: 1pt none black;'81      );82      LegacyUnit.equal(83        styles.serialize(84          styles.parse('border-width: 1pt 4pt 2pt 3pt; border-style: solid dashed dotted none; border-color: black red green blue;')85        ),86        'border-width: 1pt 4pt 2pt 3pt; border-style: solid dashed dotted none; border-color: black red green blue;'87      );88      LegacyUnit.equal(89        styles.serialize(90          styles.parse('border-top: 1px solid red; border-left: 1px solid red; border-right: 1px solid red; border-bottom: 1px solid red')91        ),92        'border: 1px solid red;'93      );94      LegacyUnit.equal(95        styles.serialize(96          styles.parse('border-top: 1px solid red; border-right: 2px solid red; border-bottom: 3px solid red; border-left: 4px solid red')97        ),98        'border-top: 1px solid red; border-right: 2px solid red; border-bottom: 3px solid red; border-left: 4px solid red;'99      );100      LegacyUnit.equal(101        styles.serialize(102          styles.parse('padding-top: 1px; padding-right: 2px; padding-bottom: 3px; padding-left: 4px')103        ),104        'padding: 1px 2px 3px 4px;'105      );106      LegacyUnit.equal(107        styles.serialize(styles.parse('margin-top: 1px; margin-right: 2px; margin-bottom: 3px; margin-left: 4px')),108        'margin: 1px 2px 3px 4px;'109      );110      LegacyUnit.equal(111        styles.serialize(styles.parse('margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: 2px')),112        'margin: 1px 1px 1px 2px;'113      );114      LegacyUnit.equal(115        styles.serialize(styles.parse('margin-top: 2px; margin-right: 1px; margin-bottom: 1px; margin-left: 1px')),116        'margin: 2px 1px 1px 1px;'117      );118      LegacyUnit.equal(119        styles.serialize(120          styles.parse('border-top-color: red; border-right-color: green; border-bottom-color: blue; border-left-color: yellow')121        ),122        'border-color: red green blue yellow;'123      );124      LegacyUnit.equal(125        styles.serialize(styles.parse('border-width: 1px; border-style: solid; border-color: red')),126        'border: 1px solid red;'127      );128      LegacyUnit.equal(129        styles.serialize(styles.parse('border-width: 1px; border-color: red')),130        'border-width: 1px; border-color: red;'131      );132    });133    suite.test('Font weight', function () {134      var styles = new Styles();135      LegacyUnit.equal(styles.serialize(styles.parse('font-weight: 700')), "font-weight: bold;");136    });137    suite.test('Valid styles', function () {138      var styles = new Styles({}, new Schema({ valid_styles: { '*': 'color,font-size', 'a': 'margin-left' } }));139      LegacyUnit.equal(140        styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; invalid: 1;'), 'b'),141        "color: #ff0000; font-size: 10px;"142      );143      LegacyUnit.equal(144        styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; invalid: 2;'), 'a'),145        "color: #ff0000; font-size: 10px; margin-left: 10px;"146      );147    });148    suite.test('Invalid styles', function () {149      var styles = new Styles({}, new Schema({ invalid_styles: { '*': 'color,font-size', 'a': 'margin-left' } }));150      LegacyUnit.equal(styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px'), 'b'), "margin-left: 10px;");151      LegacyUnit.equal(152        styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; margin-right: 10px;'), 'a'),153        "margin-right: 10px;"154      );155    });156    suite.test('Suspicious (XSS) property names', function () {157      var styles = new Styles();158      LegacyUnit.equal(styles.serialize(styles.parse('font-fa"on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");159      LegacyUnit.equal(styles.serialize(styles.parse('font-fa\\"on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");160      LegacyUnit.equal(styles.serialize(styles.parse('font-fa\\22on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");161    });162    suite.test('Script urls denied', function () {163      var styles = new Styles();164      LegacyUnit.equal(styles.serialize(styles.parse('behavior:url(test.htc)')), "");165      LegacyUnit.equal(styles.serialize(styles.parse('b\\65havior:url(test.htc)')), "");166      LegacyUnit.equal(styles.serialize(styles.parse('color:expression(alert(1))')), "");167      LegacyUnit.equal(styles.serialize(styles.parse('color:\\65xpression(alert(1))')), "");168      LegacyUnit.equal(styles.serialize(styles.parse('color:exp/**/ression(alert(1))')), "");169      LegacyUnit.equal(styles.serialize(styles.parse('color:/**/')), "");170      LegacyUnit.equal(styles.serialize(styles.parse('color:  expression  (  alert(1))')), "");171      LegacyUnit.equal(styles.serialize(styles.parse('background:url(jAvaScript:alert(1)')), "");172      LegacyUnit.equal(styles.serialize(styles.parse('background:url(javascript:alert(1)')), "");173      LegacyUnit.equal(styles.serialize(styles.parse('background:url(j\\61vascript:alert(1)')), "");174      LegacyUnit.equal(styles.serialize(styles.parse('background:url(\\6a\\61vascript:alert(1)')), "");175      LegacyUnit.equal(styles.serialize(styles.parse('background:url(\\6A\\61vascript:alert(1)')), "");176      LegacyUnit.equal(styles.serialize(styles.parse('background:url\\28\\6A\\61vascript:alert(1)')), "");177      LegacyUnit.equal(styles.serialize(styles.parse('background:\\75rl(j\\61vascript:alert(1)')), "");178      LegacyUnit.equal(styles.serialize(styles.parse('b\\61ckground:\\75rl(j\\61vascript:alert(1)')), "");179      LegacyUnit.equal(styles.serialize(styles.parse('background:url(vbscript:alert(1)')), "");180      LegacyUnit.equal(styles.serialize(styles.parse('background:url(j\navas\u0000cr\tipt:alert(1)')), "");181      LegacyUnit.equal(styles.serialize(styles.parse('background:url(data:image/svg+xml,%3Csvg/%3E)')), "");182      LegacyUnit.equal(styles.serialize(styles.parse('background:url( data:image/svg+xml,%3Csvg/%3E)')), "");183      LegacyUnit.equal(styles.serialize(styles.parse('background:url\\28 data:image/svg+xml,%3Csvg/%3E)')), "");184      LegacyUnit.equal(styles.serialize(styles.parse('background:url("data: image/svg+xml,%3Csvg/%3E")')), "");185      LegacyUnit.equal(styles.serialize(styles.parse('background:url("data: ima ge/svg+xml,%3Csvg/%3E")')), "");186      LegacyUnit.equal(styles.serialize(styles.parse('background:url("data: image /svg+xml,%3Csvg/%3E")')), "");187    });188    suite.test('Script urls allowed', function () {189      var styles = new Styles({ allow_script_urls: true });190      LegacyUnit.equal(styles.serialize(styles.parse('behavior:url(test.htc)')), "behavior: url('test.htc');");191      LegacyUnit.equal(styles.serialize(styles.parse('color:expression(alert(1))')), "color: expression(alert(1));");192      LegacyUnit.equal(styles.serialize(styles.parse('background:url(javascript:alert(1)')), "background: url('javascript:alert(1');");193      LegacyUnit.equal(styles.serialize(styles.parse('background:url(vbscript:alert(1)')), "background: url('vbscript:alert(1');");194    });195    Pipeline.async({}, suite.toSteps({}), function () {196      success();197    }, failure);198  }...Styles.js
Source:Styles.js  
1module("tinymce.html.Styles");2test('Basic parsing/serializing', function() {3	var styles = new tinymce.html.Styles();4	expect(12);5	equal(styles.serialize(styles.parse('FONT-SIZE:10px')), "font-size: 10px;");6	equal(styles.serialize(styles.parse('FONT-SIZE:10px;COLOR:red')), "font-size: 10px; color: red;");7	equal(styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  red   ')), "font-size: 10px; color: red;");8	equal(styles.serialize(styles.parse('key:"value"')), "key: 'value';");9	equal(styles.serialize(styles.parse('key:"value1" \'value2\'')), "key: 'value1' 'value2';");10	equal(styles.serialize(styles.parse('key:"val\\"ue1" \'val\\\'ue2\'')), "key: 'val\"ue1' 'val\\'ue2';");11	equal(styles.serialize(styles.parse('width:100%')), 'width: 100%;');12	equal(styles.serialize(styles.parse('value:_; value2:"_"')), 'value: _; value2: \'_\';');13	equal(styles.serialize(styles.parse('value: "&"')), "value: '&';");14	equal(styles.serialize(styles.parse('value: "&"')), "value: '&';");15	equal(styles.serialize(styles.parse('value: ')), "");16	equal(styles.serialize(styles.parse("background: url('http://www.site.com/(foo)');")), "background: url('http://www.site.com/(foo)');");17});18test('Colors force hex and lowercase', function() {19	var styles = new tinymce.html.Styles();20	expect(6);21	equal(styles.serialize(styles.parse('color: rgb(1,2,3)')), "color: #010203;");22	equal(styles.serialize(styles.parse('color: RGB(1,2,3)')), "color: #010203;");23	equal(styles.serialize(styles.parse('color: #FF0000')), "color: #ff0000;");24	equal(styles.serialize(styles.parse('  color:   RGB  (  1  ,  2  ,  3  )  ')), "color: #010203;");25	equal(styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  RGB  (  1  ,  2  ,  3  )   ')), "font-size: 10px; color: #010203;");26	equal(styles.serialize(styles.parse('   FONT-SIZE  :  10px  ;   COLOR  :  RED   ')), "font-size: 10px; color: red;");27});28test('Urls convert urls and force format', function() {29	var styles = new tinymce.html.Styles({url_converter : function(url) {30		return '|' + url + '|';31	}});32	expect(9);33	equal(styles.serialize(styles.parse('background: url(a)')), "background: url('|a|');");34	equal(styles.serialize(styles.parse('background: url("a")')), "background: url('|a|');");35	equal(styles.serialize(styles.parse("background: url('a')")), "background: url('|a|');");36	equal(styles.serialize(styles.parse('background: url(   a   )')), "background: url('|a|');");37	equal(styles.serialize(styles.parse('background: url(   "a"   )')), "background: url('|a|');");38	equal(styles.serialize(styles.parse("background: url(    'a'    )")), "background: url('|a|');");39	equal(styles.serialize(styles.parse('background1: url(a); background2: url("a"); background3: url(\'a\')')), "background1: url('|a|'); background2: url('|a|'); background3: url('|a|');");40	equal(styles.serialize(styles.parse("background: url('http://www.site.com/a?a=b&c=d')")), "background: url('|http://www.site.com/a?a=b&c=d|');");41	equal(styles.serialize(styles.parse("background: url('http://www.site.com/a_190x144.jpg');")), "background: url('|http://www.site.com/a_190x144.jpg|');");42});43test('Compress styles', function() {44	var styles = new tinymce.html.Styles();45	equal(46		styles.serialize(styles.parse('border-top: 1px solid red; border-left: 1px solid red; border-bottom: 1px solid red; border-right: 1px solid red;')),47		'border: 1px solid red;'48	);49	equal(50		styles.serialize(styles.parse('border-width: 1pt 1pt 1pt 1pt; border-style: none none none none; border-color: black black black black;')),51		'border: 1pt none black;'52	);53	equal(54		styles.serialize(styles.parse('border-width: 1pt 4pt 2pt 3pt; border-style: solid dashed dotted none; border-color: black red green blue;')),55		'border-width: 1pt 4pt 2pt 3pt; border-style: solid dashed dotted none; border-color: black red green blue;'56	);57	equal(58		styles.serialize(styles.parse('border-top: 1px solid red; border-left: 1px solid red; border-right: 1px solid red; border-bottom: 1px solid red')),59		'border: 1px solid red;'60	);61	equal(62		styles.serialize(styles.parse('border-top: 1px solid red; border-right: 2px solid red; border-bottom: 3px solid red; border-left: 4px solid red')),63		'border-top: 1px solid red; border-right: 2px solid red; border-bottom: 3px solid red; border-left: 4px solid red;'64	);65	equal(66		styles.serialize(styles.parse('padding-top: 1px; padding-right: 2px; padding-bottom: 3px; padding-left: 4px')),67		'padding: 1px 2px 3px 4px;'68	);69	equal(70		styles.serialize(styles.parse('margin-top: 1px; margin-right: 2px; margin-bottom: 3px; margin-left: 4px')),71		'margin: 1px 2px 3px 4px;'72	);73	equal(74		styles.serialize(styles.parse('margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: 2px')),75		'margin: 1px 1px 1px 2px;'76	);77	equal(78		styles.serialize(styles.parse('margin-top: 2px; margin-right: 1px; margin-bottom: 1px; margin-left: 1px')),79		'margin: 2px 1px 1px 1px;'80	);81	equal(82		styles.serialize(styles.parse('border-top-color: red; border-right-color: green; border-bottom-color: blue; border-left-color: yellow')),83		'border-color: red green blue yellow;'84	);85	equal(86		styles.serialize(styles.parse('border-width: 1px; border-style: solid; border-color: red')),87		'border: 1px solid red;'88	);89	equal(90		styles.serialize(styles.parse('border-width: 1px; border-color: red')),91		'border-width: 1px; border-color: red;'92	);93});94test('Font weight', function() {95	var styles = new tinymce.html.Styles();96	expect(1);97	equal(styles.serialize(styles.parse('font-weight: 700')), "font-weight: bold;");98});99test('Valid styles', function() {100	var styles = new tinymce.html.Styles({}, new tinymce.html.Schema({valid_styles : {'*': 'color,font-size', 'a': 'margin-left'}}));101	expect(2);102	equal(styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; invalid: 1;'), 'b'), "color: #ff0000; font-size: 10px;");103	equal(styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; invalid: 2;'), 'a'), "color: #ff0000; font-size: 10px; margin-left: 10px;");104});105test('Invalid styles', function() {106	var styles = new tinymce.html.Styles({}, new tinymce.html.Schema({invalid_styles : {'*': 'color,font-size', 'a': 'margin-left'}}));107	equal(styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px'), 'b'), "margin-left: 10px;");108	equal(styles.serialize(styles.parse('color: #ff0000; font-size: 10px; margin-left: 10px; margin-right: 10px;'), 'a'), "margin-right: 10px;");109});110test('Suspicious (XSS) property names', function() {111	var styles = new tinymce.html.Styles();112	equal(styles.serialize(styles.parse('font-fa"on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");113	equal(styles.serialize(styles.parse('font-fa\\"on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");114	equal(styles.serialize(styles.parse('font-fa\\22on-load\\3dxss\\28\\29\\20mily:\'arial\'')), "");115});116test('Script urls denied', function() {117	var styles = new tinymce.html.Styles();118	equal(styles.serialize(styles.parse('behavior:url(test.htc)')), "");119	equal(styles.serialize(styles.parse('b\\65havior:url(test.htc)')), "");120	equal(styles.serialize(styles.parse('color:expression(alert(1))')), "");121	equal(styles.serialize(styles.parse('color:\\65xpression(alert(1))')), "");122	equal(styles.serialize(styles.parse('color:exp/**/ression(alert(1))')), "");123	equal(styles.serialize(styles.parse('color:/**/')), "");124	equal(styles.serialize(styles.parse('color:  expression  (  alert(1))')), "");125	equal(styles.serialize(styles.parse('background:url(jAvaScript:alert(1)')), "");126	equal(styles.serialize(styles.parse('background:url(javascript:alert(1)')), "");127	equal(styles.serialize(styles.parse('background:url(j\\61vascript:alert(1)')), "");128	equal(styles.serialize(styles.parse('background:url(\\6a\\61vascript:alert(1)')), "");129	equal(styles.serialize(styles.parse('background:url(\\6A\\61vascript:alert(1)')), "");130	equal(styles.serialize(styles.parse('background:url\\28\\6A\\61vascript:alert(1)')), "");131	equal(styles.serialize(styles.parse('background:\\75rl(j\\61vascript:alert(1)')), "");132	equal(styles.serialize(styles.parse('b\\61ckground:\\75rl(j\\61vascript:alert(1)')), "");133	equal(styles.serialize(styles.parse('background:url(vbscript:alert(1)')), "");134	equal(styles.serialize(styles.parse('background:url(j\navas\u0000cr\tipt:alert(1)')), "");135	equal(styles.serialize(styles.parse('background:url(data:image/svg+xml,%3Csvg/%3E)')), "");136	equal(styles.serialize(styles.parse('background:url( data:image/svg+xml,%3Csvg/%3E)')), "");137	equal(styles.serialize(styles.parse('background:url\\28 data:image/svg+xml,%3Csvg/%3E)')), "");138	equal(styles.serialize(styles.parse('background:url("data: image/svg+xml,%3Csvg/%3E")')), "");139	equal(styles.serialize(styles.parse('background:url("data: ima ge/svg+xml,%3Csvg/%3E")')), "");140	equal(styles.serialize(styles.parse('background:url("data: image /svg+xml,%3Csvg/%3E")')), "");141});142test('Script urls allowed', function() {143	var styles = new tinymce.html.Styles({allow_script_urls: true});144	equal(styles.serialize(styles.parse('behavior:url(test.htc)')), "behavior: url('test.htc');");145	equal(styles.serialize(styles.parse('color:expression(alert(1))')), "color: expression(alert(1));");146	equal(styles.serialize(styles.parse('background:url(javascript:alert(1)')), "background: url('javascript:alert(1');");147	equal(styles.serialize(styles.parse('background:url(vbscript:alert(1)')), "background: url('vbscript:alert(1');");...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
