Изменения

склеиваем одинаковые соседние теги с одинаковыми атрибутами
Строка 22: Строка 22:  
'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td'
 
'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td'
 
}
 
}
 +
 +
local table_tags = (function (list)
 +
local set = {}
 +
for _, tag in ipairs (list) do
 +
set [tag] = true
 +
end
 +
return set
 +
end) { 'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td' }
    
local function quoted (quote)
 
local function quoted (quote)
Строка 27: Строка 35:  
end
 
end
    +
local function same_tag_and_attributes (node1, node2)
 +
if type (node1) ~= 'table' or type (node2) ~= 'table' then
 +
return false
 +
end
 +
local function subset (tbl1, tbl2)
 +
for key, value in pairs (tbl1) do
 +
if type (key) == 'string' then
 +
if tbl2 [key] ~= value then
 +
return false
 +
end
 +
end
 +
end
 +
return true
 +
end
 +
return subset (node1, node2) and subset (node2, node1)
 +
end
 +
 
local grammar = P { V'fragment' * -1,
 
local grammar = P { V'fragment' * -1,
 
fragment = (V'tag' + C (V'char' ^ 1)) ^ 0,
 
fragment = (V'tag' + C (V'char' ^ 1)) ^ 0,
Строка 34: Строка 59:  
tag [attr.name] = attr.value
 
tag [attr.name] = attr.value
 
end
 
end
 +
local previous
 
for _, fragment in ipairs (tbl) do
 
for _, fragment in ipairs (tbl) do
tag [#tag + 1] = fragment
+
if same_tag_and_attributes (fragment, previous) then
 +
for __, subfragment in ipairs (fragment) do
 +
previous [#previous + 1] = subfragment
 +
end
 +
else
 +
tag [#tag + 1] = fragment
 +
previous = fragment
 +
end
 
end
 
end
 
return tag
 
return tag