May 09, 2020
Are there are tutorials or examples on how to manipulate HTML using dom.d?
Replace tag, text, end so on.

ByR
May 09, 2020
On Saturday, 9 May 2020 at 16:10:37 UTC, ByR wrote:
> Are there are tutorials or examples on how to manipulate HTML using dom.d?
> Replace tag, text, end so on.
>
> ByR

I don't think so really. There's a wee bit of documentation here http://dpldocs.info/experimental-docs/arsd.dom.html but for the most part it is just find a few functions you like and use them.

The .innerText and .innerHTML properties are the easiest for some string stuff. querySelector and querySelectorAll work like the javascript functions.

Other extension functions that might help are .stripOut that removes the tag but keeps its children and .removeFromTree that removes the whole thing, children and all.

And many more but these alone can get you started.

auto document = new Document("<foo><p>hi</p></foo>");

foreach(ele; document.querySelectorAll("p"))
    ele.innerText = "changed";


foreach(ele; document.querySelectorAll("p"))
    ele.innerHTML = "<span>tags too here</span>";


foreach(ele; document.querySelectorAll("p"))
    ele.stripOut(); // would leave behind, from original, <foo>hi</foo>

or

    ele.removeFromTree(); // leaves behind just "<foo></foo>";



You can .addClass, .removeClass too, or modify attributes with the .setAttribute and .getAttribute, or the helper .attrs member.

ele.attrs.foo = "bar"; // adds foo="bar" to the tag attributes