Skip to content

Commit

Permalink
fix raw tag parsing to include multi-line HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
SampsonCrowley committed May 3, 2019
1 parent a60ce31 commit f43db16
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ Here are the names of the defaults:
}
```

## Raw HTML

If you need to include raw html, you can wrap raw content in `<raw>` tags

```html
<raw>
<button></button>
<asdf></asdf>
</raw>
```

This is a feature intended for advanced users. You will be responsible for ensuring all markup between `<raw>` tags is valid. All content after an opening `<raw>` tag will be inserted "As Is" until the next closing `</raw>` tag.

This means that `<raw>` tags CANNOT be nested

## Programmatic Use

The Inky parser can be accessed directly for programmatic use.
Expand Down
2 changes: 1 addition & 1 deletion lib/inky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def transform_doc(elem)
def self.extract_raws(string)
raws = []
i = 0
regex = %r(< *raw *>(.*?)</ *raw *>)
regex = %r(<\s*raw\s*>((?!</\s*raw\s*>).*?)</\s*raw\s*>)miu
str = string
while raw = str.match(regex)
raws[i] = raw[1]
Expand Down
63 changes: 63 additions & 0 deletions spec/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,67 @@
output = inky.release_the_kraken(input)
expect(output).to eql(expected)
end

it 'works on multiple lines' do
input = <<-HTML
<body>
<raw>
<<LCG ProgramTG LCG Coupon Code Default='246996'>>\
<button>
</raw>
</body>
HTML

# Can't do vanilla compare because the second will fail to parse
inky = Inky::Core.new
output = inky.release_the_kraken(input)
expect(output).to include("<<LCG ProgramTG LCG Coupon Code Default='246996'>>")
expect(output).to include("<button>")
expect(output).to_not include('raw')
expect(output).to_not include('<table class="button">')
end

it 'stops at the first </raw> tag' do
input = <<-HTML
<body>
<raw>
<<LCG ProgramTG LCG Coupon Code Default='246996'>>
</raw>
<button href="#">Test</button>
</raw>
</body>
HTML
expected = "<<LCG ProgramTG LCG Coupon Code Default='246996'>>"

# Can't do vanilla compare because the second will fail to parse
inky = Inky::Core.new
output = inky.release_the_kraken(input)
expect(output).to include(expected)
expect(output).to include('<table class="button">')
expect(output).to_not include('<button>')
end

it 'matches all raw tags' do
first = "<<LCG ProgramTG LCG Coupon Code Default='246996'>>"
second = "<asdf>more raw content</asdf>"
input = <<-HTML
<body>
<raw>
#{first}
</raw>
<button href="#">Test</button>
<raw>
#{second}
</raw>
</body>
HTML

# Can't do vanilla compare because the second will fail to parse
inky = Inky::Core.new
output = inky.release_the_kraken(input)
expect(output).to include(first)
expect(output).to include(second)
expect(output).to include('<table class="button">')
expect(output).to_not include('<button>')
end
end

0 comments on commit f43db16

Please sign in to comment.