Skip to content

Helper functions for CSS classes (classList) #11688

Closed
@dtdesign

Description

@dtdesign

Description

The DOM extension of PHP lacks a couple of helper functions that have eased the life of frontend engineers for many years.

The lack of a proper handling of CSS classes was painful back in the IE6 days and while jQuery solved that to some degree, the addition of the .classList was a massive improvement. I would really love to see PHP adding support for a similar interface to ease the work with CSS classes.

Old additional content:

Another improvement would be the addition of [`append()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/append), [`after()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/after), [`before()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/before) and [`insertAdjacentElement()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement). The first three functions are also special because they implicitly convert strings into text nodes. `after()` and `before()` are simple helper functions that improve readability instead of relying on some `insertBefore()` shenanigans on the parent element. `insertAdjacentElement()` is unique as it accepts a string (in PHP this would possibly be an enum?) for the position which itself can be the result of a simple match statement.

There is also replaceWith() that allows to swap out DOM nodes while preserving the original position in the DOM, but its arguably more niche of a use case.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions