15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2010 Apple Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer in the 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * documentation and/or other materials provided with the distribution. 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef HTMLParserIdioms_h 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define HTMLParserIdioms_h 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/QualifiedName.h" 2951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/Decimal.h" 30591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/Forward.h" 31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/text/WTFString.h" 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore { 345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Space characters as defined by the HTML specification. 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool isHTMLSpace(UChar); 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool isHTMLLineBreak(UChar); 385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool isNotHTMLSpace(UChar); 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch// Strip leading and trailing whitespace as defined by the HTML specification. 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)String stripLeadingAndTrailingHTMLSpaces(const String&); 42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template<size_t inlineCapacity> 43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)String stripLeadingAndTrailingHTMLSpaces(const Vector<UChar, inlineCapacity>& vector) 44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return stripLeadingAndTrailingHTMLSpaces(StringImpl::create8BitIfPossible(vector)); 46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// An implementation of the HTML specification's algorithm to convert a number to a string for number and range types. 495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)String serializeForNumberType(const Decimal&); 505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)String serializeForNumberType(double); 515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Convert the specified string to a decimal/double. If the conversion fails, the return value is fallback value or NaN if not specified. 535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Leading or trailing illegal characters cause failure, as does passing an empty string. 545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// The double* parameter may be 0 to check if the string can be parsed without getting the result. 5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)Decimal parseToDecimalForNumberType(const String&, const Decimal& fallbackValue = Decimal::nan()); 5651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)double parseToDoubleForNumberType(const String&, double fallbackValue = std::numeric_limits<double>::quiet_NaN()); 575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers 595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool parseHTMLInteger(const String&, int&); 605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers 625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool parseHTMLNonNegativeInteger(const String&, unsigned int&); 635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Inline implementations of some of the functions declared above. 655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)template<typename CharType> 6706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)inline bool isHTMLSpace(CharType character) 685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Histogram from Apple's page load test combined with some ad hoc browsing some other test suites. 705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 82%: 216330 non-space characters, all > U+0020 725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 11%: 30017 plain space characters, U+0020 735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 5%: 12099 newline characters, U+000A 745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 2%: 5346 tab characters, U+0009 755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // No other characters seen. No U+000C or U+000D, and no other control characters. 775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Accordingly, we check for non-spaces first, then space, then newline, then tab, then the other characters. 785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return character <= ' ' && (character == ' ' || character == '\n' || character == '\t' || character == '\r' || character == '\f'); 805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 8206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)template<typename CharType> 8306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)inline bool isHTMLSpaceOrComma(CharType character) 8406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles){ 8506f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) return isHTMLSpace<CharType>(character) || character == ','; 8606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)} 8706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) 885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool isHTMLLineBreak(UChar character) 895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return character <= '\r' && (character == '\n' || character == '\r'); 915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 9306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)template<typename CharType> 9406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)inline bool isNotHTMLSpace(CharType character) 955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 9606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) return !isHTMLSpace<CharType>(character); 975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 99926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)bool threadSafeMatch(const QualifiedName&, const QualifiedName&); 100e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)bool threadSafeMatch(const String&, const QualifiedName&); 101e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) 102e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)StringImpl* findStringIfStatic(const UChar* characters, unsigned length); 103e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) 104e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)enum CharacterWidth { 105e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) Likely8Bit, 106e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) Force8Bit, 107e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) Force16Bit 108e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)}; 109e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) 110e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)template<size_t inlineCapacity> 111e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)static String attemptStaticStringCreation(const Vector<UChar, inlineCapacity>& vector, CharacterWidth width) 112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 113e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) String string(findStringIfStatic(vector.data(), vector.size())); 114e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) if (string.impl()) 115e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) return string; 116e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) if (width == Likely8Bit) 117e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) string = StringImpl::create8BitIfPossible(vector); 118e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) else if (width == Force8Bit) 119e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) string = String::make8BitFrom16BitSource(vector); 120e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) else 121e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) string = String(vector); 122e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) 123e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles) return string; 124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 128