dart-lang / html

Dart port of html5lib. For parsing HTML/HTML5 with Dart. Works in the client and on the server.
https://pub.dev/packages/html
Other
276 stars 58 forks source link

How do I get an element's html without its content ? #213

Closed TheCarpetMerchant closed 1 year ago

TheCarpetMerchant commented 1 year ago

I'd like to have only the html of the node itself, without its children. So <div class="content"><p>text</p></div> would give me <div class="content"></div>.

fingerart commented 1 year ago

You can do it like:

final el = doc.querySelector('div.content');
print(el?.clone(false).outerHtml);
TheCarpetMerchant commented 1 year ago

Ah, didn't know about this clone function. Thank you !