Muthukannan Kanniappan
Muthukannan Kanniappan

Reputation: 2079

Moving a node as the first child of another node

I have a document:

<html>
 <head>
   <style>some styles<style>
 </head>
 <body>
   <h1>Header</h1>
   <table>table content</table>
   <div>some text</div>
 </body>
</A>

The below code moves the <style> tag below the <div> in <body>:

  style  = @doc.at_css "style"
  body = @doc.at_css "body"
  style.parent = body

Is there a way to move <style> above <h1>?

Upvotes: 1

Views: 157

Answers (1)

Muthukannan Kanniappan
Muthukannan Kanniappan

Reputation: 2079

Finding the first child of the body and adding the style tag as previous sibling to first child solves the problem.

  style  = @doc.at_css "style"
  body = @doc.at_css "body"
  style.parent = body

  first_child = body.first_element_child
  first_child.add_previous_sibling(style)

Upvotes: 3

Related Questions