1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:37:44 +00:00

LibJS: Add String.prototype.anchor & friends

Adds an implementation of the following StringPrototype methods:
anchor, big, blink, bold, fixed, fontcolor, fontsize, italics, link,
small, strike, sub, sup.
This commit is contained in:
Idan Horowitz 2021-05-30 01:44:18 +03:00 committed by Linus Groh
parent 460c0f9847
commit 6bfeb87572
3 changed files with 137 additions and 0 deletions

View file

@ -44,6 +44,7 @@ namespace JS {
P(acosh) \
P(all) \
P(allSettled) \
P(anchor) \
P(any) \
P(apply) \
P(arguments) \
@ -56,7 +57,10 @@ namespace JS {
P(atan) \
P(atan2) \
P(atanh) \
P(big) \
P(bind) \
P(blink) \
P(bold) \
P(buffer) \
P(byteLength) \
P(byteOffset) \
@ -103,9 +107,12 @@ namespace JS {
P(finally) \
P(find) \
P(findIndex) \
P(fixed) \
P(flags) \
P(flat) \
P(floor) \
P(fontcolor) \
P(fontsize) \
P(forEach) \
P(freeze) \
P(from) \
@ -159,12 +166,14 @@ namespace JS {
P(isSafeInteger) \
P(isSealed) \
P(isView) \
P(italics) \
P(join) \
P(keyFor) \
P(keys) \
P(lastIndex) \
P(lastIndexOf) \
P(length) \
P(link) \
P(log) \
P(log1p) \
P(log2) \
@ -214,6 +223,7 @@ namespace JS {
P(sin) \
P(sinh) \
P(slice) \
P(small) \
P(some) \
P(sort) \
P(source) \
@ -221,9 +231,12 @@ namespace JS {
P(sqrt) \
P(startsWith) \
P(sticky) \
P(strike) \
P(stringify) \
P(sub) \
P(substr) \
P(substring) \
P(sup) \
P(tan) \
P(tanh) \
P(test) \

View file

@ -89,6 +89,19 @@ void StringPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.at, at, 1, attr);
define_native_function(vm.names.match, match, 1, attr);
define_native_function(vm.names.replace, replace, 2, attr);
define_native_function(vm.names.anchor, anchor, 1, attr);
define_native_function(vm.names.big, big, 0, attr);
define_native_function(vm.names.blink, blink, 0, attr);
define_native_function(vm.names.bold, bold, 0, attr);
define_native_function(vm.names.fixed, fixed, 0, attr);
define_native_function(vm.names.fontcolor, fontcolor, 1, attr);
define_native_function(vm.names.fontsize, fontsize, 1, attr);
define_native_function(vm.names.italics, italics, 0, attr);
define_native_function(vm.names.link, link, 1, attr);
define_native_function(vm.names.small, small, 0, attr);
define_native_function(vm.names.strike, strike, 0, attr);
define_native_function(vm.names.sub, sub, 0, attr);
define_native_function(vm.names.sup, sup, 0, attr);
define_native_function(vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
}
@ -722,4 +735,102 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
return js_string(vm, builder.build());
}
// B.2.3.2.1
static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value)
{
auto& vm = global_object.vm();
if (string.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
return {};
}
auto str = string.to_string(global_object);
if (vm.exception())
return {};
StringBuilder builder;
builder.append('<');
builder.append(tag);
if (!attribute.is_empty()) {
auto value_string = value.to_string(global_object);
if (vm.exception())
return {};
value_string.replace("\"", "&quot;", true);
builder.append(' ');
builder.append(attribute);
builder.append("=\"");
builder.append(value_string);
builder.append('"');
}
builder.append('>');
builder.append(str);
builder.append("</");
builder.append(tag);
builder.append('>');
return js_string(vm, builder.build());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::anchor)
{
return create_html(global_object, vm.this_value(global_object), "a", "name", vm.argument(0));
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::big)
{
return create_html(global_object, vm.this_value(global_object), "big", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::blink)
{
return create_html(global_object, vm.this_value(global_object), "blink", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::bold)
{
return create_html(global_object, vm.this_value(global_object), "b", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::fixed)
{
return create_html(global_object, vm.this_value(global_object), "tt", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::fontcolor)
{
return create_html(global_object, vm.this_value(global_object), "font", "color", vm.argument(0));
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::fontsize)
{
return create_html(global_object, vm.this_value(global_object), "font", "size", vm.argument(0));
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::italics)
{
return create_html(global_object, vm.this_value(global_object), "i", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::link)
{
return create_html(global_object, vm.this_value(global_object), "a", "href", vm.argument(0));
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::small)
{
return create_html(global_object, vm.this_value(global_object), "small", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::strike)
{
return create_html(global_object, vm.this_value(global_object), "strike", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::sub)
{
return create_html(global_object, vm.this_value(global_object), "sub", String::empty(), Value());
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::sup)
{
return create_html(global_object, vm.this_value(global_object), "sup", String::empty(), Value());
}
}

View file

@ -45,6 +45,19 @@ private:
JS_DECLARE_NATIVE_FUNCTION(at);
JS_DECLARE_NATIVE_FUNCTION(match);
JS_DECLARE_NATIVE_FUNCTION(replace);
JS_DECLARE_NATIVE_FUNCTION(anchor);
JS_DECLARE_NATIVE_FUNCTION(big);
JS_DECLARE_NATIVE_FUNCTION(blink);
JS_DECLARE_NATIVE_FUNCTION(bold);
JS_DECLARE_NATIVE_FUNCTION(fixed);
JS_DECLARE_NATIVE_FUNCTION(fontcolor);
JS_DECLARE_NATIVE_FUNCTION(fontsize);
JS_DECLARE_NATIVE_FUNCTION(italics);
JS_DECLARE_NATIVE_FUNCTION(link);
JS_DECLARE_NATIVE_FUNCTION(small);
JS_DECLARE_NATIVE_FUNCTION(strike);
JS_DECLARE_NATIVE_FUNCTION(sub);
JS_DECLARE_NATIVE_FUNCTION(sup);
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
};