Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(widget): simplify setup of html tags widgets #4902

Merged
merged 4 commits into from
Oct 18, 2024
Merged
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
28 changes: 0 additions & 28 deletions components/widget/html/widget_html_abbr.lua

This file was deleted.

70 changes: 59 additions & 11 deletions components/widget/html/widget_html_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,66 @@

local Widgets = {}

local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')

Widgets.Abbr = Lua.import('Module:Widget/Html/Abbr')
Widgets.Center = Lua.import('Module:Widget/Html/Center')
Widgets.Div = Lua.import('Module:Widget/Html/Div')
Widgets.Fragment = Lua.import('Module:Widget/Html/Fragment')
Widgets.Li = Lua.import('Module:Widget/Html/Li')
Widgets.Span = Lua.import('Module:Widget/Html/Span')
Widgets.Table = Lua.import('Module:Widget/Html/Table')
Widgets.Td = Lua.import('Module:Widget/Html/Td')
Widgets.Th = Lua.import('Module:Widget/Html/Th')
Widgets.Tr = Lua.import('Module:Widget/Html/Tr')
Widgets.Ul = Lua.import('Module:Widget/Html/Ul')
local Widget = Lua.import('Module:Widget')

---@param tag? string
---@return WidgetHtml
local function createHtmlTag(tag)
---@class WidgetHtml: Widget
---@operator call(table): WidgetHtml
local Html = Class.new(Widget)
Html.defaultProps = {
classes = {},
css = {},
attributes = {},
}

---@return Html
function Html:render()
local htmlNode = mw.html.create(tag)

htmlNode:addClass(String.nilIfEmpty(table.concat(self.props.classes, ' ')))
htmlNode:css(self.props.css)
htmlNode:attr(self.props.attributes)

Array.forEach(self.props.children, function(child)
if Class.instanceOf(child, Widget) then
---@cast child Widget
child.context = self:_nextContext()
htmlNode:node(child:tryMake())
else
---@cast child -Widget
---@diagnostic disable-next-line: undefined-field
if type(child) == 'table' and not child._build then
mw.log('ERROR! Bad child input:' .. mw.dumpObject(self.props.children))
-- Erroring here to make it easier to debug
-- Otherwise it will fail when the html is built
error('Table passed to HtmlBase:renderAs() without _build method')
end
htmlNode:node(child)
end
end)
return htmlNode
end

return Html
end

Widgets.Abbr = createHtmlTag('abbr')
Widgets.Center = createHtmlTag('center')
Widgets.Div = createHtmlTag('div')
Widgets.Fragment = createHtmlTag()
Widgets.Li = createHtmlTag('li')
Widgets.Span = createHtmlTag('span')
Widgets.Table = createHtmlTag('table')
Widgets.Td = createHtmlTag('td')
Widgets.Th = createHtmlTag('th')
Widgets.Tr = createHtmlTag('tr')
Widgets.Ul = createHtmlTag('ul')

return Widgets
59 changes: 0 additions & 59 deletions components/widget/html/widget_html_base.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_center.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_div.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_fragment.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_li.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_span.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_table.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_td.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_th.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_tr.lua

This file was deleted.

Loading
Loading