html - How to wrap an HtmlElement in an HtmlDocument -


is possible wrap htmlelement htmlelement? , if so, how?

lets say, have html, , want wrap img additional div:

<html>   <head><title>foo</title></head>   <body>     <img src="test.png"/>     <p>hello world</p>   </body> </html> 

i've tried using following snippet (n.b. document element htmldocument):

$htmlfile = new-object -comobject "htmlfile" $htmlfile.ihtmldocument2_write((gc -path "./html.htm" -raw)) $htmlfile.documentelement.getelementsbytagname("img") | %{   $div = $htmlfile.createelement("div")   $div.classname += "nonbreakingimage"   $void = $div.appendchild($_)   $_.outerhtml = $div.outerhtml } 

but this, unfortunately, removes image tag:

<body><p>hello world</p></body> 

another idea was, this:

$htmlfile = new-object -comobject "htmlfile" $htmlfile.ihtmldocument2_write((gc -path "./html.htm" -raw)) $htmlfile.documentelement.getelementsbytagname("img") | %{   $parent = $_.parentnode   $void = $parent.removechild($_)   $div = $parent.appendchild($htmlfile.createelement("div"))   $div.classname += " nonbreakingimage"   $void = $div.appendchild($_) } 

but leaves me wrong child order:

<body>   <p>hello world</p>   <div class=" nonbreakingimage"><img src="test.png"></div> </body> 

any ideas , hints welcome!

you can replace <img> node <div> node add (now floating) <img> node newly added <div>. in code:

$htmlfile.documentelement.getelementsbytagname("img") | %{   $div = $htmlfile.createelement("div")   $div.classname += " nonbreakingimage"    $_.parentnode.replacechild($div, $_)   $div.appendchild($_) } 

in alternative may $_.insertbefore($div, ...) of node obtained $_.previoussibling(...) it's longer.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -