How can I wrap a div around existing elements?

James Bubb

I'm trying to parse an existing document and modify it by wrapping a div around some existing form elements.

HTML form looks a bit like this:

<form>
  <label for="username">Username:</label>
  <input name="username" type="text" />
  <label for="password">Password:</label>
  <input name="password" type="password" />
</form>

I can parse the document OK with Nokogiri and i'm aware of the wrap method but i'm struggling to grasp how to select both the label and input tags in one go and then wrap a div around these. So the result I am looking for is:

<form>
  <div class="form-group">
    <label for="username">Username:</label>
    <input name="username" type="text" />
  </div>    
  <div class="form-group">
    <label for="password">Password:</label>
    <input name="password" type="password" />
  </div>
</form>

I have tried various XPaths / CSS selectors and can create a nodeset of just labels/inputs or all of the elements of the whole form. Is there any way to achieve this modification?

matt

A single XPath expression can only return a single collection of nodes, so in order to achieve what you want you will need to make several queries, one for each labelinput pair.

You can select an individual pair with something like this, assuming the markup is well behaved (i.e each input has a label before it):

//label[1] | //label[1]/following-sibling::input[1]

This will select the first label and the following input. However you want to select all such pairs. One way would be to first select all the label nodes, and then for each label select it and the following input.

labels = doc.xpath("//form/label")
labels.each do |l|
  nodes = l.xpath(". | ./following-sibling::input[1]")
  # nodes now contains a label-input pair...
end

I don’t think the wrap method will work to add a div element as an ancestor to each pair, as it will add the element to each member of the nodeset. You will probably have to move them manually, something like

labels = doc.xpath("//form/label")
labels.each do |l|
  # Select this node and its neighbour.
  nodes = l.xpath(". | ./following-sibling::input[1]")

  # Create the new element, and add it before the label.
  div = Nokogiri::XML::Node.new('div', l.document)
  l.before(div)

  # Move each of the pair onto this new element.
  nodes.each do |n|
    div.add_child(n)
  end
end

Note that this method doesn’t move any text nodes, so you may find the whitespace of your document changes a bit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I dynamically wrap elements in a div within another div?

From Dev

How can I wrap text around stacked elements (ie elements with negative margins)?

From Dev

How to wrap div around image?

From Dev

How do I wrap an a tag around a div tag?

From Dev

Adding a Div tag to wrap around LI elements

From Dev

Wrap div around 2 sequential elements in jQuery

From Dev

Wrap a div around every 2 elements in jQuery

From Dev

In jQuery how can I wrap multiple elements with a div given that I have a start and end class

From Dev

How can I select these elements and wrap them?

From Dev

How can I wrap text around image using Bootstrap?

From Dev

How can I wrap text around image using Bootstrap?

From Dev

How can I avoid wrap-around in the QDial control?

From Dev

How can I make a label in an iOS renderer wrap around and resize?

From Dev

Wrap tags around items inside existing <li> elements using jQuery

From Dev

How can I wrap existing static constants into an enumeration?

From Dev

How do I create concave shaped borders around <div> elements?

From Dev

how wrap div around two element with jquery

From Dev

How to wrap text around image in div?

From Dev

How to wrap div around <p> tag?

From Dev

how wrap div around two element with jquery

From Dev

How to wrap a link around a div jQuery?

From Dev

How can I move elements around in a multidimentional array?

From Dev

How to wrap a div containing text around a div containing an image?

From Java

How can I wrap my markdown in an HTML div?

From Dev

How can I wrap contiguous sibling divs under a parent div?

From Dev

How can I wrap a <span> around matched words in HTML without breaking the HTML

From Dev

Wrap div around a hidden div

From Dev

MySQL JOIN that I can't wrap my head around

From Dev

How to overlap image over the div, but still wrap text around an image?

Related Related

  1. 1

    How can I dynamically wrap elements in a div within another div?

  2. 2

    How can I wrap text around stacked elements (ie elements with negative margins)?

  3. 3

    How to wrap div around image?

  4. 4

    How do I wrap an a tag around a div tag?

  5. 5

    Adding a Div tag to wrap around LI elements

  6. 6

    Wrap div around 2 sequential elements in jQuery

  7. 7

    Wrap a div around every 2 elements in jQuery

  8. 8

    In jQuery how can I wrap multiple elements with a div given that I have a start and end class

  9. 9

    How can I select these elements and wrap them?

  10. 10

    How can I wrap text around image using Bootstrap?

  11. 11

    How can I wrap text around image using Bootstrap?

  12. 12

    How can I avoid wrap-around in the QDial control?

  13. 13

    How can I make a label in an iOS renderer wrap around and resize?

  14. 14

    Wrap tags around items inside existing <li> elements using jQuery

  15. 15

    How can I wrap existing static constants into an enumeration?

  16. 16

    How do I create concave shaped borders around <div> elements?

  17. 17

    how wrap div around two element with jquery

  18. 18

    How to wrap text around image in div?

  19. 19

    How to wrap div around <p> tag?

  20. 20

    how wrap div around two element with jquery

  21. 21

    How to wrap a link around a div jQuery?

  22. 22

    How can I move elements around in a multidimentional array?

  23. 23

    How to wrap a div containing text around a div containing an image?

  24. 24

    How can I wrap my markdown in an HTML div?

  25. 25

    How can I wrap contiguous sibling divs under a parent div?

  26. 26

    How can I wrap a <span> around matched words in HTML without breaking the HTML

  27. 27

    Wrap div around a hidden div

  28. 28

    MySQL JOIN that I can't wrap my head around

  29. 29

    How to overlap image over the div, but still wrap text around an image?

HotTag

Archive