Bug 1621174 - Fix error handling for setting KeyframeEffect.pseudoElement. r=birtles,smaug

We should throw a DOMException SyntaxError when setting pseudoElement a
syntactically invalid string or an unsupported pseudo.

Differential Revision: https://phabricator.services.mozilla.com/D66321
This commit is contained in:
Boris Chiou
2020-03-12 00:34:46 +00:00
parent 6c59785065
commit 5af1638755
4 changed files with 24 additions and 16 deletions

View File

@@ -745,17 +745,20 @@ static KeyframeEffectParams KeyframeEffectParamsFromUnion(
RefPtr<nsAtom> pseudoAtom =
nsCSSPseudoElements::GetPseudoAtom(options.mPseudoElement);
if (!pseudoAtom) {
aRv.ThrowTypeError<MSG_INVALID_PSEUDO_SELECTOR>(
NS_ConvertUTF16toUTF8(options.mPseudoElement));
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
NS_ConvertUTF16toUTF8(options.mPseudoElement).get()));
return result;
}
result.mPseudoType = nsCSSPseudoElements::GetPseudoType(
pseudoAtom, CSSEnabledState::ForAllContent);
if (!IsSupportedPseudoForWebAnimation(result.mPseudoType)) {
aRv.ThrowTypeError<MSG_UNSUPPORTED_PSEUDO_SELECTOR>(
NS_ConvertUTF16toUTF8(options.mPseudoElement));
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
NS_ConvertUTF16toUTF8(options.mPseudoElement).get()));
}
return result;
@@ -1064,16 +1067,20 @@ void KeyframeEffect::SetPseudoElement(const nsAString& aPseudoElement,
RefPtr<nsAtom> pseudoAtom =
nsCSSPseudoElements::GetPseudoAtom(aPseudoElement);
if (!pseudoAtom) {
aRv.ThrowTypeError<MSG_INVALID_PSEUDO_SELECTOR>(
NS_ConvertUTF16toUTF8(aPseudoElement));
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
NS_ConvertUTF16toUTF8(aPseudoElement).get()));
return;
}
pseudoType = nsCSSPseudoElements::GetPseudoType(
pseudoAtom, CSSEnabledState::ForAllContent);
if (!IsSupportedPseudoForWebAnimation(pseudoType)) {
aRv.ThrowTypeError<MSG_UNSUPPORTED_PSEUDO_SELECTOR>(
NS_ConvertUTF16toUTF8(aPseudoElement));
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
NS_ConvertUTF16toUTF8(aPseudoElement).get()));
return;
}

View File

@@ -76,8 +76,6 @@ MSG_DEF(MSG_CACHE_ADD_FAILED_RESPONSE, 4, true, JSEXN_TYPEERR, "{0}Cache got {1}
MSG_DEF(MSG_SW_UPDATE_BAD_REGISTRATION, 3, true, JSEXN_TYPEERR, "{0}Failed to update the ServiceWorker for scope {1} because the registration has been {2} since the update was scheduled.")
MSG_DEF(MSG_INVALID_DURATION_ERROR, 2, true, JSEXN_TYPEERR, "{0}Invalid duration '{1}'.")
MSG_DEF(MSG_INVALID_EASING_ERROR, 2, true, JSEXN_TYPEERR, "{0}Invalid easing '{1}'.")
MSG_DEF(MSG_INVALID_PSEUDO_SELECTOR, 2, true, JSEXN_TYPEERR, "{0}Invalid pseudo-selector '{1}'")
MSG_DEF(MSG_UNSUPPORTED_PSEUDO_SELECTOR, 2, true, JSEXN_TYPEERR, "{0}Unsupported pseudo-selector '{1}'")
MSG_DEF(MSG_TOKENLIST_NO_SUPPORTED_TOKENS, 3, true, JSEXN_TYPEERR, "{0}{1} attribute of <{2}> does not define any supported tokens")
MSG_DEF(MSG_TIME_VALUE_OUT_OF_RANGE, 2, true, JSEXN_TYPEERR, "{0}{1} is outside the supported range for time values.")
MSG_DEF(MSG_ONLY_IF_CACHED_WITHOUT_SAME_ORIGIN, 2, true, JSEXN_TYPEERR, "{0}Request mode '{1}' was used, but request cache mode 'only-if-cached' can only be used with request mode 'same-origin'.")

View File

@@ -317,15 +317,17 @@ test(t => {
for (const pseudo of [
'',
'before',
':abc',
'::abc',
'::placeholder',
]) {
test(t => {
const div = createDiv(t);
assert_throws_js(TypeError, () => {
assert_throws_dom("SyntaxError", () => {
div.animate(null, {pseudoElement: pseudo});
});
}, `animate() with the invalid pseudoElement '${pseudo}' throws a TypeError`);
}, `animate() with a non-null invalid pseudoElement '${pseudo}' throws a ` +
`SyntaxError`);
}
</script>

View File

@@ -251,14 +251,15 @@ for (const hasContent of [true, false]){
for (const pseudo of [
'',
'before',
':abc',
'::abc',
'::placeholder',
]) {
test(t => {
const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
assert_throws_js(TypeError, () => effect.pseudoElement = pseudo );
}, `Changing pseudoElement to invalid pseudo-selector '${pseudo}' throws a ` +
`TypeError`);
assert_throws_dom("SyntaxError", () => effect.pseudoElement = pseudo );
}, `Changing pseudoElement to a non-null invalid pseudo-selector ` +
`'${pseudo}' throws a SyntaxError`);
}
</script>