File: csssanitizer_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (709 lines) | stat: -rw-r--r-- 23,178 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// Copyright 2016 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 testcases for CSS Sanitizer.*/

goog.provide('goog.html.CssSanitizerTest');
goog.setTestOnly();

goog.require('goog.array');
goog.require('goog.html.SafeStyle');
goog.require('goog.html.SafeStyleSheet');
goog.require('goog.html.SafeUrl');
goog.require('goog.html.sanitizer.CssSanitizer');
goog.require('goog.html.testing');
goog.require('goog.string');
goog.require('goog.testing.dom');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
goog.require('goog.userAgent.product.isVersion');


var isIE8 = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9);


var isSafari9OrOlder =
    goog.userAgent.product.SAFARI && !goog.userAgent.product.isVersion(10);


var supportsDomParser =
    !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);


/**
 * @param {string} cssText CSS text usually associated with an inline style.
 * @return {!CSSStyleDeclaration} A styleSheet object.
 */
function getStyleFromCssText(cssText) {
  var styleDecleration = document.createElement('div').style;
  styleDecleration.cssText = cssText || '';
  return styleDecleration;
}


/**
 * Asserts that the expected CSS text is equal to the actual CSS text.
 * @param {string} expectedCssText Expected CSS text.
 * @param {string} actualCssText Actual CSS text.
 */
function assertCSSTextEquals(expectedCssText, actualCssText) {
  if (isIE8) {
    // We get a bunch of default values set in IE8 because of the way we iterate
    // over the CSSStyleDecleration keys.
    // TODO(danesh): Fix IE8 or remove this hack. It will be problematic for
    // tests which have an extra semi-colon in the value (even if quoted).
    var actualCssArray = actualCssText.split(/\s*;\s*/);
    var ie8StyleString = 'WIDTH: 0px; BOTTOM: 0px; HEIGHT: 0px; TOP: 0px; ' +
        'RIGHT: 0px; TEXT-DECORATION: none underline overline line-through; ' +
        'LEFT: 0px; TEXT-DECORATION: underline line-through;';
    goog.array.forEach(ie8StyleString.split(/\s*;\s*/), function(ie8Css) {
      goog.array.remove(actualCssArray, ie8Css);
    });
    actualCssText = actualCssArray.join('; ');
  }
  assertEquals(
      getStyleFromCssText(expectedCssText).cssText,
      getStyleFromCssText(actualCssText).cssText);
}


/**
 * Gets sanitized inline style.
 * @param {string} sourceCss CSS to be sanitized.
 * @param {function (string, string):?goog.html.SafeUrl=} opt_urlRewrite URL
 *     rewriter that only returns a goog.html.SafeUrl.
 * @return {string} Sanitized inline style.
 */
function getSanitizedInlineStyle(sourceCss, opt_urlRewrite) {
  try {
    return goog.html.SafeStyle.unwrap(
               goog.html.sanitizer.CssSanitizer.sanitizeInlineStyle(
                   getStyleFromCssText(sourceCss), opt_urlRewrite)) ||
        '';
  } catch (err) {
    // IE8 doesn't like setting invalid properties. It throws an "Invalid
    // Argument" exception.
    if (!isIE8) {
      throw err;
    }
    return '';
  }
}


/**
 * Asserts that the input CSS text is equal to the expected CSS text after
 * sanitization using {@link CssSanitizer.sanitizeStyleSheetString}.
 * @param {string} expectedCssText
 * @param {string} inputCssText
 * @param {?string=} opt_containerId
 * @param {function(string, string):?SafeUrl=} opt_uriRewriter
 */
function assertSanitizedCssEquals(
    expectedCssText, inputCssText, opt_containerId, opt_uriRewriter) {
  assertBrowserSanitizedCssEquals(
      {chrome: expectedCssText}, inputCssText, opt_containerId,
      opt_uriRewriter);
}


/**
 * Asserts that on each browser the input CSS text is equal to the expected CSS
 * text after sanitization using {@link CssSanitizer.sanitizeStyleSheetString}.
 * Automatically verifies that on older browsers the sanitizer returns an empty
 * string.
 * @param {!{
 *     chrome: string,
 *     firefox: (string|undefined),
 *     safari: (string|undefined),
 *     newIE: (string|undefined),
 *     ie10: (string|undefined)}} expectedCssTextByBrowser An object that maps
 *     each browser to a different expected value. All browsers but chrome are
 *     optional. If a browser is missing, the chrome expected value is used
 *     instead.
 * @param {string} inputCssText
 * @param {?string=} opt_containerId
 * @param {function(string, string):?SafeUrl=} opt_uriRewriter
 */
function assertBrowserSanitizedCssEquals(
    expectedCssTextByBrowser, inputCssText, opt_containerId, opt_uriRewriter) {
  var expectedCssText = undefined;
  if (goog.userAgent.product.CHROME) {
    expectedCssText = expectedCssTextByBrowser.chrome;
  } else if (goog.userAgent.product.FIREFOX) {
    expectedCssText = expectedCssTextByBrowser.firefox;
  } else if (goog.userAgent.product.SAFARI) {
    expectedCssText = expectedCssTextByBrowser.safari;
  } else if (goog.userAgent.product.IE && document.documentMode == 10) {
    expectedCssText = expectedCssTextByBrowser.ie10;
    console.log('ie10');
  } else if (
      goog.userAgent.product.IE && !goog.userAgent.isVersionOrHigher(10)) {
    // Don't even try with chrome as default for IE8 and IE9.
    expectedCssText = '';
  } else if (goog.userAgent.product.IE || goog.userAgent.product.EDGE) {
    expectedCssText = expectedCssTextByBrowser.newIE;
  } else {
    throw new Error('Unrecognized browser, this function needs to be updated.');
  }
  if (expectedCssText == undefined) {
    expectedCssText = expectedCssTextByBrowser.chrome;
  }
  assertEquals(
      expectedCssText,
      goog.html.SafeStyleSheet.unwrap(
          goog.html.sanitizer.CssSanitizer.sanitizeStyleSheetString(
              inputCssText,
              opt_containerId === undefined ? 'foo' : opt_containerId,
              opt_uriRewriter)));
}


/**
 * Converts rules in STYLE tags into style attributes on the tags they apply to,
 * and returns a new string.
 * @param {string} html
 * @return {string}
 */
function inlineStyleRulesString(html) {
  var tree = goog.html.sanitizer.CssSanitizer.safeParseHtmlAndGetInertElement(
      '<span>' + html + '</span>');
  if (tree == null) {
    return '';
  }
  goog.html.sanitizer.CssSanitizer.inlineStyleRules(tree);
  return tree.innerHTML;
}


/**
 * Inlines the rules in STYLE tags in the original HTML and asserts that it is
 * the same as the expected HTML.
 * @param {string} expectedHtml
 * @param {string} originalHtml
 */
function assertInlinedStyles(expectedHtml, originalHtml) {
  var inlinedHtml = inlineStyleRulesString(originalHtml);
  if (!supportsDomParser) {
    assertEquals('', inlinedHtml);
    return;
  }
  goog.testing.dom.assertHtmlMatches(
      expectedHtml, inlinedHtml, true /* opt_strictAttributes */);
}


function testValidCss() {
  var actualCSS = 'font-family: inherit';
  var expectedCSS = 'font-family: inherit';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // .1 -> 0.1; 1.0 -> 1
  actualCSS = 'padding: 1pt .1pt 1pt 1.0em';
  expectedCSS = 'padding: 1pt 0.1pt 1pt 1em';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // Negative margins are allowed.
  actualCSS = 'margin:    -7px -.5px -23px -1.25px';
  expectedCSS = 'margin: -7px -0.5px -23px -1.25px';
  if (isIE8) {
    // IE8 doesn't like sub-pixels
    // https://blogs.msdn.microsoft.com/ie/2010/11/03/sub-pixel-fonts-in-ie9/
    expectedCSS = expectedCSS.replace('-0.5px', '0px');
    expectedCSS = expectedCSS.replace('-1.25px', '-1px');
  }
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  actualCSS = 'quotes: "{" "}" "<" ">"';
  expectedCSS = 'quotes: "{" "}" "<" ">";';
  if (isSafari9OrOlder) {
    // We never figured out why Safari didn't work here, but it's obsolete now.
    expectedCSS = 'quotes: \'{\';';
  }
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}


function testInvalidCssRemoved() {
  var actualCSS;

  // Tests all have null results.
  var expectedCSS = '';

  actualCSS = 'font: Arial Black,monospace,Helvetica,#88ff88';
  // Hash values are not allowed so are dropped.
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // Negative numbers for border not allowed.
  actualCSS = 'border : -7px -0.5px -23px -1.25px';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // Negative numbers converted to empty.
  actualCSS = 'padding: -0 -.0 -0. -0.0 ';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // Invalid values not allowed.
  actualCSS = 'padding : #123 - 5 "5"';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  // Font-family does not allow quantities at all.
  actualCSS = 'font-family: 7 .5 23 1.25 -7 -.5 -23 -1.25 +7 +.5 +23 +1.25 ' +
      '7cm .5em 23.mm 1.25px -7cm -.5em -23.mm -1.25px ' +
      '+7cm +.5em +23.mm +1.25px 0 .0 -0+00.0 /';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));

  actualCSS = 'background: bogus url("foo.png") transparent';
  assertCSSTextEquals(
      expectedCSS,
      getSanitizedInlineStyle(actualCSS, goog.html.SafeUrl.sanitize));

  // expression(...) is not allowed for font so is rejected wholesale -- the
  // internal string "pwned" is not passed through.
  actualCSS = 'font-family: Arial Black,monospace,expression(return "pwned"),' +
      'Helvetica,#88ff88';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}


function testCssBackground() {
  var actualCSS, expectedCSS;

  function proxyUrl(url) {
    return goog.html.testing.newSafeUrlForTest(
        'https://goo.gl/proxy?url=' + url);
  }

  // Don't require the URL sanitizer to protect string boundaries.
  actualCSS = 'background-image: url("javascript:evil(1337)")';
  expectedCSS = '';
  assertCSSTextEquals(
      expectedCSS,
      getSanitizedInlineStyle(actualCSS, goog.html.SafeUrl.sanitize));

  actualCSS = 'background-image: url("http://goo.gl/foo.png")';
  expectedCSS =
      'background-image: url(https://goo.gl/proxy?url=http://goo.gl/foo.png)';
  assertCSSTextEquals(
      expectedCSS, getSanitizedInlineStyle(actualCSS, proxyUrl));

  // Without any URL sanitizer.
  actualCSS = 'background: transparent url("Bar.png")';
  var sanitizedCss = getSanitizedInlineStyle(actualCSS);
  assertFalse(goog.string.contains(sanitizedCss, 'background-image'));
  assertFalse(goog.string.contains(sanitizedCss, 'Bar.png'));
}

function testVendorPrefixed() {
  var actualCSS = '-webkit-text-stroke: 1px red';
  var expectedCSS = '';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}

function testDisallowedFunction() {
  var actualCSS = 'border-width: calc(10px + 20px)';
  var expectedCSS = '';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}

function testColor() {
  var colors = [
    'red', 'Red', 'RED', 'Gray', 'grey', '#abc', '#123', '#ABC123',
    'rgb( 127, 64 , 255 )'
  ];
  var notcolors = [
    // Finding words that are not X11 colors is harder than you think.
    'killitwithfire', 'invisible', 'expression(red=blue)', '#aa-1bb',
    '#expression', '#doevil'
    // 'rgb(0, 0, 100%)' // Invalid in all browsers
    // 'rgba(128,255,128,50%)', // Invalid in all browsers
  ];

  for (var i = 0; i < colors.length; ++i) {
    var validColorValue = 'color: ' + colors[i];
    assertCSSTextEquals(
        validColorValue, getSanitizedInlineStyle(validColorValue));
  }

  for (var i = 0; i < notcolors.length; ++i) {
    var invalidColorValue = 'color: ' + notcolors[i];
    assertCSSTextEquals('', getSanitizedInlineStyle(invalidColorValue));
  }
}


function testCustomVariablesSanitized() {
  var actualCSS = '\\2d-leak: leakTest; background: var(--leak);';
  assertCSSTextEquals('', getSanitizedInlineStyle(actualCSS));
}


function testExpressionsPreserved() {
  if (isIE8) {
    // Disable this test as IE8 doesn't support expressions.
    // https://msdn.microsoft.com/en-us/library/ms537634(v=VS.85).aspx
    return;
  }

  var actualCSS, expectedCSS;
  actualCSS = 'background-image: linear-gradient(to bottom right, red, blue)';
  expectedCSS = 'background-image: linear-gradient(to right bottom, red, blue)';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}


function testMultipleInlineStyles() {
  var actualCSS = 'margin: 1px ; padding: 0';
  var expectedCSS = 'margin: 1px; padding: 0px;';
  assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS));
}


function testSanitizeInlineStyleString() {
  var tests = [
    {
      // empty string
      inputCss: '',
      sanitizedCss: ''
    },
    {
      // one rule
      inputCss: 'color: red',
      sanitizedCss: 'color: red;'
    },
    {
      // two rules
      inputCss: 'color: green; padding: 10px',
      sanitizedCss: 'color: green; padding: 10px;'
    },
    {
      // malicious rule
      inputCss: 'color: expression("pwned")',
      sanitizedCss: ''
    },
    {
      // disallowed URL
      inputCss: 'background-image: url("http://example.com")',
      sanitizedCss: ''
    },
    {
      // disallowed URL
      inputCss: 'background-image: url("http://example.com")',
      sanitizedCss: '',
      uriRewriter: function(uri) {
        return null;
      }
    },
    {
      // allowed URL
      inputCss: 'background-image: url("http://example.com")',
      sanitizedCss: 'background-image: url("http://example.com");',
      uriRewriter: goog.html.SafeUrl.sanitize
    },
    {
      // preserves case
      inputCss: 'font-family: Roboto, sans-serif',
      sanitizedCss: 'font-family: Roboto, sans-serif'
    }
  ];

  for (var i = 0; i < tests.length; i++) {
    var test = tests[i];

    var expectedOutput = test.sanitizedCss;
    if (goog.userAgent.IE && document.documentMode < 10) {
      expectedOutput = '';
    }

    var safeStyle = goog.html.sanitizer.CssSanitizer.sanitizeInlineStyleString(
        test.inputCss, test.uriRewriter);
    var output = goog.html.SafeStyle.unwrap(safeStyle);
    assertCSSTextEquals(expectedOutput, output);
  }
}


/**
 * @suppress {accessControls}
 */
function testInertDocument() {
  if (!document.implementation.createHTMLDocument) {
    return;  // skip test
  }

  window.xssFiredInertDocument = false;
  var doc = goog.html.sanitizer.CssSanitizer.createInertDocument_();
  try {
    doc.write('<script> window.xssFiredInertDocument = true; </script>');
  } catch (e) {
    // ignore
  }
  assertFalse(window.xssFiredInertDocument);
}


/**
 * @suppress {accessControls}
 */
function testInertCustomElements() {
  if (typeof HTMLTemplateElement != 'function' || !document.registerElement) {
    return;  // skip test
  }

  var inertDoc = goog.html.sanitizer.CssSanitizer.createInertDocument_();
  var xFooConstructor = document.registerElement('x-foo');
  var xFooElem =
      document.implementation.createHTMLDocument('').createElement('x-foo');
  assertTrue(xFooElem instanceof xFooConstructor);  // sanity check

  var inertXFooElem = inertDoc.createElement('x-foo');
  assertFalse(inertXFooElem instanceof xFooConstructor);
}


function testSanitizeStyleSheetString_basic() {
  var input = '';
  assertSanitizedCssEquals(input, input);

  input = 'a {color: red}';
  var expected = '#foo a{color: red;}';
  assertSanitizedCssEquals(expected, input);

  input = 'a {color: red} b {color:red; not-allowed: 1; ' +
      'background-image: url(\'http://not.allowed\');}';
  expected = '#foo a{color: red;}#foo b{color: red;}';
  assertSanitizedCssEquals(expected, input);
}


function testSanitizeInlineStyleString_noSelector() {
  var input = 'a{color: red;}';
  assertSanitizedCssEquals(input, input, null /* opt_containerId */);
}


function testSanitizeStyleSheetString_comma() {
  var input = 'a, b, c > d {color: red}';
  var expected = '#foo a,#foo b,#foo c > d{color: red;}';
  assertSanitizedCssEquals(expected, input);
}


function testSanitizeStyleSheetString_atRule() {
  var input = '@media screen { a { color: red; } }';
  var expected = '';
  assertSanitizedCssEquals(expected, input);
}


function testSanitizeStyleSheetString_urlRewrite() {
  var urlRewriter = function(url) {
    if (input.indexOf('bar') > -1) {
      return goog.html.SafeUrl.sanitize(url);
    } else {
      return null;
    }
  };

  var input = 'a {background-image: url("http://bar.com")}';
  var quoted = '#foo a{background-image: url("http://bar.com");}';
  // Safari will strip quotes if they are not needed and add a slash.
  var unquoted = '#foo a{background-image: url(http://bar.com/);}';
  assertBrowserSanitizedCssEquals(
      {safari: unquoted, chrome: quoted}, input,
      undefined /* opt_containerId */, urlRewriter);

  input = 'a {background-image: url("http://nope.com")}';
  var expected = '#foo a{}';
  assertSanitizedCssEquals(
      expected, input, undefined /* opt_containerId */, urlRewriter);

  input = 'a{background-image: url("javascript:alert(\"bar\")")}';
  expected = '#foo a{}';
  assertSanitizedCssEquals(
      expected, input, undefined /* opt_containerId */, urlRewriter);
}


function testSanitizeStyleSheetString_unrecognized() {
  var input = 'a {mso-font-size: 2px; color: black}';
  var expected = 'a{color: black;}';
  assertSanitizedCssEquals(expected, input, null /* opt_containerId */);
}


function testSanitizeStyleSheetString_malformed() {
  var input = '<script>alert(1)</script>';
  var expected = '';
  assertSanitizedCssEquals(expected, input);

  input = 'a { } </style><script>alert(1)</script><style>';
  expected = '#foo a{}';
  assertSanitizedCssEquals(expected, input);

  input = 'a < b { } a { font-size: 10px }';
  expected = '#foo a{font-size: 10px;}';
  assertSanitizedCssEquals(expected, input);

  input = 'a {;;;} a { font-size: 10px } ;;; a { background-image: url(() }};;';
  expected = '#foo a{}#foo a{font-size: 10px;}';
  assertSanitizedCssEquals(expected, input);

  input = 'a[a=\"ccc] { color: red;}';
  expected = '';
  assertSanitizedCssEquals(expected, input);
}


function testSanitizeStyleSheetString_stringWithNoEscapedQuotesInSelector() {
  var input = 'a[data-foo="foo,bar"], b { color: red }';
  // IE converts the string to single quotes.
  var doubleQuoted = '#foo a[data-foo="foo,bar"],#foo b{color: red;}';
  var singleQuoted = '#foo a[data-foo=\'foo,bar\'],#foo b{color: red;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input);

  input = 'a[data-foo=\'foo,bar\'], b { color: red }';
  // Chrome converts the string to double quotes.
  doubleQuoted = '#foo a[data-foo="foo,bar"],#foo b{color: red;}';
  singleQuoted = '#foo a[data-foo=\'foo,bar\'],#foo b{color: red;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input);

  input = 'a[foo="foo,bar"][bar="baz"], b { color: blue }';
  doubleQuoted = '#foo a[foo="foo,bar"][bar="baz"],#foo b{color: blue;}';
  singleQuoted = '#foo a[foo=\'foo,bar\'][bar=\'baz\'],#foo b{color: blue;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input);

  input = 'a[foo="foo,bar"], b, c[foo="f,b"][bar="f,b"] { color: red }';
  doubleQuoted = '#foo a[foo="foo,bar"],#foo b,#foo c[foo="f,b"][bar="f,b"]' +
      '{color: red;}';
  singleQuoted =
      '#foo a[foo=\'foo,bar\'],#foo b,#foo c[bar=\'f,b\'][foo=\'f,b\']' +
      '{color: red;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input);
}


function testSanitizeStyleSheetString_stringWithEscapedQuotesInSelector() {
  // Contains an escaped string, but the selector is converted to a[a='a"b']
  // before the regex is executed.
  var input = 'a[a="a\\"b"] { color: black; }';
  var doubleQuoted = '#foo a[a="a\\"b"]{color: black;}';
  var singleQuoted = '#foo a[a=\'a"b\']{color: black;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, ie10: singleQuoted, newIE: singleQuoted}, input);

  input = 'a[a="a\\\'b"] { color: grey; }';
  doubleQuoted = '#foo a[a="a\'b"]{color: grey;}';
  singleQuoted = '#foo a[a=\'a\\\'b\']{color: grey;}';
  assertBrowserSanitizedCssEquals(
      {
        chrome: doubleQuoted,
        firefox: doubleQuoted,
        ie10: '',
        newIE: singleQuoted
      },
      input);

  input = 'a[a=\'\\\'b\'] {color: red; }';
  doubleQuoted = '#foo a[a="\'b"]{color: red;}';
  singleQuoted = '#foo a[a=\'\\\'b\']{color: red;}';
  assertBrowserSanitizedCssEquals(
      {
        chrome: doubleQuoted,
        firefox: doubleQuoted,
        newIE: singleQuoted,
        ie10: ''
      },
      input);

  input = 'a[foo=\'b\\\'a, ,\'] { color: blue; }';
  doubleQuoted = '#foo a[foo="b\'a, ,"]{color: blue;}';
  singleQuoted = '#foo a[foo=\'b\\\'a, ,\']{color: blue;}';
  assertBrowserSanitizedCssEquals(
      {
        chrome: doubleQuoted,
        firefox: doubleQuoted,
        newIE: singleQuoted,
        ie10: ''
      },
      input);

  input = 'a[a=\'a\\"b\'] { color: black; }';
  doubleQuoted = '#foo a[a="a\\"b"]{color: black;}';
  singleQuoted = '#foo a[a=\'a"b\']{color: black;}';
  assertBrowserSanitizedCssEquals(
      {chrome: doubleQuoted, ie10: singleQuoted, newIE: singleQuoted}, input);
}


function testSanitizeInlineStyleString_invalidSelector() {
  var input = 'a{}';
  if (supportsDomParser) {
    assertThrows(function() {
      goog.html.sanitizer.CssSanitizer.sanitizeStyleSheetString(
          input, '</style>');
    });
  }
}


function testInlineStyleRules_empty() {
  assertInlinedStyles('', '');
}


function testInlineStyleRules_basic() {
  var input = '<style>a{color:red}</style><a>foo</a>';
  var expected = '<a style="color:red;">foo</a>';
  assertInlinedStyles(expected, input);
}


function testInlineStyleRules_onlyStyle() {
  var input = '<style>a{color:red}</style>';
  assertInlinedStyles('', input);
}


function testInlineStyleRules_noStyle() {
  var input = '<a>hi</a>';
  assertInlinedStyles(input, input);
}


function testInlineStyleRules_onlyText() {
  var input = 'hello';
  assertInlinedStyles(input, input);
}


function testInlineStyleRules_specificity() {
  var input = '<style>a{color: red; border: 1px}' +
      '#foo{color: white; border: 2px}</style>' +
      '<a id="foo" style="color: black">foo</a>';
  var expected = '<a id="foo" style="color: black; border: 2px">foo</a>';
  assertInlinedStyles(expected, input);
}


function testInlineStyleRules_media() {
  var input = '<style>a{color: red;} @media screen { border: 1px; }</style>' +
      '<a id="foo">foo</a>';
  var expected = '<a id="foo" style="color: red;">foo</a>';
  assertInlinedStyles(expected, input);
}