mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-04 12:32:39 +00:00
Added additional tests for injection
This commit is contained in:
@@ -754,7 +754,7 @@ const extractHTMLStyleTags = (htmlString)=> {
|
|||||||
const id = htmlString.match(/id="([^"]*)"/)?.[1] || null;
|
const id = htmlString.match(/id="([^"]*)"/)?.[1] || null;
|
||||||
const classes = htmlString.match(/class="([^"]*)"/)?.[1] || null;
|
const classes = htmlString.match(/class="([^"]*)"/)?.[1] || null;
|
||||||
const styles = htmlString.match(/style="([^"]*)"/)?.[1] || null;
|
const styles = htmlString.match(/style="([^"]*)"/)?.[1] || null;
|
||||||
const attributes = htmlString.match(/([a-z]+="[^"]*)"/g)
|
const attributes = htmlString.match(/[a-zA-Z]+="[^"]*"/g)
|
||||||
?.filter(attr => !attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="'))
|
?.filter(attr => !attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="'))
|
||||||
.reduce((obj, attr) => {
|
.reduce((obj, attr) => {
|
||||||
let [key, value] = attr.split("=");
|
let [key, value] = attr.split("=");
|
||||||
|
|||||||
@@ -279,6 +279,36 @@ describe('Injection: When an injection tag follows an element', ()=>{
|
|||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" style="color:red; background:blue;">text</span>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" style="color:red; background:blue;">text</span>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own ID, overwritten with an injected ID', function() {
|
||||||
|
const source = '{{#oldId text}}{#newId}';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" id="newId">text</span>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own attributes, overwritten with an injected attribute, plus a new one', function() {
|
||||||
|
const source = '{{attrA="old",attrB="old" text}}{attrA="new",attrC="new"}';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" attrA="new" attrB="old" attrC="new">text</span>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own attributes, overwritten with an injected attribute, ignoring "class", "style", and "id"', function() {
|
||||||
|
const source = '{{attrA="old",attrB="old" text}}{attrA="new",attrC="new",class="new",style="new",id="new"}';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" attrA="new" attrB="old" attrC="new">text</span>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own styles, appended with injected styles', function() {
|
||||||
|
const source = '{{color:blue,height:10px text}}{width:10px,color:red}';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block" style="color:blue; height:10px; width:10px; color:red;">text</span>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own classes, appended with injected classes', function() {
|
||||||
|
const source = '{{classA,classB text}}{classA,classC}';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<span class="inline-block classA classB classA classC">text</span>');
|
||||||
|
});
|
||||||
|
|
||||||
it('Renders an emphasis element with injected Class name', function() {
|
it('Renders an emphasis element with injected Class name', function() {
|
||||||
const source = '*emphasis*{big}';
|
const source = '*emphasis*{big}';
|
||||||
const rendered = Markdown.render(source).trimReturns();
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
@@ -340,13 +370,58 @@ describe('Injection: When an injection tag follows an element', ()=>{
|
|||||||
|
|
||||||
it('renders a div "text" with injected variable string', function() {
|
it('renders a div "text" with injected variable string', function() {
|
||||||
const source = dedent`{{
|
const source = dedent`{{
|
||||||
text
|
text
|
||||||
}}
|
}}
|
||||||
{--stringVariable:"'string'"}`;
|
{--stringVariable:"'string'"}`;
|
||||||
const rendered = Markdown.render(source).trimReturns();
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<div class="block" style="--stringVariable:'string';"><p>text</p></div>`);
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<div class="block" style="--stringVariable:'string';"><p>text</p></div>`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own ID, overwritten with an injected ID', function() {
|
||||||
|
const source = dedent`{{#oldId
|
||||||
|
text
|
||||||
|
}}
|
||||||
|
{#newId}`;
|
||||||
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<div class="block" id="newId"><p>text</p></div>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own attributes, overwritten with an injected attribute, plus a new one', function() {
|
||||||
|
const source = dedent`{{attrA="old",attrB="old"
|
||||||
|
text
|
||||||
|
}}
|
||||||
|
{attrA="new",attrC="new"}`;
|
||||||
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<div class="block" attrA="new" attrB="old" attrC="new"><p>text</p></div>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own attributes, overwritten with an injected attribute, ignoring "class", "style", and "id"', function() {
|
||||||
|
const source = dedent`{{attrA="old",attrB="old"
|
||||||
|
text
|
||||||
|
}}
|
||||||
|
{attrA="new",attrC="new",class="new",style="new",id="new"}`;
|
||||||
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<div class="block" attrA="new" attrB="old" attrC="new"><p>text</p></div>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own styles, appended with injected styles', function() {
|
||||||
|
const source = dedent`{{color:blue,height:10px
|
||||||
|
text
|
||||||
|
}}
|
||||||
|
{width:10px,color:red}`;
|
||||||
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<div class="block" style="color:blue; height:10px; width:10px; color:red;"><p>text</p></div>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Renders a span "text" with its own classes, appended with injected classes', function() {
|
||||||
|
const source = dedent`{{classA,classB
|
||||||
|
text
|
||||||
|
}}
|
||||||
|
{classA,classC}`;
|
||||||
|
const rendered = Markdown.render(source).trimReturns();
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<div class="block classA classB classA classC"><p>text</p></div>');
|
||||||
|
});
|
||||||
|
|
||||||
it('renders an h2 header "text" with injected class name', function() {
|
it('renders an h2 header "text" with injected class name', function() {
|
||||||
const source = dedent`## text
|
const source = dedent`## text
|
||||||
{ClassName}`;
|
{ClassName}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user