Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 1 addition & 50 deletions bridge/bindings/qjs/converter_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "core/css/computed_css_style_declaration.h"
#include "core/css/legacy/legacy_computed_css_style_declaration.h"
#include "foundation/utility/make_visitor.h"

namespace webf {

Expand Down Expand Up @@ -637,56 +638,6 @@ struct Converter<IDLNullable<T, typename std::enable_if_t<std::is_base_of<Script
}
};

template <>
struct Converter<ElementStyle> {
using ImplType = ElementStyle;

static ElementStyle FromValue(JSContext* ctx, JSValue value, ExceptionState& exception_state) {
auto ectx = ExecutingContext::From(ctx);
if (ectx->isBlinkEnabled()) {
if (JS_IsNull(value)) {
return static_cast<InlineCssStyleDeclaration*>(nullptr);
}

return Converter<InlineCssStyleDeclaration>::FromValue(ctx, value, exception_state);
} else {
if (JS_IsNull(value)) {
return static_cast<legacy::LegacyInlineCssStyleDeclaration*>(nullptr);
}

return Converter<legacy::LegacyInlineCssStyleDeclaration>::FromValue(ctx, value, exception_state);
}
}

static ElementStyle ArgumentsValue(ExecutingContext* context,
JSValue value,
uint32_t argv_index,
ExceptionState& exception_state) {
if (context->isBlinkEnabled()) {
if (JS_IsNull(value)) {
return static_cast<InlineCssStyleDeclaration*>(nullptr);
}

return Converter<InlineCssStyleDeclaration>::ArgumentsValue(context, value, argv_index, exception_state);
} else {
if (JS_IsNull(value)) {
return static_cast<legacy::LegacyInlineCssStyleDeclaration*>(nullptr);
}

return Converter<legacy::LegacyInlineCssStyleDeclaration>::ArgumentsValue(context, value, argv_index, exception_state);
}
}

static JSValue ToValue(JSContext* ctx, ElementStyle value) {
return std::visit(MakeVisitor([&ctx](auto* style) {
if (style == nullptr)
return JS_NULL;
return Converter<std::remove_pointer_t<std::decay_t<decltype(style)>>>::ToValue(ctx, style);
}),
value);
}
};

template <>
struct Converter<WindowComputedStyle> {
using ImplType = WindowComputedStyle;
Expand Down
18 changes: 7 additions & 11 deletions bridge/core/api/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
#include "core/css/legacy/legacy_inline_css_style_declaration.h"
#include "core/dom/container_node.h"
#include "core/dom/element.h"
#include "foundation/utility/make_visitor.h"

namespace webf {

WebFValue<LegacyCssStyleDeclaration, LegacyCssStyleDeclarationPublicMethods> ElementPublicMethods::Style(Element* ptr) {
auto* element = static_cast<webf::Element*>(ptr);
MemberMutationScope member_mutation_scope{element->GetExecutingContext()};
auto style = element->style();
auto* style_declaration = element->style();
if (!style_declaration) {
return WebFValue<LegacyCssStyleDeclaration, LegacyCssStyleDeclarationPublicMethods>::Null();
}

return std::visit(
MakeVisitor(
[&](legacy::LegacyInlineCssStyleDeclaration* styleDeclaration) {
WebFValueStatus* status_block = styleDeclaration->KeepAlive();
return WebFValue<LegacyCssStyleDeclaration, LegacyCssStyleDeclarationPublicMethods>(
styleDeclaration, styleDeclaration->legacyCssStyleDeclarationPublicMethods(), status_block);
},
[](auto&&) { return WebFValue<LegacyCssStyleDeclaration, LegacyCssStyleDeclarationPublicMethods>::Null(); }),
style);
WebFValueStatus* status_block = style_declaration->KeepAlive();
return WebFValue<LegacyCssStyleDeclaration, LegacyCssStyleDeclarationPublicMethods>(
style_declaration, style_declaration->legacyCssStyleDeclarationPublicMethods(), status_block);
}

void ElementPublicMethods::ToBlob(Element* ptr,
Expand Down
43 changes: 8 additions & 35 deletions bridge/core/css/blink_inline_style_validation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::string CommandArg01ToUTF8(const UICommandItem& item) {
return String(utf16, static_cast<size_t>(item.args_01_length)).ToUTF8String();
}

std::string SharedNativeStringToUTF8(const SharedNativeString* s) {
std::string SharedNativeStringToUTF8(const webf::SharedNativeString* s) {
if (!s || !s->string() || s->length() == 0) {
return "";
}
Expand All @@ -53,7 +53,7 @@ bool HasSetStyleWithKeyValue(ExecutingContext* context, const std::string& key,
auto* items = static_cast<UICommandItem*>(pack->data);
for (int64_t i = 0; i < pack->length; ++i) {
const UICommandItem& item = items[i];
if (item.type == static_cast<int32_t>(UICommand::kSetStyle)) {
if (item.type == static_cast<int32_t>(UICommand::kSetInlineStyle)) {
if (CommandArg01ToUTF8(item) != key) {
continue;
}
Expand All @@ -79,34 +79,7 @@ bool HasSetStyleWithKeyValue(ExecutingContext* context, const std::string& key,
if (item.string_01 < 0) {
value_text = getValueName(static_cast<CSSValueID>(-item.string_01 - 1));
} else {
auto* value_ptr = reinterpret_cast<SharedNativeString*>(static_cast<uintptr_t>(item.string_01));
value_text = SharedNativeStringToUTF8(value_ptr);
}
if (value_text == value) {
return true;
}
}
return false;
}

bool HasSetStyleByIdWithKeyValue(ExecutingContext* context, const std::string& key, const std::string& value) {
const CSSPropertyID expected_property_id = CssPropertyID(context, ConvertCamelCaseToKebabCase(key));
auto* pack = static_cast<UICommandBufferPack*>(context->uiCommandBuffer()->data());
auto* items = static_cast<UICommandItem*>(pack->data);
for (int64_t i = 0; i < pack->length; ++i) {
const UICommandItem& item = items[i];
if (item.type != static_cast<int32_t>(UICommand::kSetStyleById)) {
continue;
}
if (item.args_01_length != static_cast<int32_t>(expected_property_id)) {
continue;
}

std::string value_text;
if (item.string_01 < 0) {
value_text = getValueName(static_cast<CSSValueID>(-item.string_01 - 1));
} else {
auto* value_ptr = reinterpret_cast<SharedNativeString*>(static_cast<uintptr_t>(item.string_01));
auto* value_ptr = reinterpret_cast<webf::SharedNativeString*>(static_cast<uintptr_t>(item.string_01));
value_text = SharedNativeStringToUTF8(value_ptr);
}
if (value_text == value) {
Expand All @@ -118,7 +91,7 @@ bool HasSetStyleByIdWithKeyValue(ExecutingContext* context, const std::string& k

} // namespace

TEST(BlinkCSSStyleDeclarationValidation, RejectsInvalidFontSize) {
TEST(BlinkCSSStyleDeclarationValidation, ForwardsInvalidFontSizeToDart) {
bool static errorCalled = false;
webf::WebFPage::consoleMessageHandler = [](void*, const std::string&, int) {};

Expand All @@ -137,15 +110,15 @@ TEST(BlinkCSSStyleDeclarationValidation, RejectsInvalidFontSize) {
const char* set_valid = "document.body.style.fontSize = '18px';";
env->page()->evaluateScript(set_valid, strlen(set_valid), "vm://", 0);
TEST_runLoop(context);
EXPECT_TRUE(HasSetStyleByIdWithKeyValue(context, "fontSize", "18px"));
EXPECT_TRUE(HasSetStyleWithKeyValue(context, "fontSize", "18px"));

context->uiCommandBuffer()->clear();

// Invalid font-size should be rejected on the native (Blink) CSS side and
// thus should not be forwarded to Dart.
// Inline style is legacy-only even when Blink CSS is enabled; invalid values
// are forwarded to Dart for validation/handling.
const char* set_invalid = "document.body.style.fontSize = '-1px';";
env->page()->evaluateScript(set_invalid, strlen(set_invalid), "vm://", 0);
TEST_runLoop(context);
EXPECT_FALSE(HasSetStyleByIdWithKeyValue(context, "fontSize", "-1px"));
EXPECT_TRUE(HasSetStyleWithKeyValue(context, "fontSize", "-1px"));
EXPECT_EQ(errorCalled, false);
}
4 changes: 2 additions & 2 deletions bridge/core/css/css_properties.json5
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@
},
{
name: "list-style-type",
// Parsing and initial value only – forwarded to Dart via UICommand kSetStyle
// Parsing and initial value only – forwarded to Dart via UICommand kSetInlineStyle
// We don't currently store this on ComputedStyle; values are emitted from the inline
// property set during style resolution.
field_template: "keyword",
Expand Down Expand Up @@ -1151,7 +1151,7 @@
// CSS Counters
{
name: "counter-reset",
// Parsing and initial value only – forwarded to Dart via UICommand kSetStyle
// Parsing and initial value only – forwarded to Dart via UICommand kSetInlineStyle
property_methods: [
"ParseSingleValue",
"InitialValue"
Expand Down
4 changes: 2 additions & 2 deletions bridge/core/css/inline_css_style_declaration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ document.body.style.setProperty('--main-color', 'lightblue'); console.assert(doc

UICommandItem& last = ((UICommandItem*)p_buffer_pack->data)[commandSize - 1];

EXPECT_EQ(last.type, (int32_t)UICommand::kSetStyle);
EXPECT_EQ(last.type, (int32_t)UICommand::kSetInlineStyle);
uint16_t* last_key = (uint16_t*)last.string_01;

// auto native_str = new webf::SharedNativeString(last_key, last.args_01_length);
Expand Down Expand Up @@ -101,4 +101,4 @@ TEST(InlineCSSStyleDeclaration, setNullValue) {
"console.assert(document.body.style.height === '')";
env->page()->evaluateScript(code, strlen(code), "vm://", 0);
EXPECT_EQ(errorCalled, false);
}
}
Loading
Loading