Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: Source/core/dom/Document.cpp

Issue 555813003: Shrink Document::parseQualifiedName a bit. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Document::parseQualifiedName better names Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved. 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved.
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. 8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved.
9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
(...skipping 4189 matching lines...) Expand 10 before | Expand all | Expand 10 after
4200 } 4200 }
4201 4201
4202 const UChar* characters = name.characters16(); 4202 const UChar* characters = name.characters16();
4203 4203
4204 if (isValidNameASCII(characters, length)) 4204 if (isValidNameASCII(characters, length))
4205 return true; 4205 return true;
4206 4206
4207 return isValidNameNonASCII(characters, length); 4207 return isValidNameNonASCII(characters, length);
4208 } 4208 }
4209 4209
4210 enum QualifiedNameStatus {
4211 QNValid,
4212 QNMultipleColons,
4213 QNInvalidStartChar,
4214 QNInvalidChar,
4215 QNEmptyPrefix,
4216 QNEmptyLocalName
4217 };
4218
4219 struct ParseQualifiedNameResult {
4220 QualifiedNameStatus status;
4221 UChar32 character;
4222 ParseQualifiedNameResult() { }
4223 explicit ParseQualifiedNameResult(QualifiedNameStatus status) : status(statu s) { }
4224 ParseQualifiedNameResult(QualifiedNameStatus status, UChar32 character) : st atus(status), character(character) { }
4225 };
4226
4210 template<typename CharType> 4227 template<typename CharType>
4211 static bool parseQualifiedNameInternal(const AtomicString& qualifiedName, const CharType* characters, unsigned length, AtomicString& prefix, AtomicString& local Name, ExceptionState& exceptionState) 4228 static ParseQualifiedNameResult parseQualifiedNameInternal(const AtomicString& q ualifiedName, const CharType* characters, unsigned length, AtomicString& prefix, AtomicString& localName)
4212 { 4229 {
4213 bool nameStart = true; 4230 bool nameStart = true;
4214 bool sawColon = false; 4231 bool sawColon = false;
4215 int colonPos = 0; 4232 int colonPos = 0;
4216 4233
4217 for (unsigned i = 0; i < length;) { 4234 for (unsigned i = 0; i < length;) {
4218 UChar32 c; 4235 UChar32 c;
4219 U16_NEXT(characters, i, length, c) 4236 U16_NEXT(characters, i, length, c)
4220 if (c == ':') { 4237 if (c == ':') {
4221 if (sawColon) { 4238 if (sawColon)
4222 exceptionState.throwDOMException(NamespaceError, "The qualified name provided ('" + qualifiedName + "') contains multiple colons."); 4239 return ParseQualifiedNameResult(QNMultipleColons);
4223 return false; // multiple colons: not allowed
4224 }
4225 nameStart = true; 4240 nameStart = true;
4226 sawColon = true; 4241 sawColon = true;
4227 colonPos = i - 1; 4242 colonPos = i - 1;
4228 } else if (nameStart) { 4243 } else if (nameStart) {
4229 if (!isValidNameStart(c)) { 4244 if (!isValidNameStart(c))
4230 StringBuilder message; 4245 return ParseQualifiedNameResult(QNInvalidStartChar, c);
4231 message.appendLiteral("The qualified name provided ('");
4232 message.append(qualifiedName);
4233 message.appendLiteral("') contains the invalid name-start charac ter '");
4234 message.append(c);
4235 message.appendLiteral("'.");
4236 exceptionState.throwDOMException(InvalidCharacterError, message. toString());
4237 return false;
4238 }
4239 nameStart = false; 4246 nameStart = false;
4240 } else { 4247 } else {
4241 if (!isValidNamePart(c)) { 4248 if (!isValidNamePart(c))
4242 StringBuilder message; 4249 return ParseQualifiedNameResult(QNInvalidChar, c);
4243 message.appendLiteral("The qualified name provided ('");
4244 message.append(qualifiedName);
4245 message.appendLiteral("') contains the invalid character '");
4246 message.append(c);
4247 message.appendLiteral("'.");
4248 exceptionState.throwDOMException(InvalidCharacterError, message. toString());
4249 return false;
4250 }
4251 } 4250 }
4252 } 4251 }
4253 4252
4254 if (!sawColon) { 4253 if (!sawColon) {
4255 prefix = nullAtom; 4254 prefix = nullAtom;
4256 localName = qualifiedName; 4255 localName = qualifiedName;
4257 } else { 4256 } else {
4258 prefix = AtomicString(characters, colonPos); 4257 prefix = AtomicString(characters, colonPos);
4259 if (prefix.isEmpty()) { 4258 if (prefix.isEmpty())
4260 exceptionState.throwDOMException(NamespaceError, "The qualified name provided ('" + qualifiedName + "') has an empty namespace prefix."); 4259 return ParseQualifiedNameResult(QNEmptyPrefix);
4261 return false;
4262 }
4263 int prefixStart = colonPos + 1; 4260 int prefixStart = colonPos + 1;
4264 localName = AtomicString(characters + prefixStart, length - prefixStart) ; 4261 localName = AtomicString(characters + prefixStart, length - prefixStart) ;
4265 } 4262 }
4266 4263
4267 if (localName.isEmpty()) { 4264 if (localName.isEmpty())
4268 exceptionState.throwDOMException(NamespaceError, "The qualified name pro vided ('" + qualifiedName + "') has an empty local name."); 4265 return ParseQualifiedNameResult(QNEmptyLocalName);
4269 return false;
4270 }
4271 4266
4272 return true; 4267 return ParseQualifiedNameResult(QNValid);
4273 } 4268 }
4274 4269
4275 bool Document::parseQualifiedName(const AtomicString& qualifiedName, AtomicStrin g& prefix, AtomicString& localName, ExceptionState& exceptionState) 4270 bool Document::parseQualifiedName(const AtomicString& qualifiedName, AtomicStrin g& prefix, AtomicString& localName, ExceptionState& exceptionState)
4276 { 4271 {
4277 unsigned length = qualifiedName.length(); 4272 unsigned length = qualifiedName.length();
4278 4273
4279 if (!length) { 4274 if (!length) {
4280 exceptionState.throwDOMException(InvalidCharacterError, "The qualified n ame provided is empty."); 4275 exceptionState.throwDOMException(InvalidCharacterError, "The qualified n ame provided is empty.");
4281 return false; 4276 return false;
4282 } 4277 }
4283 4278
4279 ParseQualifiedNameResult returnValue;
4284 if (qualifiedName.is8Bit()) 4280 if (qualifiedName.is8Bit())
4285 return parseQualifiedNameInternal(qualifiedName, qualifiedName.character s8(), length, prefix, localName, exceptionState); 4281 returnValue = parseQualifiedNameInternal(qualifiedName, qualifiedName.ch aracters8(), length, prefix, localName);
4286 return parseQualifiedNameInternal(qualifiedName, qualifiedName.characters16( ), length, prefix, localName, exceptionState); 4282 else
4283 returnValue = parseQualifiedNameInternal(qualifiedName, qualifiedName.ch aracters16(), length, prefix, localName);
4284 if (returnValue.status == QNValid)
4285 return true;
4286
4287 StringBuilder message;
4288 message.appendLiteral("The qualified name provided ('");
4289 message.append(qualifiedName);
4290 message.appendLiteral("') ");
4291
4292 if (returnValue.status == QNMultipleColons) {
4293 message.appendLiteral("contains multiple colons.");
4294 } else if (returnValue.status == QNInvalidStartChar) {
4295 message.appendLiteral("contains the invalid name-start character '");
4296 message.append(returnValue.character);
4297 message.appendLiteral("'.");
4298 } else if (returnValue.status == QNInvalidChar) {
4299 message.appendLiteral("contains the invalid character '");
4300 message.append(returnValue.character);
4301 message.appendLiteral("'.");
4302 } else if (returnValue.status == QNEmptyPrefix) {
4303 message.appendLiteral("has an empty namespace prefix.");
4304 } else {
4305 ASSERT(returnValue.status == QNEmptyLocalName);
4306 message.appendLiteral("has an empty local name.");
4307 }
4308
4309 if (returnValue.status == QNInvalidStartChar || returnValue.status == QNInva lidChar)
4310 exceptionState.throwDOMException(InvalidCharacterError, message.toString ());
4311 else
4312 exceptionState.throwDOMException(NamespaceError, message.toString());
4313 return false;
4287 } 4314 }
4288 4315
4289 void Document::setEncodingData(const DocumentEncodingData& newData) 4316 void Document::setEncodingData(const DocumentEncodingData& newData)
4290 { 4317 {
4291 // It's possible for the encoding of the document to change while we're deco ding 4318 // It's possible for the encoding of the document to change while we're deco ding
4292 // data. That can only occur while we're processing the <head> portion of th e 4319 // data. That can only occur while we're processing the <head> portion of th e
4293 // document. There isn't much user-visible content in the <head>, but there is 4320 // document. There isn't much user-visible content in the <head>, but there is
4294 // the <title> element. This function detects that situation and re-decodes the 4321 // the <title> element. This function detects that situation and re-decodes the
4295 // document's title so that the user doesn't see an incorrectly decoded titl e 4322 // document's title so that the user doesn't see an incorrectly decoded titl e
4296 // in the title bar. 4323 // in the title bar.
(...skipping 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after
5851 using namespace blink; 5878 using namespace blink;
5852 void showLiveDocumentInstances() 5879 void showLiveDocumentInstances()
5853 { 5880 {
5854 WeakDocumentSet& set = liveDocumentSet(); 5881 WeakDocumentSet& set = liveDocumentSet();
5855 fprintf(stderr, "There are %u documents currently alive:\n", set.size()); 5882 fprintf(stderr, "There are %u documents currently alive:\n", set.size());
5856 for (WeakDocumentSet::const_iterator it = set.begin(); it != set.end(); ++it ) { 5883 for (WeakDocumentSet::const_iterator it = set.begin(); it != set.end(); ++it ) {
5857 fprintf(stderr, "- Document %p URL: %s\n", *it, (*it)->url().string().ut f8().data()); 5884 fprintf(stderr, "- Document %p URL: %s\n", *it, (*it)->url().string().ut f8().data());
5858 } 5885 }
5859 } 5886 }
5860 #endif 5887 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698