Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplements HtmlParser using Symfony DomCrawler #21

Merged
merged 3 commits into from
Dec 21, 2021

Conversation

JohnathonKoster
Copy link
Contributor

This PR removes the RegEx-based parser and replaces it with PHP's HTML parser. Additionally, this PR utilizes Symfony's DomCrawler component to target the HTML nodes that should have their CSS class properties updated. Symfony's CssSelector component is also used to support more complex CSS-like selectors.

Backwards Compatibility

This PR maintains the existing configuration format, and converts them to their appropriate CSS style selectors. All existing test behavior has been preserved (with minor test changes to reflect the differences in how the emitted HTML attributes are ordered in the output).

An example of HTML attributes being emitted in a different order is:

<a class="link" href="#">Link</a>

now emits:

<a href="#" class="link">Link</a>

Improved Nested Element Support

This PR resolves #16 , and allows for deeply nested class configurations to be specified:

$config = [
    'default'  => [
        'a' => 'root-link',
        'ul' => 'parent',
        'ul li' => 'nested',
        'ul li a' => 'first-nested-links',
        'ul li ul' => 'nested-parent',
        'ul li ul li' => 'nested-item',
    ],
];

The above configuration could be used to produce output similar to the following example:

<a class="root-link">Root link</a>
<ul class="parent">
   <li class="nested"><a class="first-nested-links">I am nested 1</a></li>
   <li class="nested"><a class="first-nested-links">I am nested 2</a></li>
   <li class="nested"><a class="first-nested-links">I am nested 3</a></li>
   <li class="nested">
       <ul class="nested-parent">
           <li class="nested-item"><a class="root-link">I am nested 2.1</a></li>
           <li class="nested-item"><a class="root-link">I am nested 2.2</a></li>
           <li class="nested-item"><a class="root-link">I am nested 2.3</a></li>
       </ul>
   </li>
</ul>

By utilizing more complicated CSS-style selectors, developers may now be more specific in how they apply their CSS classes:

$config = [
    'default'  => [
        'a' => 'root-link',
        'ul' => 'parent',
        'ul li' => 'nested',
        'ul li:nth-child(2n+2)' => 'nested nested-even',
        'ul li a' => 'first-nested-links',
        'ul li ul' => 'nested-parent',
        'ul li ul li' => 'nested-item',
        'ul li ul li a' => 'deeply-nested-links',
    ],
];

Would produce the following output:

<a class="root-link">Root link</a>
<ul class="parent">
   <li class="nested"><a class="first-nested-links">I am nested 1</a></li>
   <li class="nested nested-even"><a class="first-nested-links">I am nested 2</a></li>
   <li class="nested"><a class="first-nested-links">I am nested 3</a></li>
   <li class="nested nested-even">
       <ul class="nested-parent">
           <li class="nested-item"><a class="deeply-nested-links">I am nested 2.1</a></li>
           <li class="nested-item"><a class="deeply-nested-links">I am nested 2.2</a></li>
           <li class="nested-item"><a class="deeply-nested-links">I am nested 2.3</a></li>
       </ul>
   </li>
</ul>

Improvements to CSS Framework Build Tooling Workflows

Additionally, this PR offers a solution for #20. While this PR does not directly solve the issue, it provides a new Artisan command that can be used to produce a JavaScript configuration file containing all of the Classify class names:

php artisan classify:export

This command will create a new classify.config.js file at the root of the project, with contents similar to:

module.exports.classes = [
    "mt-8",
    "first:mt-0",
    "text-xs",
    "uppercase",
];

This new configuration file can then be used to configure the CSS build process. For example, a Tailwind CSS version 3 configuration file could be updated to the following:

const classify = require('./classify.config');

module.exports = {
  content: [
    './pages/**/*.{html,js}'
    './components/**/*.{html,js}',
  ],
  safelist: [
    ...classify.classes,
    'bg-red-500',
    'text-3xl',
    'lg:text-4xl',
  ]
  // ...
}

Adds a new classify:export command, clean up and more test coverage

Updates composer dependencies, more test coverage
@jonassiewertsen jonassiewertsen merged commit 799b395 into visuellverstehen:main Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Only first nested item gets classes
2 participants