Skip to content

Commit

Permalink
Add Annex B String.prototype HTML methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 18, 2015
1 parent 7781aad commit c23133d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
31 changes: 31 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@
// Call the constructor.
var result = ES.Call(C, obj, args);
return ES.TypeIsObject(result) ? result : obj;
},

CreateHTML: function (string, tag, attribute, value) {
var S = ES.ToString(string);
var p1 = '<' + tag;
if (attribute !== '') {
var V = ES.toString(value);
var escapedV = V.replace(/"/g, '&quot;');
p1 += ' ' + attribute + '="' + escapedV + '"';
}
var p2 = p1 + '>';
var p3 = p2 + S;
return p3 + '</' + tag + '>';
}
};

Expand Down Expand Up @@ -2488,5 +2501,23 @@
});
}

// Annex B HTML methods
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-additional-properties-of-the-string.prototype-object
defineProperties(String.prototype, {
anchor: function anchor(name) { return ES.CreateHTML(this, 'a', 'name', name); },
big: function big() { return ES.CreateHTML(this, 'big', '', ''); },
blink: function blink() { return ES.CreateHTML(this, 'blink', '', ''); },
bold: function bold() { return ES.CreateHTML(this, 'b', '', ''); },
fixed: function fixed() { return ES.CreateHTML(this, 'tt', '', ''); },
fontcolor: function fontcolor(color) { return ES.CreateHTML(this, 'font', 'color', color); },
fontsize: function fontsize(size) { return ES.CreateHTML(this, 'font', 'size', size); },
italics: function italics() { return ES.CreateHTML(this, 'i', '', ''); },
link: function link(url) { return ES.CreateHTML(this, 'a', 'href', url); },
small: function small(url) { return ES.CreateHTML(this, 'small', '', ''); },
strike: function strike(url) { return ES.CreateHTML(this, 'strike', '', ''); },
sub: function sub(url) { return ES.CreateHTML(this, 'sub', '', ''); },
sup: function sub(url) { return ES.CreateHTML(this, 'sup', '', ''); }
});

return globals;
}));
42 changes: 42 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,48 @@ var runStringTests = function () {
});
});
});

describe('Annex B', function () {
it('has #anchor', function () {
expect('foo'.anchor('bar"baz"')).to.equal('<a name="bar&quot;baz&quot;">foo</a>');
});
it('has #big', function () {
expect('foo'.big()).to.equal('<big>foo</big>');
});
it('has #blink', function () {
expect('foo'.blink()).to.equal('<blink>foo</blink>');
});
it('has #bold', function () {
expect('foo'.bold()).to.equal('<b>foo</b>');
});
it('has #fixed', function () {
expect('foo'.fixed()).to.equal('<tt>foo</tt>');
});
it('has #fontcolor', function () {
expect('foo'.fontcolor('blue"red"green')).to.equal('<font color="blue&quot;red&quot;green">foo</font>');
});
it('has #fontsize', function () {
expect('foo'.fontsize('10"large"small')).to.equal('<font size="10&quot;large&quot;small">foo</font>');
});
it('has #italics', function () {
expect('foo'.italics()).to.equal('<i>foo</i>');
});
it('has #link', function () {
expect('foo'.link('url"http://"')).to.equal('<a href="url&quot;http://&quot;">foo</a>');
});
it('has #small', function () {
expect('foo'.small()).to.equal('<small>foo</small>');
});
it('has #strike', function () {
expect('foo'.strike()).to.equal('<strike>foo</strike>');
});
it('has #sub', function () {
expect('foo'.sub()).to.equal('<sub>foo</sub>');
});
it('has #sup', function () {
expect('foo'.sup()).to.equal('<sup>foo</sup>');
});
});
};

describe('clean Object.prototype', runStringTests);
Expand Down

0 comments on commit c23133d

Please sign in to comment.