std::string WIntValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WIntValidator(" << isMandatory() << ','; if (bottom_ != std::numeric_limits<int>::min()) js << bottom_; else js << "null"; js << ','; if (top_ != std::numeric_limits<int>::max()) js << top_; else js << "null"; js << "," << WWebWidget::jsStringLiteral(WLocale::currentLocale() .groupSeparator()) << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidNotANumberText().jsStringLiteral() << ',' << invalidTooSmallText().jsStringLiteral() << ',' << invalidTooLargeText().jsStringLiteral() << ");"; return js.str(); }
void WCssTheme::applyValidationStyle(WWidget *widget, const Wt::WValidator::Result& validation, WFlags<ValidationStyleFlag> styles) const { WApplication *app = WApplication::instance(); LOAD_JAVASCRIPT(app, "js/CssThemeValidate.js", "validate", wtjs1); LOAD_JAVASCRIPT(app, "js/CssThemeValidate.js", "setValidationState", wtjs2); if (app->environment().ajax()) { WStringStream js; js << WT_CLASS ".setValidationState(" << widget->jsRef() << "," << (validation.state() == WValidator::Valid ? 1 : 0) << "," << validation.message().jsStringLiteral() << "," << styles.value() << ");"; widget->doJavaScript(js.str()); } else { bool validStyle = (validation.state() == WValidator::Valid) && (styles & ValidationValidStyle); bool invalidStyle = (validation.state() != WValidator::Valid) && (styles & ValidationInvalidStyle); widget->toggleStyleClass("Wt-valid", validStyle); widget->toggleStyleClass("Wt-invalid", invalidStyle); } }
std::string urlDecode(const std::string &text) { WStringStream result; for (unsigned i = 0; i < text.length(); ++i) { char c = text[i]; if (c == '+') { result << ' '; } else if (c == '%' && i + 2 < text.length()) { std::string h = text.substr(i + 1, 2); char *e = 0; int hval = std::strtol(h.c_str(), &e, 16); if (*e == 0) { result << (char)hval; i += 2; } else // not a proper %XX with XX hexadecimal format result << c; } else result << c; } return result.str(); }
bool XSSFilterRemoveScript(WString& text) { if (text.empty()) return true; std::string result = "<span>" + text.toUTF8() + "</span>"; char *ctext = const_cast<char *>(result.c_str()); // Shhht it's okay ! try { xml_document<> doc; doc.parse<parse_comment_nodes | parse_validate_closing_tags | parse_validate_utf8 | parse_xhtml_entity_translation>(ctext); XSSSanitize(doc.first_node()); WStringStream out; print(out.back_inserter(), *doc.first_node(), print_no_indenting); result = out.str(); } catch (parse_error& e) { LOG_ERROR("Error reading XHTML string: " << e.what()); return false; } if (result.length() < 13) result.clear(); else result = result.substr(6, result.length() - 13); text = WString::fromUTF8(result); return true; }
void WPopupMenu::prepareRender(WApplication *app) { if (app->environment().agentIsIE()) { doJavaScript(jsRef() + ".lastChild.style.width=" + jsRef() + ".lastChild.offsetWidth + 'px';"); } // FIXME: we should really also prepareRender() of submenus when shown... if (!cancel_.isConnected()) { LOAD_JAVASCRIPT(app, "js/WPopupMenu.js", "WPopupMenu", wtjs1); std::vector<WPopupMenu *> subMenus; getSubMenus(subMenus); WStringStream s; s << "new " WT_CLASS ".WPopupMenu(" << app->javaScriptClass() << ',' << jsRef() << ',' << autoHideDelay_ << ",["; for (unsigned i = 0; i < subMenus.size(); ++i) { if (i != 0) s << ','; s << WWebWidget::jsStringLiteral(subMenus[i]->id()); } s << "]);"; setJavaScriptMember(" WPopupMenu", s.str()); cancel_.connect(this, &WPopupMenu::done); } }
std::string WLengthValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WLengthValidator(" << isMandatory() << ','; if (minLength_ != 0) js << minLength_; else js << "null"; js << ','; if (maxLength_ != std::numeric_limits<int>::max()) js << maxLength_; else js << "null"; js << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidTooShortText().jsStringLiteral() << ',' << invalidTooLongText().jsStringLiteral() << ");"; return js.str(); }
std::string WRegExpValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WRegExpValidator(" << isMandatory() << ','; if (regexp_) { js << WWebWidget::jsStringLiteral(regexp_->pattern()) << ",'"; #ifndef WT_TARGET_JAVA WFlags<RegExpFlag> flags = regexp_->flags(); #else int flags = regexp_->flags(); #endif if (flags & MatchCaseInsensitive) js << 'i'; js << '\''; } else js << "null, null"; js << ',' << WWebWidget::jsStringLiteral(invalidBlankText()) << ',' << WWebWidget::jsStringLiteral(invalidNoMatchText()) << ");"; return js.str(); }
const std::string WColor::cssText(bool withAlpha) const { if (default_) return std::string(); else { if (!name_.empty()) return name_.toUTF8(); else { WStringStream s; #ifndef WT_TARGET_JAVA char buf[30]; #else char *buf; #endif if (alpha_ != 255 && withAlpha) { s << "rgba(" << red_ << ',' << green_ << ',' << blue_ << ',' << Utils::round_css_str(alpha_ / 255., 2, buf) << ')'; } else s << "rgb(" << red_ << ',' << green_ << ',' << blue_ << ')'; return s.c_str(); } } }
void EncodeRefs(WString& text, WFlags<RefEncoderOption> options) { if (text.empty()) return; std::string result = "<span>" + text.toUTF8() + "</span>"; char *ctext = const_cast<char *>(result.c_str()); // Shhht it's okay ! WApplication *app = WApplication::instance(); try { xml_document<> doc; doc.parse<parse_comment_nodes | parse_validate_closing_tags | parse_validate_utf8 | parse_xhtml_entity_translation>(ctext); EncodeRefs(doc.first_node(), app, options); WStringStream out; print(out.back_inserter(), *doc.first_node(), print_no_indenting); result = out.str(); } catch (parse_error& e) { LOG_ERROR("Error reading XHTML string: " << e.what()); return; } if (result.length() < 13) result.clear(); else result = result.substr(6, result.length() - 13); text = WString::fromUTF8(result); }
static std::string replaceUrlInStyle(std::string& style, WApplication *app) { boost::regex re("url\\((.*//.*)\\)", boost::regex::perl | boost::regex::icase); boost::sregex_iterator i(style.begin(), style.end(), re); boost::sregex_iterator end; WStringStream result; std::size_t pos = 0; for (; i != end; ++i) { result << style.substr(pos, i->position(1) - pos); std::string url = style.substr(i->position(1), i->length(1)); boost::algorithm::trim(url); if (url.length() > 2) if (url[0] == '\'' || url[1] == '"') url = url.substr(1, url.length() - 2); result << WWebWidget::jsStringLiteral(app->encodeUntrustedUrl(url), '\''); pos = i->position(1) + i->length(1); } result << style.substr(pos); return result.str(); }
std::string WPointF::jsValue() const { char buf[30]; WStringStream ss; ss << '['; ss << Utils::round_js_str(x_, 3, buf) << ','; ss << Utils::round_js_str(y_, 3, buf) << ']'; return ss.str(); }
void WWidgetCanvasPainter::createContents(DomElement *result, WPaintDevice *device) { std::string wstr = boost::lexical_cast<std::string>(widget_->renderWidth_); std::string hstr = boost::lexical_cast<std::string>(widget_->renderHeight_); result->setProperty(PropertyStylePosition, "relative"); result->setProperty(PropertyStyleOverflowX, "hidden"); result->setProperty(PropertyStyleOverflowY, "hidden"); DomElement *canvas = DomElement::createNew(DomElement_CANVAS); canvas->setId('c' + widget_->id()); canvas->setProperty(PropertyStyleDisplay, "block"); canvas->setAttribute("width", wstr); canvas->setAttribute("height", hstr); result->addChild(canvas); widget_->sizeChanged_ = false; WCanvasPaintDevice *canvasDevice = dynamic_cast<WCanvasPaintDevice *>(device); DomElement *text = 0; if (canvasDevice->textMethod() == WCanvasPaintDevice::DomText) { text = DomElement::createNew(DomElement_DIV); text->setId('t' + widget_->id()); text->setProperty(PropertyStylePosition, "absolute"); text->setProperty(PropertyStyleZIndex, "1"); text->setProperty(PropertyStyleTop, "0px"); text->setProperty(PropertyStyleLeft, "0px"); } DomElement *el = text ? text : result; bool hasJsObjects = widget_->jsObjects_.size() > 0; if (hasJsObjects) { WStringStream ss; WApplication *app = WApplication::instance(); ss << "new " WT_CLASS ".WPaintedWidget(" << app->javaScriptClass() << "," << widget_->jsRef() << ");"; widget_->jsObjects_.updateJs(ss); el->callJavaScript(ss.str()); } canvasDevice->render('c' + widget_->id(), el); if (hasJsObjects) { WStringStream ss; ss << widget_->objJsRef() << ".repaint=function(){"; ss << canvasDevice->recordedJs_.str(); ss << "};"; el->callJavaScript(ss.str()); } if (text) result->addChild(text); delete device; }
std::string WImage::setAreaCoordsJS() { WStringStream ss; if (!targetJS_.empty()) { ss << "jQuery.data(" << jsRef() << ", 'obj').setAreaCoordsJSON(" << updateAreaCoordsJSON() << ");"; } return ss.str(); }
WString toWString(wchar_t val, unsigned short width, char fill, std::ios::fmtflags flags) { WStringStream stream; stream.width(width); stream.fill(fill); if (flags) stream.setf(flags); stream << val; return stream.str(); }
std::string WBrush::jsValue() const { WStringStream ss; ss << "{\"color\":[" << color_.red() << "," << color_.green() << "," << color_.blue() << "," << color_.alpha() << "]}"; return ss.str(); }
void AuthModel::updateThrottling(WInteractWidget *button) { if (passwordAuth() && passwordAuth()->attemptThrottlingEnabled()) { WStringStream s; s << button->jsRef() << ".wtThrottle.reset(" << throttlingDelay_ << ");"; button->doJavaScript(s.str()); } }
const std::string Money::toString() const { WStringStream ans; if(cents() > 9) ans << value() << "." << cents(); //todo use formater. else ans << value() << ".0" << cents(); //todo use formater. return ans.str(); }
void WPopupWidget::setTransient(bool isTransient, int autoHideDelay) { transient_ = isTransient; autoHideDelay_ = autoHideDelay; if (isRendered()) { WStringStream ss; ss << "jQuery.data(" << jsRef() << ", 'popup').setTransient(" << transient_ << ',' << autoHideDelay_ << ");"; doJavaScript(ss.str()); } }
void WImage::defineJavaScript() { WApplication *app = WApplication::instance(); LOAD_JAVASCRIPT(app, "js/WImage.js", "WImage", wtjs1); WStringStream ss; ss << "new " WT_CLASS ".WImage(" << app->javaScriptClass() << "," << jsRef() << "," << targetJS_ << ");"; doJavaScript(ss.str()); }
const std::string WFont::cssText(bool combined) const { WStringStream result; if (combined) { std::string s; s = cssStyle(false); if (!s.empty()) result << s << ' '; s = cssVariant(false); if (!s.empty()) result << s << ' '; s = cssWeight(false); if (!s.empty()) result << s << ' '; result << cssSize(true) << ' '; s = cssFamily(true); if (!s.empty()) result << s << ' '; else result << s << " inherit"; } else { std::string s; s = cssSize(false); if (!s.empty()) result << "font-size: " << s << ";"; s = cssStyle(false); if (!s.empty()) result << "font-style: " << s << ";"; s = cssVariant(false); if (!s.empty()) result << "font-variant: " << s << ";"; s = cssWeight(false); if (!s.empty()) result << "font-weight: " << s << ";"; // Last because of workaround in WVmlImage that searches for a ',' s = cssFamily(false); if (!s.empty()) result << "font-family: " << s << ";"; } return result.str(); }
void WMediaPlayer::playerDo(const std::string& method, const std::string& args) { WStringStream ss; ss << ".jPlayer('" << method << '\''; if (!args.empty()) { ss << ',' << args; } ss << ')'; playerDoRaw(ss.str()); }
std::string WTransform::jsValue() const { char buf[30]; WStringStream ss; ss << '['; ss << Utils::round_js_str(m_[0], 3, buf) << ','; ss << Utils::round_js_str(m_[2], 3, buf) << ','; ss << Utils::round_js_str(m_[1], 3, buf) << ','; ss << Utils::round_js_str(m_[3], 3, buf) << ','; ss << Utils::round_js_str(m_[4], 3, buf) << ','; ss << Utils::round_js_str(m_[5], 3, buf) << ']'; return ss.str(); }
WString toWString(const Matrix3& val) { WStringStream stream; stream << val[0][0] << L" " << val[0][1] << L" " << val[0][2] << L" " << val[1][0] << L" " << val[1][1] << L" " << val[1][2] << L" " << val[2][0] << L" " << val[2][1] << L" " << val[2][2]; return stream.str(); }
WPointF WPointF::swapHV(double width) const { WPointF result(width - y(), x()); if (isJavaScriptBound()) { WStringStream ss; char buf[30]; ss << "((function(p){return ["; ss << Utils::round_js_str(width, 3, buf) << " - p[1],p[0]];})(" << jsRef() + "))"; result.assignBinding(*this, ss.str()); } return result; }
void WPopupWidget::defineJavaScript() { WApplication *app = WApplication::instance(); LOAD_JAVASCRIPT(app, "js/WPopupWidget.js", "WPopupWidget", wtjs1); WStringStream jsObj; jsObj << "new " WT_CLASS ".WPopupWidget(" << app->javaScriptClass() << ',' << jsRef() << ',' << transient_ << ',' << autoHideDelay_ << ',' << !isHidden() << ");"; setJavaScriptMember(" WPopupWidget", jsObj.str()); }
std::string WTimeValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WTimeValidator(" << (isMandatory() ? "true" : "false") << ",["; for (unsigned i = 0; i < formats_.size(); ++i) { WTime::RegExpInfo r = WTime::formatToRegExp(formats_[i]); if (i != 0) js << ','; js << "{" << "regexp:" << WWebWidget::jsStringLiteral(r.regexp) << ',' << "getHour:function(results){" << r.hourGetJS << ";}," << "getMinute:function(results){" << r.minuteGetJS << ";}," << "getSecond:function(results){" << r.secGetJS << ";}" << "getMSec:function(results){" << r.msecGetJS << ";}" << "}"; } js << "],"; if (!bottom_.isNull()) js << "new Time(" << bottom_.hour() << ',' << bottom_.minute()-1 << ',' << bottom_.second() << ")"; else js << "null"; js << ','; if (!top_.isNull()) js << "new Time(" << top_.hour() << ',' << top_.minute()-1 << ',' << top_.second() << ")"; else js << "null"; js << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidNotATimeText().jsStringLiteral() << ',' << invalidTooEarlyText().jsStringLiteral() << ',' << invalidTooLateText().jsStringLiteral() << ");"; return js.str(); }
std::string WDateValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WDateValidator(" << isMandatory() << ",["; for (unsigned i = 0; i < formats_.size(); ++i) { WDate::RegExpInfo r = WDate::formatToRegExp(formats_[i]); if (i != 0) js << ','; js << "{" << "regexp:" << WWebWidget::jsStringLiteral(r.regexp) << ',' << "getMonth:function(results){" << r.monthGetJS << ";}," << "getDay:function(results){" << r.dayGetJS << ";}," << "getYear:function(results){" << r.yearGetJS << ";}" << "}"; } js << "],"; if (!bottom_.isNull()) js << "new Date(" << bottom_.year() << ',' << bottom_.month()-1 << ',' << bottom_.day() << ")"; else js << "null"; js << ','; if (!top_.isNull()) js << "new Date(" << top_.year() << ',' << top_.month()-1 << ',' << top_.day() << ")"; else js << "null"; js << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidNotADateText().jsStringLiteral() << ',' << invalidTooEarlyText().jsStringLiteral() << ',' << invalidTooLateText().jsStringLiteral() << ");"; return js.str(); }
void GUIStatusBar::setProject(const WString& name, bool modified) { WStringStream content; content << L"Project: "; if (name.size() > 20) content << name.substr(0, 20) << L"..."; else content << name; if (modified) content << L"*"; mProject->setContent(HString(content.str())); }
std::string WImage::updateAreasJS() { WStringStream ss; if (!targetJS_.empty()) { ss << "(function(){" """var w = " << jsRef() << ";" """if (w) {" "" "var o = jQuery.data(" << jsRef() << ", 'obj');" "" "if (o) { o.updateAreas(); }" """}" "})();"; } return ss.str(); }
void GUIStatusBar::setScene(const WString& name, bool modified) { WStringStream content; content << L"Scene: "; if (name.size() > 15) content << name.substr(0, 15) << L"..."; else content << name; if (modified) content << L"*"; mScene->setContent(HString(content.str())); }