Add support for regexp() function in @-moz-document rule. (Bug 398962) r=bzbarsky

This commit is contained in:
L. David Baron 2011-04-28 10:21:37 -07:00
parent 2ee5bebdb6
commit 6c68aef211
11 changed files with 198 additions and 56 deletions

View File

@ -1777,6 +1777,25 @@ public:
FindInternalContentViewer(const char* aType,
ContentViewerType* aLoaderType = nsnull);
/**
* This helper method returns true if the aPattern pattern matches aValue.
* aPattern should not contain leading and trailing slashes (/).
* The pattern has to match the entire value not just a subset.
* aDocument must be a valid pointer (not null).
*
* This is following the HTML5 specification:
* http://dev.w3.org/html5/spec/forms.html#attr-input-pattern
*
* WARNING: This method mutates aPattern and aValue!
*
* @param aValue the string to check.
* @param aPattern the string defining the pattern.
* @param aDocument the owner document of the element.
* @result whether the given string is matches the pattern.
*/
static PRBool IsPatternMatching(nsAString& aValue, nsAString& aPattern,
nsIDocument* aDocument);
/**
* Calling this adds support for
* ontouch* event handler DOM attributes.

View File

@ -6552,3 +6552,37 @@ nsContentUtils::FindInternalContentViewer(const char* aType,
return NULL;
}
// static
PRBool
nsContentUtils::IsPatternMatching(nsAString& aValue, nsAString& aPattern,
nsIDocument* aDocument)
{
NS_ASSERTION(aDocument, "aDocument should be a valid pointer (not null)");
NS_ENSURE_TRUE(aDocument->GetScriptGlobalObject(), PR_TRUE);
JSContext* ctx = (JSContext*) aDocument->GetScriptGlobalObject()->
GetContext()->GetNativeContext();
NS_ENSURE_TRUE(ctx, PR_TRUE);
JSAutoRequest ar(ctx);
// The pattern has to match the entire value.
aPattern.Insert(NS_LITERAL_STRING("^(?:"), 0);
aPattern.Append(NS_LITERAL_STRING(")$"));
JSObject* re = JS_NewUCRegExpObjectNoStatics(ctx, reinterpret_cast<jschar*>
(aPattern.BeginWriting()),
aPattern.Length(), 0);
NS_ENSURE_TRUE(re, PR_TRUE);
jsval rval = JSVAL_NULL;
size_t idx = 0;
JSBool res;
res = JS_ExecuteRegExpNoStatics(ctx, re, reinterpret_cast<jschar*>
(aValue.BeginWriting()),
aValue.Length(), &idx, JS_TRUE, &rval);
return res == JS_FALSE || rval != JSVAL_NULL;
}

View File

@ -3813,7 +3813,7 @@ nsHTMLInputElement::HasPatternMismatch() const
return PR_FALSE;
}
return !IsPatternMatching(value, pattern, doc);
return !nsContentUtils::IsPatternMatching(value, pattern, doc);
}
void
@ -4095,40 +4095,6 @@ nsHTMLInputElement::IsValidEmailAddress(const nsAString& aValue)
return PR_TRUE;
}
//static
PRBool
nsHTMLInputElement::IsPatternMatching(nsAString& aValue, nsAString& aPattern,
nsIDocument* aDocument)
{
NS_ASSERTION(aDocument, "aDocument should be a valid pointer (not null)");
NS_ENSURE_TRUE(aDocument->GetScriptGlobalObject(), PR_TRUE);
JSContext* ctx = (JSContext*) aDocument->GetScriptGlobalObject()->
GetContext()->GetNativeContext();
NS_ENSURE_TRUE(ctx, PR_TRUE);
JSAutoRequest ar(ctx);
// The pattern has to match the entire value.
aPattern.Insert(NS_LITERAL_STRING("^(?:"), 0);
aPattern.Append(NS_LITERAL_STRING(")$"));
JSObject* re = JS_NewUCRegExpObjectNoStatics(ctx, reinterpret_cast<jschar*>
(aPattern.BeginWriting()),
aPattern.Length(), 0);
NS_ENSURE_TRUE(re, PR_TRUE);
jsval rval = JSVAL_NULL;
size_t idx = 0;
JSBool res;
res = JS_ExecuteRegExpNoStatics(ctx, re, reinterpret_cast<jschar*>
(aValue.BeginWriting()),
aValue.Length(), &idx, JS_TRUE, &rval);
return res == JS_FALSE || rval != JSVAL_NULL;
}
NS_IMETHODIMP_(PRBool)
nsHTMLInputElement::IsSingleLineTextControl() const
{

View File

@ -368,23 +368,6 @@ protected:
*/
static PRBool IsValidEmailAddressList(const nsAString& aValue);
/**
* This helper method returns true if the aPattern pattern matches aValue.
* aPattern should not contain leading and trailing slashes (/).
* The pattern has to match the entire value not just a subset.
* aDocument must be a valid pointer (not null).
*
* This is following the HTML5 specification:
* http://dev.w3.org/html5/spec/forms.html#attr-input-pattern
*
* @param aValue the string to check.
* @param aPattern the string defining the pattern.
* @param aDocument the owner document of the element.
* @result whether the given string is matches the pattern.
*/
static PRBool IsPatternMatching(nsAString& aValue, nsAString& aPattern,
nsIDocument* aDocument);
// Helper method
nsresult SetValueInternal(const nsAString& aValue,
PRBool aUserInput,

View File

@ -58,6 +58,7 @@ PEGroupRuleEOF=end of @media or @-moz-document rule
PEGroupRuleNestedAtRule=%1$S rule not allowed within @media or @-moz-document rule.
PEMozDocRuleBadFunc=Expected url(), url-prefix(), or domain() in @-moz-document rule but found '%1$S'.
PEMozDocRuleNotURI=Expected URI in @-moz-document rule but found '%1$S'.
PEMozDocRuleNotString=Expected string in @-moz-document rule regexp() function but found '%1$S'.
PEAtNSPrefixEOF=namespace prefix in @namespace rule
PEAtNSURIEOF=namespace URI in @namespace rule
PEAtNSUnexpected=Unexpected token within @namespace: '%1$S'.

View File

@ -2116,7 +2116,8 @@ CSSParserImpl::ParseMozDocumentRule(RuleAppendFunc aAppendFunc, void* aData)
!(eCSSToken_URL == mToken.mType ||
(eCSSToken_Function == mToken.mType &&
(mToken.mIdent.LowerCaseEqualsLiteral("url-prefix") ||
mToken.mIdent.LowerCaseEqualsLiteral("domain"))))) {
mToken.mIdent.LowerCaseEqualsLiteral("domain") ||
mToken.mIdent.LowerCaseEqualsLiteral("regexp"))))) {
REPORT_UNEXPECTED_TOKEN(PEMozDocRuleBadFunc);
delete urls;
return PR_FALSE;
@ -2126,6 +2127,21 @@ CSSParserImpl::ParseMozDocumentRule(RuleAppendFunc aAppendFunc, void* aData)
if (mToken.mType == eCSSToken_URL) {
cur->func = css::DocumentRule::eURL;
CopyUTF16toUTF8(mToken.mIdent, cur->url);
} else if (mToken.mIdent.LowerCaseEqualsLiteral("regexp")) {
// regexp() is different from url-prefix() and domain() (but
// probably the way they *should* have been* in that it requires a
// string argument, and doesn't try to behave like url().
cur->func = css::DocumentRule::eRegExp;
GetToken(PR_TRUE);
// copy before we know it's valid (but before ExpectSymbol changes
// mToken.mIdent)
CopyUTF16toUTF8(mToken.mIdent, cur->url);
if (eCSSToken_String != mToken.mType || !ExpectSymbol(')', PR_TRUE)) {
REPORT_UNEXPECTED_TOKEN(PEMozDocRuleNotString);
SkipUntil(')');
delete urls;
return PR_FALSE;
}
} else {
if (mToken.mIdent.LowerCaseEqualsLiteral("url-prefix")) {
cur->func = css::DocumentRule::eURLPrefix;

View File

@ -947,6 +947,8 @@ DocumentRule::List(FILE* out, PRInt32 aIndent) const
break;
case eDomain:
str.AppendLiteral("domain(\"");
case eRegExp:
str.AppendLiteral("regexp(\"");
break;
}
nsCAutoString escapedURL(url->url);
@ -998,6 +1000,9 @@ DocumentRule::GetCssText(nsAString& aCssText)
case eDomain:
aCssText.AppendLiteral("domain(");
break;
case eRegExp:
aCssText.AppendLiteral("regexp(");
break;
}
nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(url->url),
aCssText);
@ -1049,9 +1054,10 @@ DocumentRule::DeleteRule(PRUint32 aIndex)
// GroupRule interface
/* virtual */ PRBool
DocumentRule::UseForPresentation(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey& aKey)
nsMediaQueryResultCacheKey& aKey)
{
nsIURI *docURI = aPresContext->Document()->GetDocumentURI();
nsIDocument *doc = aPresContext->Document();
nsIURI *docURI = doc->GetDocumentURI();
nsCAutoString docURISpec;
if (docURI)
docURI->GetSpec(docURISpec);
@ -1080,6 +1086,13 @@ DocumentRule::UseForPresentation(nsPresContext* aPresContext,
return PR_TRUE;
}
} break;
case eRegExp: {
NS_ConvertUTF8toUTF16 spec(docURISpec);
NS_ConvertUTF8toUTF16 regex(url->url);
if (nsContentUtils::IsPatternMatching(spec, regex, doc)) {
return PR_TRUE;
}
} break;
}
}

View File

@ -156,7 +156,8 @@ public:
enum Function {
eURL,
eURLPrefix,
eDomain
eDomain,
eRegExp
};
struct URL {

View File

@ -49,8 +49,16 @@ _CHROME_FILES = \
bug535806-html.html \
bug535806-xul.xul \
test_hover.html \
test_moz_document_rules.html \
hover_helper.html \
$(NULL)
_TEST_FILES = \
moz_document_helper.html \
$(NULL)
libs:: $(_CHROME_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
libs:: $(_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)

View File

@ -0,0 +1,2 @@
<!DOCTYPE HTML>
<div id="display" style="position: relative"></div>

View File

@ -0,0 +1,99 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for @-moz-document rules</title>
<script type="application/javascript" src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body onload="run()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=398962">Mozilla Bug 398962</a>
<iframe id="iframe" src="http://mochi.test:8888/tests/layout/style/test/chrome/moz_document_helper.html"></iframe>
<pre id="test">
<script type="application/javascript; version=1.8">
var [gStyleSheetService, gIOService] = (function() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
return [
Components.classes["@mozilla.org/content/style-sheet-service;1"]
.getService(Components.interfaces.nsIStyleSheetService),
Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
];
})();
function set_user_sheet(sheeturi)
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var uri = gIOService.newURI(sheeturi, null, null);
gStyleSheetService.loadAndRegisterSheet(uri, gStyleSheetService.USER_SHEET);
}
function remove_user_sheet(sheeturi)
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var uri = gIOService.newURI(sheeturi, null, null);
gStyleSheetService.unregisterSheet(uri, gStyleSheetService.USER_SHEET);
}
function run()
{
var iframe = document.getElementById("iframe");
var subdoc = iframe.contentDocument;
var subwin = iframe.contentWindow;
var cs = subwin.getComputedStyle(subdoc.getElementById("display"), "");
var zIndexCounter = 0;
function test_document_rule(urltests, shouldapply)
{
var zIndex = ++zIndexCounter;
var rule = "@-moz-document " + urltests +
" { #display { z-index: " + zIndex + " } }";
var sheeturi = "data:text/css," + encodeURI(rule);
set_user_sheet(sheeturi);
if (shouldapply) {
is(cs.zIndex, zIndex,
"@-moz-document " + urltests +
" should apply to this document");
} else {
is(cs.zIndex, "auto",
"@-moz-document " + urltests +
" should NOT apply to this document");
}
remove_user_sheet(sheeturi);
}
test_document_rule("domain(mochi.test)", true);
test_document_rule("domain(\"mochi.test\")", true);
test_document_rule("domain('mochi.test')", true);
test_document_rule("domain('test')", true);
test_document_rule("domain(.test)", false);
test_document_rule("domain('.test')", false);
test_document_rule("domain('ochi.test')", false);
test_document_rule("domain(ochi.test)", false);
test_document_rule("url-prefix(http://moch)", true);
test_document_rule("url-prefix(http://och)", false);
test_document_rule("url-prefix(http://mochi.test)", true);
test_document_rule("url-prefix(http://mochi.test:88)", true);
test_document_rule("url-prefix(http://mochi.test:8888)", true);
test_document_rule("url-prefix(http://mochi.test:8888/)", true);
test_document_rule("url-prefix('http://mochi.test:8888/tests/layout/style/test/chrome/moz_document_helper.html')", true);
test_document_rule("url-prefix('http://mochi.test:8888/tests/layout/style/test/chrome/moz_document_helper.htmlx')", false);
test_document_rule("url(http://mochi.test:8888/)", false);
test_document_rule("url('http://mochi.test:8888/tests/layout/style/test/chrome/moz_document_helper.html')", true);
test_document_rule("url('http://mochi.test:8888/tests/layout/style/test/chrome/moz_document_helper.htmlx')", false);
test_document_rule("regexp(.*ochi.*)", false); // syntax error
test_document_rule("regexp('.*ochi.*')", true);
test_document_rule("regexp('ochi.*')", false);
test_document_rule("regexp('.*ochi')", false);
test_document_rule("regexp('http:.*ochi.*')", true);
test_document_rule("regexp('http:.*ochi')", false);
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>