1local function FixSelection(selector)
2 selector = selector:trim()
3
4 local lastPos = nil
5 while true do
6 local pos = selector:find('%[', lastPos)
7 if not pos then
8 break
9 end
10
11 lastPos = selector:find(']', lastPos)
12
13 local res = nil
14 local cmd = selector:sub(pos + 1, lastPos - 1)
15 local values = cmd:split('=')
16 if #values > 1 then
17 res = '[' .. values[1] .. '="' .. values[2] .. '"]'
18 end
19
20 if res then
21 selector = table.concat { selector:sub(1, pos - 1), res, selector:sub(lastPos + 2) }
22 end
23 end
24
25 return selector
26end
27
28local function parseCss(content, cssList, checkExist)
29 local css = CssParse.new()
30 css:parse(content)
31 local data = css:get_objects()
32 for _, o in ipairs(data) do
33 table.insert(cssList, {
34 selector = FixSelection(o.selector),
35 attrs = o.declarations,
36 checkExist = checkExist
37 })
38 end
39end
40
41local function parseAndSetDisplayAttr(el)
42 if el.widget:hasAnchoredLayout() then
43 if el.widget:getChildIndex() == 1 or el.attributes and el.attributes.anchor == 'parent' then
44 el.widget:addAnchor(AnchorLeft, 'parent', AnchorLeft)
45 el.widget:addAnchor(AnchorTop, 'parent', AnchorTop)
46 elseif el.prev and el.prev.style and el.prev.style.display == 'block' then
47 el.widget:addAnchor(AnchorLeft, 'parent', AnchorLeft)
48 el.widget:addAnchor(AnchorTop, 'prev', AnchorBottom)
49 else -- if el.prev.style.display == 'inline' then
50 el.widget:addAnchor(AnchorLeft, 'prev', AnchorRight)
51 el.widget:addAnchor(AnchorTop, 'prev', AnchorTop)
52 end
53 end
54
55 if not el.style then
56 return
57 end
58
59 if el.style.display == 'none' then
60 el.widget:setVisible(false)
61 end
62end
63
64local function parseAndSetFloatStyle(el)
65 if not el.style or not el.style.float or not el.widget:hasAnchoredLayout() then
66 return
67 end
68
69 if el.style.float == 'right' then
70 local anchor = 'parent'
71 local anchorType = AnchorRight
72 for _, child in pairs(el.parent.nodes) do
73 if child ~= el and child.style and child.style.float == 'right' then
74 anchor = child.widget:getId()
75 anchorType = AnchorLeft
76 break
77 end
78 end
79
80 el.widget:removeAnchor(AnchorLeft)
81 el.widget:addAnchor(AnchorRight, anchor, anchorType)
82 elseif el.style.float == 'left' then
83 local anchor = 'parent'
84 local anchorType = AnchorLeft
85 for _, child in pairs(el.parent.nodes) do
86 if child ~= el and child.style and child.style.float == 'right' then
87 anchor = child.widget:getId()
88 anchorType = AnchorRight
89 break
90 end
91 end
92
93 el.widget:removeAnchor(AnchorRight)
94 el.widget:addAnchor(AnchorLeft, anchor, anchorType)
95 end
96end
97
98return parseCss, parseAndSetDisplayAttr, parseAndSetFloatStyle
99