Search code examples
regexopencartvqmod

Regex to find a div based on the attribute of one of its elements for vqmod xml


Ok, I'm creating a vqmod extension for opencart and I need to add a field to an admin page. The admin page has a form with bootstrap elements like this:

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-keyword"><span data-toggle="tooltip" title="<?php echo $help_keyword; ?>"><?php echo $entry_keyword; ?></span></label>
  <div class="col-sm-10">
    <input type="text" name="keyword" value="<?php echo $keyword; ?>" placeholder="<?php echo $entry_keyword; ?>" id="input-keyword" class="form-control" />
    <?php if ($error_keyword) { ?>
    <div class="text-danger"><?php echo $error_keyword; ?></div>
    <?php } ?>
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label" for="input-bottom"><span data-toggle="tooltip" title="<?php echo $help_bottom; ?>"><?php echo $entry_bottom; ?></span></label>
  <div class="col-sm-10">
    <div class="checkbox">
      <label>
        <?php if ($bottom) { ?>
        <input type="checkbox" name="bottom" value="1" checked="checked" id="input-bottom" />
        <?php } else { ?>
        <input type="checkbox" name="bottom" value="1" id="input-bottom" />
        <?php } ?>
        &nbsp; </label>
    </div>
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
  <div class="col-sm-10">
    <select name="status" id="input-status" class="form-control">
      <?php if ($status) { ?>
      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
      <option value="0"><?php echo $text_disabled; ?></option>
      <?php } else { ?>
      <option value="1"><?php echo $text_enabled; ?></option>
      <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
      <?php } ?>
    </select>
  </div>
</div>

I need to tell my vqmod XML to add a form group before the "input-bottom" form group. I could just do something like this:

<search position="before"><![CDATA[<div class="form-group">
            <label class="col-sm-2 control-label" for="input-bottom"]]></search>

But that looks pretty fragile to me. If the label class is changed or anything, it'd break. So is there a way I could use a regex or something to find the <div class="form-group"> immediately before the "input-bottom" label, regardless of the HTML between the div and the label's "for" attribute?

Or am I looking at this all wrong? Is there a better way to do what I'm trying to do? I'm pretty new to opencart and vqmod.


Solution

  • So I found this post on stackoverflow that helped me understand negated sets in lookaheads, and using the link in the same post, I tested this regex with this online tester:

    <div class="form-group">(?=[^\>]*?bottom)
    

    This matches <div class="form-group"> as long as the word "bottom" appears before next > symbol. That's about the best I could figure out.

    Then I went to try it in vqmod and noticed it didn't work... Upon closer inspection of vqmod's documentation, I learned it can only search one line...

    But using the "offset" attribute in vqmod, I was able to do this:

    <search position="before" offset="1"><![CDATA[for="input-bottom"]]></search>
    <add><![CDATA[my code]]></add>
    

    It searches for the line that has input-bottom and places my code 2 lines above it, skipping over the <div class="form-group"> line. That'll work for me.