initial import
[vuplus_webkit] / Source / WebCore / css / CSSOMUtils.cpp
1 /*
2  * Copyright (c) 2010 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "CSSOMUtils.h"
33
34 #include <wtf/HexNumber.h>
35 #include <wtf/text/StringBuilder.h>
36
37 namespace WebCore {
38
39 static void appendCharacter(UChar32 c, StringBuilder& appendTo)
40 {
41     if (U16_LENGTH(c) == 1)
42         appendTo.append(static_cast<UChar>(c));
43     else {
44         appendTo.append(U16_LEAD(c));
45         appendTo.append(U16_TRAIL(c));
46     }
47 }
48
49 void serializeCharacter(UChar32 c, StringBuilder& appendTo)
50 {
51     appendTo.append('\\');
52     appendCharacter(c, appendTo);
53 }
54
55 void serializeCharacterAsCodePoint(UChar32 c, StringBuilder& appendTo)
56 {
57     appendTo.append('\\');
58     appendUnsignedAsHex(c, appendTo, Lowercase);
59     appendTo.append(' ');
60 }
61
62 void serializeIdentifier(const String& identifier, String& appendTo)
63 {
64     StringBuilder addend;
65     serializeIdentifier(identifier, addend);
66     appendTo.append(addend.toString());
67 }
68
69 void serializeIdentifier(const String& identifier, StringBuilder& appendTo)
70 {
71     bool isFirst = true;
72     bool isSecond = false;
73     bool isFirstCharHyphen = false;
74     unsigned index = 0;
75     while (index < identifier.length()) {
76         UChar32 c = identifier.characterStartingAt(index);
77         index += U16_LENGTH(c);
78
79         if (c <= 0x1f || (0x30 <= c && c <= 0x39 && (isFirst || (isSecond && isFirstCharHyphen))))
80             serializeCharacterAsCodePoint(c, appendTo);
81         else if (c == 0x2d && isSecond && isFirstCharHyphen)
82             serializeCharacter(c, appendTo);
83         else if (0x80 <= c || c == 0x2d || c == 0x5f || (0x30 <= c && c <= 0x39) || (0x41 <= c && c <= 0x5a) || (0x61 <= c && c <= 0x7a))
84             appendCharacter(c, appendTo);
85         else
86             serializeCharacter(c, appendTo);
87
88         if (isFirst) {
89             isFirst = false;
90             isSecond = true;
91             isFirstCharHyphen = (c == 0x2d);
92         } else if (isSecond) {
93             isSecond = false;
94         }
95     }
96 }
97
98 void serializeString(const String& string, String& appendTo)
99 {
100     StringBuilder addend;
101     serializeString(string, addend);
102     appendTo.append(addend.toString());
103 }
104
105 void serializeString(const String& string, StringBuilder& appendTo)
106 {
107     appendTo.append('\"');
108
109     unsigned index = 0;
110     while (index < string.length()) {
111         UChar32 c = string.characterStartingAt(index);
112         index += U16_LENGTH(c);
113         if (c <= 0x1f)
114             serializeCharacterAsCodePoint(c, appendTo);
115         else if (c == 0x22 || c == 0x5c)
116             serializeCharacter(c, appendTo);
117         else
118             appendCharacter(c, appendTo);
119     }
120
121     appendTo.append('\"');
122 }
123
124 } // namespace WebCore