Skip to content

Instantly share code, notes, and snippets.

@uzluisf
Created May 31, 2022 16:42
Show Gist options
  • Save uzluisf/fcef5455397bc1f6a738a0e28333b814 to your computer and use it in GitHub Desktop.
Save uzluisf/fcef5455397bc1f6a738a0e28333b814 to your computer and use it in GitHub Desktop.
HTML Tag Class Exercise
  1. Design and implement a class (or other data structure) that can represent a single HTML tag.
my $html-tag = Tag.new("html");
  1. Write a function or method that can print/return the HTML string representation of a tag.
say $html-tag.new("html"); #=> "<html></html>"
  1. Extend tag data structure to model class attributes and content.
<html class="blue-theme">content</html>
  1. Write a function or method that can add a class to a tag.
$html-tag.add-class("blue-theme");
say $html-tag; #=> <html class="blue-theme"></html>
$html-tag.add-class("main-content"); 
say $html-tag; #=> <html class="blue-theme main-content"></html>
  1. Extend the tag data structure to allow the addition of child tags. Ensure that the HTML tree is pretty-printed with reasonable indentation and new lines.
my $html-tag = Tag.new("html");
my $body-tag = Tag.new("body");
my $div-tag = Tag.new("div");
$html-tag.append-child($body-tag);
$body-tag.appen-child(div-tag);

Then say $html-tag should output:

<html class="blue-theme">
  <body>
    <div></div>
  </body>
</html>
  1. Add a method or function to traverse the dom and return the first element by selector.

Given $html-tag represented by

<html>
  <body>
    <div class="main-content">
      <span class="some-other-content"></span>
      <p class="some-other-content">
    </div>
  </body>
</html>

Then:

say $html-tag.query-selector(['main-content', 'some-other-content']);
#=> <span class="some-other-content"></span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment