Bug 1384463 - only trim CSS-allowed whitespace in declaration parser; r=gl

MozReview-Commit-ID: 7bnu2a9G1uq

--HG--
extra : rebase_source : 32fc701fe10f44e36c88aa73d4b234b94f9990f1
This commit is contained in:
Tom Tromey 2017-10-19 11:04:30 -06:00
Родитель 67aa4b3be9
Коммит 42097bd9cb
2 изменённых файлов: 36 добавлений и 5 удалений

Просмотреть файл

@ -365,6 +365,15 @@ const TEST_DATA = [
input: "stroke-dasharray: 1/*ThisIsAComment*/2;",
expected: [{name: "stroke-dasharray", value: "1 2", priority: "", offsets: [0, 39]}]
},
// Regression test for bug 1384463 - don't trim significant
// whitespace.
{
// \u00a0 is non-breaking space.
input: "\u00a0vertical-align: top",
expected: [{name: "\u00a0vertical-align", value: "top", priority: "",
offsets: [0, 20]}]
},
];
function run_test() {

Просмотреть файл

@ -249,6 +249,28 @@ function getEmptyDeclaration() {
colonOffsets: false};
}
/**
* Like trim, but only trims CSS-allowed whitespace.
*/
function cssTrim(str) {
let match = /^[ \t\r\n\f]*(.*?)[ \t\r\n\f]*$/.exec(str);
if (match) {
return match[1];
}
return str;
}
/**
* Like trimRight, but only trims CSS-allowed whitespace.
*/
function cssTrimRight(str) {
let match = /^(.*?)[ \t\r\n\f]*$/.exec(str);
if (match) {
return match[1];
}
return str;
}
/**
* A helper function that does all the parsing work for
* parseDeclarations. This is separate because it has some arguments
@ -310,7 +332,7 @@ function parseDeclarationsInternal(isCssPropertyKnown, inputString,
if (token.tokenType === "symbol" && token.text === ":") {
if (!lastProp.name) {
// Set the current declaration name if there's no name yet
lastProp.name = current.trim();
lastProp.name = cssTrim(current);
lastProp.colonOffsets = [token.startOffset, token.endOffset];
current = "";
hasBang = false;
@ -336,7 +358,7 @@ function parseDeclarationsInternal(isCssPropertyKnown, inputString,
current = "";
break;
}
lastProp.value = current.trim();
lastProp.value = cssTrim(current);
current = "";
hasBang = false;
declarations.push(getEmptyDeclaration());
@ -384,11 +406,11 @@ function parseDeclarationsInternal(isCssPropertyKnown, inputString,
// Ignore this case in comments.
if (!inComment) {
// Trailing property found, e.g. p1:v1;p2:v2;p3
lastProp.name = current.trim();
lastProp.name = cssTrim(current);
}
} else {
// Trailing value found, i.e. value without an ending ;
lastProp.value = current.trim();
lastProp.value = cssTrim(current);
let terminator = lexer.performEOFFixup("", true);
lastProp.terminator = terminator + ";";
// If the input was unterminated, attribute the remainder to
@ -833,7 +855,7 @@ RuleRewriter.prototype = {
// a property but which would break the entire style sheet.
let newText = this.inputString.substring(decl.colonOffsets[1],
decl.offsets[1]);
newText = unescapeCSSComment(newText).trimRight();
newText = cssTrimRight(unescapeCSSComment(newText));
this.result += this.sanitizeText(newText, index) + ";";
// See if the comment end can be deleted.