Reviewers: felix8a,
Description:
Recognize "!important" at the end of CSS property values in stylesheets.
Please review this at https://codereview.appspot.com/11745045/
Affected files:
M src/com/google/caja/plugin/sanitizecss.js
M tests/com/google/caja/plugin/sanitizecss_test.html
M tests/com/google/caja/plugin/sanitizecss_test.js
Index: src/com/google/caja/plugin/sanitizecss.js
===================================================================
--- src/com/google/caja/plugin/sanitizecss.js (revision 5500)
+++ src/com/google/caja/plugin/sanitizecss.js (working copy)
@@ -692,8 +692,9 @@
* which is true if the external url itself contained other
external
* URLs.
*/
- function sanitizeStylesheetInternal(baseUri, cssText, virtualization,
- naiveUriRewriter, naiveUriFetcher, continuation) {
+ function sanitizeStylesheetInternal(
+ baseUri, cssText, virtualization, naiveUriRewriter,
naiveUriFetcher,
+ continuation) {
var safeCss = void 0;
var moreToCome = false;
// A stack describing the { ... } regions.
@@ -838,10 +839,20 @@
},
declaration: function (property, valueArray) {
if (!elide) {
+ var isImportant = false;
+ var nValues = valueArray.length;
+ if (nValues >= 2
+ && valueArray[nValues - 2] === '!'
+ && valueArray[nValues - 1].toLowerCase()
=== 'important') {
+ isImportant = true;
+ valueArray.length -= 2;
+ }
sanitizeCssProperty(
property, valueArray, naiveUriRewriter, baseUri);
if (valueArray.length) {
- safeCss.push(property, ':', valueArray.join(' '), ';');
+ safeCss.push(
+ property, ':', valueArray.join(' '),
+ isImportant ? ' !important;' : ';');
}
}
}
@@ -859,14 +870,17 @@
sanitizeStylesheet = function (
baseUri, cssText, virtualization, naiveUriRewriter) {
- return sanitizeStylesheetInternal(baseUri, cssText, virtualization,
- naiveUriRewriter, undefined, undefined).result;
+ return sanitizeStylesheetInternal(
+ baseUri, cssText, virtualization,
+ naiveUriRewriter, undefined, undefined).result;
};
- sanitizeStylesheetWithExternals = function (baseUri, cssText,
- virtualization, naiveUriRewriter, naiveUriFetcher, continuation) {
- return sanitizeStylesheetInternal(baseUri, cssText, virtualization,
- naiveUriRewriter, naiveUriFetcher, continuation);
+ sanitizeStylesheetWithExternals = function (
+ baseUri, cssText, virtualization, naiveUriRewriter,
naiveUriFetcher,
+ continuation) {
+ return sanitizeStylesheetInternal(
+ baseUri, cssText, virtualization,
+ naiveUriRewriter, naiveUriFetcher, continuation);
};
})();
})();
Index: tests/com/google/caja/plugin/sanitizecss_test.html
===================================================================
--- tests/com/google/caja/plugin/sanitizecss_test.html (revision 5500)
+++ tests/com/google/caja/plugin/sanitizecss_test.html (working copy)
@@ -6,6 +6,7 @@
<script src="css-defs.js"></script>
<script src="html4-defs.js"></script>
<script src="csslexer.js"></script>
+ <script src="cssparser.js"></script>
<script src="sanitizecss.js"></script>
<script src="jsUnitCore.js"></script>
<script src="jsunit.js"></script>
Index: tests/com/google/caja/plugin/sanitizecss_test.js
===================================================================
--- tests/com/google/caja/plugin/sanitizecss_test.js (revision 5500)
+++ tests/com/google/caja/plugin/sanitizecss_test.js (working copy)
@@ -363,3 +363,28 @@
log);
jsunit.pass();
});
+
+jsunitRegister('testImportant', function testImportant() {
+ function assertSanitizedStylesheet(golden, input) {
+ var selectors = sanitizeStylesheet(
+ 'http://example.com/baseurl', input,
+ {
+ containerClass: 'scopeClass',
+ idSuffix: '-suffix',
+ tagPolicy: function (elName, attrs) { return []; }
+ });
+ assertArrayEquals(input, golden, selectors);
+ }
+
+ assertSanitizedStylesheet(
+ ''
+ + '.scopeClass p{color:red !important;}'
+ + '.scopeClass q{color:green !important;}'
+ + '.scopeClass #id-suffix{color:blue;}',
+ ''
+ + 'p { color: red !important }\n'
+ + 'q { color: green ! Important }\n'
+ + '#id { color: blue }');
+
+ jsunit.pass();
+});
--
---
You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.