Home | History | Annotate | Download | only in page
      1 /*
      2  * Copyright (C) 2012 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/page/PagePopupClient.h"
     33 
     34 #include "wtf/text/StringBuilder.h"
     35 
     36 namespace WebCore {
     37 
     38 #define addLiteral(literal, writer)    writer.addData(literal, sizeof(literal) - 1)
     39 
     40 void PagePopupClient::addJavaScriptString(const String& str, DocumentWriter& writer)
     41 {
     42     addLiteral("\"", writer);
     43     StringBuilder builder;
     44     builder.reserveCapacity(str.length());
     45     for (unsigned i = 0; i < str.length(); ++i) {
     46         if (str[i] == '\\' || str[i] == '"')
     47             builder.append('\\');
     48         builder.append(str[i]);
     49     }
     50     addString(builder.toString(), writer);
     51     addLiteral("\"", writer);
     52 }
     53 
     54 void PagePopupClient::addProperty(const char* name, const String& value, DocumentWriter& writer)
     55 {
     56     writer.addData(name, strlen(name));
     57     addLiteral(": ", writer);
     58     addJavaScriptString(value, writer);
     59     addLiteral(",\n", writer);
     60 }
     61 
     62 void PagePopupClient::addProperty(const char* name, int value, DocumentWriter& writer)
     63 {
     64     writer.addData(name, strlen(name));
     65     addLiteral(": ", writer);
     66     addString(String::number(value), writer);
     67     addLiteral(",\n", writer);
     68 }
     69 
     70 void PagePopupClient::addProperty(const char* name, unsigned value, DocumentWriter& writer)
     71 {
     72     writer.addData(name, strlen(name));
     73     addLiteral(": ", writer);
     74     addString(String::number(value), writer);
     75     addLiteral(",\n", writer);
     76 }
     77 
     78 void PagePopupClient::addProperty(const char* name, bool value, DocumentWriter& writer)
     79 {
     80     writer.addData(name, strlen(name));
     81     addLiteral(": ", writer);
     82     if (value)
     83         addLiteral("true", writer);
     84     else
     85         addLiteral("false", writer);
     86     addLiteral(",\n", writer);
     87 }
     88 
     89 void PagePopupClient::addProperty(const char* name, const Vector<String>& values, DocumentWriter& writer)
     90 {
     91     writer.addData(name, strlen(name));
     92     addLiteral(": [", writer);
     93     for (unsigned i = 0; i < values.size(); ++i) {
     94         if (i)
     95             addLiteral(",", writer);
     96         addJavaScriptString(values[i], writer);
     97     }
     98     addLiteral("],\n", writer);
     99 }
    100 
    101 void PagePopupClient::addProperty(const char* name, const IntRect& rect, DocumentWriter& writer)
    102 {
    103     writer.addData(name, strlen(name));
    104     addLiteral(": {", writer);
    105     addProperty("x", rect.x(), writer);
    106     addProperty("y", rect.y(), writer);
    107     addProperty("width", rect.width(), writer);
    108     addProperty("height", rect.height(), writer);
    109     addLiteral("},\n", writer);
    110 }
    111 
    112 } // namespace WebCore
    113 
    114