Module:Mw.html extension/doc

From RuneScape Classic Wiki
Jump to: navigation, search

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

Information icon-grey.svg
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 Module:LibraryUtil.
Module:Mw.html extension is required by Module:Helper module.

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.

ModuleFunctionTypeUse
Mw.html extensionaddClassIf(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
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()
    :...
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