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 "core/css/CSSOMUtils.h"
33
34 #include "wtf/HexNumber.h"
35 #include "wtf/text/StringBuilder.h"
36
37 namespace WebCore {
38
serializeCharacter(UChar32 c,StringBuilder & appendTo)39 void serializeCharacter(UChar32 c, StringBuilder& appendTo)
40 {
41 appendTo.append('\\');
42 appendTo.append(c);
43 }
44
serializeCharacterAsCodePoint(UChar32 c,StringBuilder & appendTo)45 void serializeCharacterAsCodePoint(UChar32 c, StringBuilder& appendTo)
46 {
47 appendTo.append('\\');
48 appendUnsignedAsHex(c, appendTo, Lowercase);
49 appendTo.append(' ');
50 }
51
serializeIdentifier(const String & identifier,StringBuilder & appendTo)52 void serializeIdentifier(const String& identifier, StringBuilder& appendTo)
53 {
54 bool isFirst = true;
55 bool isSecond = false;
56 bool isFirstCharHyphen = false;
57 unsigned index = 0;
58 while (index < identifier.length()) {
59 UChar32 c = identifier.characterStartingAt(index);
60 index += U16_LENGTH(c);
61
62 if (c <= 0x1f || (0x30 <= c && c <= 0x39 && (isFirst || (isSecond && isFirstCharHyphen))))
63 serializeCharacterAsCodePoint(c, appendTo);
64 else if (c == 0x2d && isSecond && isFirstCharHyphen)
65 serializeCharacter(c, appendTo);
66 else if (0x80 <= c || c == 0x2d || c == 0x5f || (0x30 <= c && c <= 0x39) || (0x41 <= c && c <= 0x5a) || (0x61 <= c && c <= 0x7a))
67 appendTo.append(c);
68 else
69 serializeCharacter(c, appendTo);
70
71 if (isFirst) {
72 isFirst = false;
73 isSecond = true;
74 isFirstCharHyphen = (c == 0x2d);
75 } else if (isSecond) {
76 isSecond = false;
77 }
78 }
79 }
80
serializeString(const String & string,StringBuilder & appendTo)81 void serializeString(const String& string, StringBuilder& appendTo)
82 {
83 appendTo.append('\"');
84
85 unsigned index = 0;
86 while (index < string.length()) {
87 UChar32 c = string.characterStartingAt(index);
88 index += U16_LENGTH(c);
89 if (c <= 0x1f)
90 serializeCharacterAsCodePoint(c, appendTo);
91 else if (c == 0x22 || c == 0x5c)
92 serializeCharacter(c, appendTo);
93 else
94 appendTo.append(c);
95 }
96
97 appendTo.append('\"');
98 }
99
100 } // namespace WebCore
101