Module:Mw.html extension/doc

This is the current revision of this page, as edited by Alex (talk | contribs) at 14:49, 15 October 2024 (Created page with "{{Documentation}} {{Helper module |name = Mw.html extension |fname1 = addClassIf(cond, class) |ftype1 = boolean, string |fuse1 = If <code>cond</code> = <code>true</code> it behaves the same as the normal <code>addClass</code> function, otherwise it's a no-op. Ex.: <code>mw.html.create('div'):addClassIf(true, 'align-left-1')</code> |fname2 = attrIf(cond, name, value) |ftype2 = boolean, string/table, string/nil |fuse2 = Similar to <code>addClassIf</code> |fname3 = cssIf..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is the documentation page for Module:Mw.html extension

This is a documentation subpage for Module:Mw.html extension.
It contains usage information, categories, and other content that is not part of the original module page.
Module:Mw.html extension requires libraryUtil.
Module:Mw.html extension is required by Module:Helper module.
Module:Mw.html extension is required by Module:Slottable.
Module:Mw.html extension is required by Module:User hiscores.

This module is a helper module to be used by other modules; it may not designed to be invoked directly. See RuneScape:Lua/Helper modules for a full list and more information.

FunctionTypeUse
addClassIf(cond, class)boolean, stringIf cond = true it behaves the same as the normal addClass function, otherwise it's a no-op. Ex.: mw.html.create('div'):addClassIf(true, 'align-left-1')
attrIf(cond, name, value)boolean, string/table, string/nilSimilar to addClassIf
cssIf(cond, name, value)boolean, string/table, string/nilSimilar to addClassIf
doneIf(cond)booleanSimilar to addClassIf
tagIf(cond, tag)boolean, stringSimilar to addClassIf
wikitextIf(cond, text)boolean, stringSimilar to addClassIf
na()N/AShortcut for :tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
naIf(cond)booleanSimilar to addClassIf
tr([settings])table/nilShortcut for :tag('tr') but also auto closes the previous 'tr', 'th' or 'td' tag (so you don't need to add :done() before it). settings is a table with keys:
  • class or addClass - A string passed to :addClass()
  • attr - A table passed to :attr()
  • css - A table passed to :css()
  • cssText - A string passed to :cssText()
th([settings])string/table/nilShortcut for :tag('th'):wikitext(settings) if settings is a string. Also auto closes the previous 'th' or 'td' tag. settings can also be a table with keys:
  • [1] (array) or wikitext - A string passed to :wikitext()
  • class or addClass - A string passed to :addClass()
  • attr - A table passed to :attr()
  • css - A table passed to :css()
  • cssText - A string passed to :cssText()
td([settings])string/table/nilSame as :th(). Example:
local tbl = mw.html.create('table')
tbl:tr{ class='sortable' }
        :th{'foo', attr={'data-sort-type', 'number'}}
        :th('bar')
    :tr()
        :td('buz')
            :attr('data-sort-value', 10)
        :td{'N/A', class='table-na'}
IF(cond)booleanAllows for if-blocks without breaking the chain. If the condition is true it is a no-op, if false everything inside the balanced IF-END block will be ignored. Can be nested. Ex.:
mw.html.create('div')
    :IF(true)
        :wikitext('Conditional text')
    :END()
    :...

Note: This only prevents elements from being added to your html object, it does not protect against statements that throw errors. I.e

mw.html.create('div')
    :IF(false)
        :wikitext(5 * nil) -- This will still throw an error
    :END()
ELSEIF(cond)booleanUsed together with IF().
ELSE()N/AUsed together with IF().
END()N/AUsed together with IF(). Make sure the IF-END tags are balanced, it wont throw an error if they are not.
exec(func, ...)function, anyCall a function without breaking the chain. See module docs for more info.
addFunction(func, name)function, stringAdd a function to the mw.html class that can then be used on mw.html object. See module docs for more info.

Usage

For all functions other than addFunction() all you need to do is simply require this module:

require('Module:Mw.html extension')
local p = {}

function p.main()
    ...
    local tbl = mw.html.create('div')
        :IF(true)
            :wikitext('Conditional text')
        :ELSE()
            :wikitext('something else')
        :END()
        :addClassIf(true, 'wikitable')
        :tag('span)'
            :wikitext('normal wikitext')
        :done()
    ...
end

return p

You can mix the normal old functions with the newly added ones.

attrIf

This accepts either a name-value pair or a table

  • :attrIf(true, 'data-sort-value', '0')
  • :attrIf(true, {'data-sort-value' = '0', ...})

cssIf

This accepts either a name-value pair or a table similar to attrIf

exec

The first parameter of the function will have the current state of the mw.html object passed into it, usually we call this parameter self, the rest of the parameters can be anything you want. To not break the chaining the function must also return a mw.html object. Example:

local function repNa(self, times)
    for i = 1,times do
        self:na()
    end
    return self
end

This function can then be used as follows

mw.html.create('div'):exec(repNa, 5)

addFunction

The function you want to add has to be of the same structure as in exec(). Example:

local htmlExtension = require('Module:Mw.html extension')
local p = {}

local function repNa(self, times)
    for i = 1,times do
        self:na()
    end
    return self
end
htmlExtension.addFunction(repNa, 'repNaName')

function p.main()
    ...
    local tbl = mw.html.create('div'):repNaName(5)
    ...
end

return p

tr, th and td

The following three tables are the same:

local tbl = mw.html.create('table')
tbl:tr{ class='sortable' }
        :th{'foo', attr={['data-sort-type']='number'}} -- or attr={'data-sort-type', 'number'}
        :th('bar')
            :IF(expression)
                :addClass('table-na')
            :END()
    :tr()
        :td('buz')
        :td{'N/A', class='table-na'}

local tbl2 = mw.html.create('table')
tbl2:tag('tr')
        :addClass('sortable')
        :tag('th')
            :attr('data-sort-type', 'number')
            :wikitext('foo')
        :done()
        :tag('th')
            :wikitext('bar')
            :IF(expression)
                :addClass('table-na')
            :END()
        :done() -- This is needed because "tag('tr')" is used after this instead of "tr()"
    :done()
    :tag('tr')
        :tag('td')
            :wikitext('buz')
        :done()
        :tag('td')
            :wikitext('N/A')
            :addClass('table-na')

local tbl3 = mw.html.create('table')
tbl3:tag('tr')
        :addClass('sortable')
        :tag('th')
            :attr('data-sort-type', 'number')
            :wikitext('foo')
        :th('bar')
            :IF(expression)
                :addClass('table-na')
            :END()
        :done()
    :done()
    :tag('tr')
        :td('buz')
        :td('N/A')
            :addClass('table-na')