Commit c29b0a96 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Add `CheckObjectCoercible` for the `String.prototype` HTML methods

Contributed by Mathias Bynens <mathiasb@opera.com>.

TEST=mjsunit/es6/string-html
BUG=v8:2218
LOG=Y
R=yangguo@chromium.org

Review URL: https://codereview.chromium.org/422543003

Patch from Mathias Bynens <mathias@qiwi.be>.

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22938 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent d07203bc
......@@ -706,7 +706,7 @@ function StringSubstring(start, end) {
}
// This is not a part of ECMA-262.
// ES6 draft, revision 26 (2014-07-18), section B.2.3.1
function StringSubstr(start, n) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.substr");
......@@ -827,7 +827,7 @@ function StringFromCharCode(code) {
}
// Helper function for very basic XSS protection.
// ES6 draft, revision 26 (2014-07-18), section B.2.3.2.1
function HtmlEscape(str) {
return TO_STRING_INLINE(str).replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
......@@ -836,69 +836,93 @@ function HtmlEscape(str) {
}
// Compatibility support for KJS.
// Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js.
function StringLink(s) {
return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.2
function StringAnchor(name) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.anchor");
return "<a name=\"" + HtmlEscape(name) + "\">" + this + "</a>";
}
function StringFontcolor(color) {
return "<font color=\"" + HtmlEscape(color) + "\">" + this + "</font>";
}
function StringFontsize(size) {
return "<font size=\"" + HtmlEscape(size) + "\">" + this + "</font>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.3
function StringBig() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.big");
return "<big>" + this + "</big>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.4
function StringBlink() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.blink");
return "<blink>" + this + "</blink>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.5
function StringBold() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.bold");
return "<b>" + this + "</b>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.6
function StringFixed() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fixed");
return "<tt>" + this + "</tt>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.7
function StringFontcolor(color) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontcolor");
return "<font color=\"" + HtmlEscape(color) + "\">" + this + "</font>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.8
function StringFontsize(size) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontsize");
return "<font size=\"" + HtmlEscape(size) + "\">" + this + "</font>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.9
function StringItalics() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.italics");
return "<i>" + this + "</i>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.10
function StringLink(s) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.link");
return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.11
function StringSmall() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.small");
return "<small>" + this + "</small>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.12
function StringStrike() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.strike");
return "<strike>" + this + "</strike>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.13
function StringSub() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.sub");
return "<sub>" + this + "</sub>";
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.14
function StringSup() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup");
return "<sup>" + this + "</sup>";
}
......
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests taken from:
// http://mathias.html5.org/tests/javascript/string/
assertEquals('_'.anchor('b'), '<a name="b">_</a>');
//assertEquals('<'.anchor('<'), '<a name="<"><</a>'); // #2217
assertEquals('_'.anchor(0x2A), '<a name="42">_</a>');
assertEquals('_'.anchor('\x22'), '<a name="&quot;">_</a>');
assertEquals(String.prototype.anchor.call(0x2A, 0x2A), '<a name="42">42</a>');
assertThrows(function() {
String.prototype.anchor.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.anchor.call(null);
}, TypeError);
assertEquals(String.prototype.anchor.length, 1);
assertEquals('_'.big(), '<big>_</big>');
assertEquals('<'.big(), '<big><</big>');
assertEquals(String.prototype.big.call(0x2A), '<big>42</big>');
assertThrows(function() {
String.prototype.big.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.big.call(null);
}, TypeError);
assertEquals(String.prototype.big.length, 0);
assertEquals('_'.blink(), '<blink>_</blink>');
assertEquals('<'.blink(), '<blink><</blink>');
assertEquals(String.prototype.blink.call(0x2A), '<blink>42</blink>');
assertThrows(function() {
String.prototype.blink.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.blink.call(null);
}, TypeError);
assertEquals(String.prototype.blink.length, 0);
assertEquals('_'.bold(), '<b>_</b>');
assertEquals('<'.bold(), '<b><</b>');
assertEquals(String.prototype.bold.call(0x2A), '<b>42</b>');
assertThrows(function() {
String.prototype.bold.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.bold.call(null);
}, TypeError);
assertEquals(String.prototype.bold.length, 0);
assertEquals('_'.fixed(), '<tt>_</tt>');
assertEquals('<'.fixed(), '<tt><</tt>');
assertEquals(String.prototype.fixed.call(0x2A), '<tt>42</tt>');
assertThrows(function() {
String.prototype.fixed.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.fixed.call(null);
}, TypeError);
assertEquals(String.prototype.fixed.length, 0);
assertEquals('_'.fontcolor('b'), '<font color="b">_</font>');
//assertEquals('<'.fontcolor('<'), '<font color="<"><</font>'); // #2217
assertEquals('_'.fontcolor(0x2A), '<font color="42">_</font>');
assertEquals('_'.fontcolor('\x22'), '<font color="&quot;">_</font>');
assertEquals(String.prototype.fontcolor.call(0x2A, 0x2A),
'<font color="42">42</font>');
assertThrows(function() {
String.prototype.fontcolor.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.fontcolor.call(null);
}, TypeError);
assertEquals(String.prototype.fontcolor.length, 1);
assertEquals('_'.fontsize('b'), '<font size="b">_</font>');
//assertEquals('<'.fontsize('<'), '<font size="<"><</font>'); // #2217
assertEquals('_'.fontsize(0x2A), '<font size="42">_</font>');
assertEquals('_'.fontsize('\x22'), '<font size="&quot;">_</font>');
assertEquals(String.prototype.fontsize.call(0x2A, 0x2A),
'<font size="42">42</font>');
assertThrows(function() {
String.prototype.fontsize.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.fontsize.call(null);
}, TypeError);
assertEquals(String.prototype.fontsize.length, 1);
assertEquals('_'.italics(), '<i>_</i>');
assertEquals('<'.italics(), '<i><</i>');
assertEquals(String.prototype.italics.call(0x2A), '<i>42</i>');
assertThrows(function() {
String.prototype.italics.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.italics.call(null);
}, TypeError);
assertEquals(String.prototype.italics.length, 0);
assertEquals('_'.link('b'), '<a href="b">_</a>');
//assertEquals('<'.link('<'), '<a href="<"><</a>'); // #2217
assertEquals('_'.link(0x2A), '<a href="42">_</a>');
assertEquals('_'.link('\x22'), '<a href="&quot;">_</a>');
assertEquals(String.prototype.link.call(0x2A, 0x2A), '<a href="42">42</a>');
assertThrows(function() {
String.prototype.link.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.link.call(null);
}, TypeError);
assertEquals(String.prototype.link.length, 1);
assertEquals('_'.small(), '<small>_</small>');
assertEquals('<'.small(), '<small><</small>');
assertEquals(String.prototype.small.call(0x2A), '<small>42</small>');
assertThrows(function() {
String.prototype.small.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.small.call(null);
}, TypeError);
assertEquals(String.prototype.small.length, 0);
assertEquals('_'.strike(), '<strike>_</strike>');
assertEquals('<'.strike(), '<strike><</strike>');
assertEquals(String.prototype.strike.call(0x2A), '<strike>42</strike>');
assertThrows(function() {
String.prototype.strike.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.strike.call(null);
}, TypeError);
assertEquals(String.prototype.strike.length, 0);
assertEquals('_'.sub(), '<sub>_</sub>');
assertEquals('<'.sub(), '<sub><</sub>');
assertEquals(String.prototype.sub.call(0x2A), '<sub>42</sub>');
assertThrows(function() {
String.prototype.sub.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.sub.call(null);
}, TypeError);
assertEquals(String.prototype.sub.length, 0);
assertEquals('_'.sup(), '<sup>_</sup>');
assertEquals('<'.sup(), '<sup><</sup>');
assertEquals(String.prototype.sup.call(0x2A), '<sup>42</sup>');
assertThrows(function() {
String.prototype.sup.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.sup.call(null);
}, TypeError);
assertEquals(String.prototype.sup.length, 0);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment