wip: select elements from html document and css selector

main
Guilian 2025-01-18 18:54:10 +01:00
parent 0f4f9030aa
commit e9c2553f88
Signed by: Guilian
GPG Key ID: B86CC9678982ED8C
2 changed files with 153 additions and 2 deletions

View File

@ -1,9 +1,10 @@
#!/bin/env lua
local html = require(".html")
local css = require(".css")
local file = io.open("test.html", "r")
local file = io.open("small.html", "r")
if file == nil then
error("File doesn't exist")
@ -11,4 +12,94 @@ end
local content = file:read("a")
html.print_document( html.parse( content ) )
local doc = html.parse( content )
print("Write a css selector:")
local whole_selector = css.parse( io.read() )
local current_selector = whole_selector
local elements = {}
-- start with all elements matching the first selector
doc:foreach(function( el )
if el:check_simple_selector( current_selector.selector ) then
table.insert( elements, el )
end
end)
while current_selector.combinator ~= nil do
local next_selector = current_selector.next
local new_elements = {}
if current_selector.combinator == css.COMBINATORS.DESCENDANT then
for _, element in ipairs( elements ) do
element:foreach(function( el )
if el:check_simple_selector( next_selector.selector ) then
table.insert( new_elements, el )
end
end)
end
goto continue
end
if current_selector.combinator == css.COMBINATORS.DIRECT_DESCENDANT then
for _, element in ipairs( elements ) do
for _, child in ipairs( element.children ) do
if child:check_simple_selector( next_selector.selector ) then
table.insert( new_elements, child )
end
end
end
goto continue
end
if current_selector.combinator == css.COMBINATORS.NEXT_SIBLING then
for _, element in ipairs( elements ) do
local next_sibling = element:get_next_sibling()
while next_sibling and next_sibling.tag_name == ":text" do
next_sibling = next_sibling:get_next_sibling()
end
if next_sibling and next_sibling:check_simple_selector( next_selector.selector ) then
table.insert( new_elements, next_sibling )
end
end
goto continue
end
if current_selector.combinator == css.COMBINATORS.SUBSEQUENT_SIBLING then
for _, element in ipairs( elements ) do
local sibling = element:get_next_sibling()
while sibling ~= nil do
if sibling:check_simple_selector( next_selector.selector ) then
table.insert( new_elements, sibling )
end
sibling = sibling:get_next_sibling()
end
end
goto continue
end
::continue::
elements = new_elements
current_selector = next_selector
end
for _, el in ipairs(elements) do
print( html.tostring( el ) )
end

60
small.html Normal file
View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTMLQ Test Document</title>
</head>
<body>
<div id="main" class="container wrapper">
<header class="header nav-bar">
<h1>HTMLQ Test Document</h1>
<nav class="nav" data-nav-type="main">
<ul>
<li><a href="#" data-link-type="internal">Home</a></li>
<li><a href="#" data-link-type="external">About</a></li>
<li><a href="#" data-link-type="internal">Contact</a></li>
</ul>
</nav>
</header>
<main class="content main-content">
<section class="section featured" id="featured-section">
<h2>Featured Section</h2>
<p>This is the featured section.</p>
<div class="card featured-card" data-card-type="featured">
<h3>Featured Card</h3>
<p>This is the featured card.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</section>
<section class="section regular" id="regular-section">
<h2>Regular Section</h2>
<p>This is the regular section.</p>
<div class="card regular-card" data-card-type="regular">
<h3>Regular Card</h3>
<p>This is the regular card.</p>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
</div>
</section>
</main>
<footer class="footer nav-bar" data-footer-type="main">
<p>&copy; 2024 HTMLQ Test Document</p>
<nav class="nav" data-nav-type="footer">
<ul>
<li><a href="#" data-link-type="internal">Home</a></li>
<li><a href="#" data-link-type="external">About</a></li>
<li><a href="#" data-link-type="internal">Contact</a></li>
</ul>
</nav>
</footer>
</div>
</body>
</html>