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