From 6503b92fc7d49eed9a7d756f5c4f0edd74662788 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Sat, 19 Oct 2024 11:29:55 +0200 Subject: [PATCH 1/5] fix(widget): attr's widget --- components/widget/html/widget_html_all.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/components/widget/html/widget_html_all.lua b/components/widget/html/widget_html_all.lua index 1dc56d97e74..284f48967e7 100644 --- a/components/widget/html/widget_html_all.lua +++ b/components/widget/html/widget_html_all.lua @@ -16,8 +16,9 @@ local String = require('Module:StringUtils') local Widget = Lua.import('Module:Widget') ---@param tag? string +---@param specialMapping? fun(self: WidgetHtml) ---@return WidgetHtml -local function createHtmlTag(tag) +local function createHtmlTag(tag, specialMapping) ---@class WidgetHtml: Widget ---@operator call(table): WidgetHtml local Html = Class.new(Widget) @@ -25,11 +26,15 @@ local function createHtmlTag(tag) classes = {}, css = {}, attributes = {}, + tag = tag, } ---@return Html function Html:render() - local htmlNode = mw.html.create(tag) + if specialMapping then + specialMapping(self) + end + local htmlNode = mw.html.create(self.props.tag) htmlNode:addClass(String.nilIfEmpty(table.concat(self.props.classes, ' '))) htmlNode:css(self.props.css) @@ -58,10 +63,12 @@ local function createHtmlTag(tag) return Html end -Widgets.Abbr = createHtmlTag('abbr') +Widgets.Abbr = createHtmlTag('abbr', function (self) + self.props.attributes.title = self.props.attributes.title or self.props.title +end) Widgets.Center = createHtmlTag('center') Widgets.Div = createHtmlTag('div') -Widgets.Fragment = createHtmlTag() +Widgets.Fragment = createHtmlTag(nil) Widgets.Li = createHtmlTag('li') Widgets.Span = createHtmlTag('span') Widgets.Table = createHtmlTag('table') From 770bd47aea7052d44c8f508b10400ff95d6cc5d4 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 21 Oct 2024 14:12:20 +0200 Subject: [PATCH 2/5] add html widget tests --- spec/widgets/html_spec.lua | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 spec/widgets/html_spec.lua diff --git a/spec/widgets/html_spec.lua b/spec/widgets/html_spec.lua new file mode 100644 index 00000000000..a0c9c4797ec --- /dev/null +++ b/spec/widgets/html_spec.lua @@ -0,0 +1,45 @@ +--- components/widget/html/widget_html_all.lua + +describe('HTML Widget', function() + local Widgets = require('components/widget/html/widget_html_all') + + describe('Abbr', function() + it('should create an abbr tag', function() + local abbr = Widgets.Abbr{ children = { "Test" }, title = "Title" } + local rendered = abbr:render():allDone() + assert.are.same('Test', tostring(rendered)) + end) + end) + + describe('Div', function() + it('should create a div tag', function() + local div = Widgets.Div{ children = { "Content" } } + local rendered = div:render():allDone() + assert.are.same('
Content
', tostring(rendered)) + end) + end) + + describe('Span', function() + it('should create a span tag', function() + local span = Widgets.Span{ children = { "Inline" } } + local rendered = span:render():allDone() + assert.are.same('Inline', tostring(rendered)) + end) + end) + + describe('Table', function() + it('should create a table tag', function() + local table = Widgets.Table{ children = { Widgets.Tr{ children = { Widgets.Td{ children = { "Cell" } } } } } } + local rendered = table:render():allDone() + assert.are.same('
Cell
', tostring(rendered)) + end) + end) + + describe('Ul & Li', function() + it('should create a list', function() + local ul = Widgets.Ul{ children = { Widgets.Li{ children = { "Item", "Item2" } } } } + local rendered = ul:render():allDone() + assert.are.same('', tostring(rendered)) + end) + end) +end) \ No newline at end of file From 07891ec4e7543c2a17e106d10dabb473df47d20a Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 21 Oct 2024 14:14:39 +0200 Subject: [PATCH 3/5] fix typo in input for one of the tests --- spec/widgets/html_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/widgets/html_spec.lua b/spec/widgets/html_spec.lua index a0c9c4797ec..979a21c051b 100644 --- a/spec/widgets/html_spec.lua +++ b/spec/widgets/html_spec.lua @@ -37,7 +37,7 @@ describe('HTML Widget', function() describe('Ul & Li', function() it('should create a list', function() - local ul = Widgets.Ul{ children = { Widgets.Li{ children = { "Item", "Item2" } } } } + local ul = Widgets.Ul{ children = { Widgets.Li{ children = { "Item" }}, Widgets.Li{ children = { "Item2" }}}} local rendered = ul:render():allDone() assert.are.same('
  • Item
  • Item 2
', tostring(rendered)) end) From 28b6efcecdd04f0d4cc37b1756ea7cebeb45ed66 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 21 Oct 2024 14:15:50 +0200 Subject: [PATCH 4/5] helps if the strings match --- spec/widgets/html_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/widgets/html_spec.lua b/spec/widgets/html_spec.lua index 979a21c051b..bd4d4104316 100644 --- a/spec/widgets/html_spec.lua +++ b/spec/widgets/html_spec.lua @@ -39,7 +39,7 @@ describe('HTML Widget', function() it('should create a list', function() local ul = Widgets.Ul{ children = { Widgets.Li{ children = { "Item" }}, Widgets.Li{ children = { "Item2" }}}} local rendered = ul:render():allDone() - assert.are.same('
  • Item
  • Item 2
', tostring(rendered)) + assert.are.same('
  • Item
  • Item2
', tostring(rendered)) end) end) end) \ No newline at end of file From d8c5499ace067bb4580b48cfa7f6f065d92c5403 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 21 Oct 2024 17:55:06 +0200 Subject: [PATCH 5/5] eof --- spec/widgets/html_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/widgets/html_spec.lua b/spec/widgets/html_spec.lua index bd4d4104316..0c476ce6f54 100644 --- a/spec/widgets/html_spec.lua +++ b/spec/widgets/html_spec.lua @@ -42,4 +42,4 @@ describe('HTML Widget', function() assert.are.same('
  • Item
  • Item2
', tostring(rendered)) end) end) -end) \ No newline at end of file +end)