1 /* 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef PreloadScanner_h 27 #define PreloadScanner_h 28 29 #include "AtomicString.h" 30 #include "SegmentedString.h" 31 #include <wtf/Noncopyable.h> 32 #include <wtf/Vector.h> 33 34 namespace WebCore { 35 36 class CachedResource; 37 class CachedResourceClient; 38 class Document; 39 40 class PreloadScanner : public Noncopyable { 41 public: 42 PreloadScanner(Document*); 43 ~PreloadScanner(); 44 void begin(); 45 void write(const SegmentedString&); 46 void end(); 47 bool inProgress() const { return m_inProgress; } 48 49 bool scanningBody() const; 50 51 static unsigned consumeEntity(SegmentedString&, bool& notEnoughCharacters); 52 53 private: 54 void tokenize(const SegmentedString&); 55 void reset(); 56 57 void emitTag(); 58 void emitCharacter(UChar); 59 60 void tokenizeCSS(UChar); 61 void emitCSSRule(); 62 63 void processAttribute(); 64 65 66 void clearLastCharacters(); 67 void rememberCharacter(UChar); 68 bool lastCharactersMatch(const char*, unsigned count) const; 69 70 bool m_inProgress; 71 SegmentedString m_source; 72 73 enum State { 74 Data, 75 EntityData, 76 TagOpen, 77 CloseTagOpen, 78 TagName, 79 BeforeAttributeName, 80 AttributeName, 81 AfterAttributeName, 82 BeforeAttributeValue, 83 AttributeValueDoubleQuoted, 84 AttributeValueSingleQuoted, 85 AttributeValueUnquoted, 86 EntityInAttributeValue, 87 BogusComment, 88 MarkupDeclarationOpen, 89 CommentStart, 90 CommentStartDash, 91 Comment, 92 CommentEndDash, 93 CommentEnd 94 }; 95 State m_state; 96 bool m_escape; 97 enum ContentModel { 98 PCDATA, 99 RCDATA, 100 CDATA, 101 PLAINTEXT 102 }; 103 ContentModel m_contentModel; 104 unsigned m_commentPos; 105 State m_stateBeforeEntityInAttributeValue; 106 107 static const unsigned lastCharactersBufferSize = 8; 108 UChar m_lastCharacters[lastCharactersBufferSize]; 109 unsigned m_lastCharacterIndex; 110 111 bool m_closeTag; 112 Vector<UChar, 32> m_tagName; 113 Vector<UChar, 32> m_attributeName; 114 Vector<UChar> m_attributeValue; 115 AtomicString m_lastStartTag; 116 117 String m_urlToLoad; 118 String m_charset; 119 bool m_linkIsStyleSheet; 120 121 enum CSSState { 122 CSSInitial, 123 CSSMaybeComment, 124 CSSComment, 125 CSSMaybeCommentEnd, 126 CSSRuleStart, 127 CSSRule, 128 CSSAfterRule, 129 CSSRuleValue, 130 CSSAfterRuleValue 131 }; 132 CSSState m_cssState; 133 Vector<UChar, 16> m_cssRule; 134 Vector<UChar> m_cssRuleValue; 135 136 double m_timeUsed; 137 138 bool m_bodySeen; 139 Document* m_document; 140 }; 141 142 } 143 144 #endif 145