voku / simple_html_dom

📜 Modern Simple HTML DOM Parser for PHP
MIT License
863 stars 118 forks source link

Question: parsing Laravel blade #86

Open lotsofbytes opened 2 years ago

lotsofbytes commented 2 years ago

A laravel blade file is not really a html file. It includes program directives for conditions and loops. But, because it starts with @ (like @foreach), the laravel directrives get escaped by domReplaceHelpers.

For example,

                 @foreach ($company->members as $m)
                    @if ($m->checkbox)
                      {{ $m->checkbox }}
                    @else
                      <span class="" title="{{ $m->profile }}">{{ $m->name }}</span>
                    @endif
                  @endforeach

will be parsed to:

                 ____SIMPLE_HTML_DOM__VOKU__AT____foreach ($company-&gt;members as $m)\n
                          ____SIMPLE_HTML_DOM__VOKU__AT____if ($m-&gt;checkbox)\n
                            {{ $m-&gt;checkbox }}\n
                          ____SIMPLE_HTML_DOM__VOKU__AT____else\n
                            <span class="" title="{{ $m-&gt;profile }}">{{ $m-&gt;name }}</span>\n
                          ____SIMPLE_HTML_DOM__VOKU__AT____endif\n
                        ____SIMPLE_HTML_DOM__VOKU__AT____endforeach\n

So, this works well for me to manipulate html parts of the blade file.

However, a case below messes it up:

        @for ($i = 2; $i <= 6; $i++)
            <div class="form-group">
              <label for="file{{ $i }}" class="col-md-2 control-label">添付
                {{ $i }}</label>
              <div class="col-md-10">
                <input type="file" name="file{{ $i }}" id="file{{ $i }}">
                <input id="filename{{ $i }}" type="hidden" name="filename{{ $i }}"
                  value="">
                {{ HTML::error($errors, 'file{$i}') }}
              </div>
            </div>
          @endfor

You see below, the for part gets truncated.

            ____SIMPLE_HTML_DOM__VOKU__AT____for ($i = 2; $i \n
                    <label for="file{{ $i }}" class="col-md-2 control-label">添付\n
                      {{ $i }}</label>\n
                    <div class="col-md-10">\n
                      <input type="file" name="file{{ $i }}" id="file{{ $i }}"/>\n
                      <input id="filename{{ $i }}" type="hidden" name="filename{{ $i }}" value=""/>\n
                      <x-error name="file{$i}"/>\n
                    </div>\n
                  </x-form></div>\n
                ____SIMPLE_HTML_DOM__VOKU__AT____endfor\n

I believe that because 'i <= 6' includesa html starting tag: <.

I wonder if there is anyway to prevent this from happening.

Thanks in advance.

lotsofbytes commented 2 years ago

@voku, Do you have any suggestion?