From 9c4de58161879b4c4d26fc6cc513f7aabf2892e5 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 6 Jul 2024 13:22:47 +1200 Subject: [PATCH 1/3] Limit htmlString to the first element ONLY --- shared/naturalcrit/markdown.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 529129833..cabb73c89 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -788,10 +788,11 @@ const processStyleTags = (string)=>{ }; const extractHTMLStyleTags = (htmlString)=>{ - const id = htmlString.match(/id="([^"]*)"/)?.[1] || null; - const classes = htmlString.match(/class="([^"]*)"/)?.[1] || null; - const styles = htmlString.match(/style="([^"]*)"/)?.[1] || null; - const attributes = htmlString.match(/[a-zA-Z]+="[^"]*"/g) + const firstElementOnly = htmlString.indexOf('>') > 0 ? htmlString.substring(0, htmlString.indexOf('>')) : htmlString; + const id = firstElementOnly.match(/id="([^"]*)"/)?.[1] || null; + const classes = firstElementOnly.match(/class="([^"]*)"/)?.[1] || null; + const styles = firstElementOnly.match(/style="([^"]*)"/)?.[1] || null; + const attributes = firstElementOnly.match(/[a-zA-Z]+="[^"]*"/g) ?.filter((attr)=>!attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="')) .reduce((obj, attr)=>{ const index = attr.indexOf('='); From 5433cda52fb1493ea25139a363e5714d4dca8a4c Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 6 Jul 2024 17:05:23 -0400 Subject: [PATCH 2/3] Add test case --- tests/markdown/mustache-syntax.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/markdown/mustache-syntax.test.js b/tests/markdown/mustache-syntax.test.js index 7b0115cae..3f7f2529b 100644 --- a/tests/markdown/mustache-syntax.test.js +++ b/tests/markdown/mustache-syntax.test.js @@ -333,6 +333,13 @@ describe('Injection: When an injection tag follows an element', ()=>{ expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('

text{background:blue}

'); }); + it('Renders an parent and child element, each modified by an injector', function() { + const source = dedent`**bolded text**{color:red} + {color:blue}`; + const rendered = Markdown.render(source).trimReturns(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('

bolded text

'); + }); + it('Renders an image with added attributes', function() { const source = `![homebrew mug](https://i.imgur.com/hMna6G0.png) {position:absolute,bottom:20px,left:130px,width:220px,a="b and c",d=e}`; const rendered = Markdown.render(source).trimReturns(); From 0a199e750fa6c445e126f00ed491d6fa6e75febe Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 6 Jul 2024 18:00:18 -0400 Subject: [PATCH 3/3] Simplify string splitting code String.split will return the substring before > or the whole string if no > exists. --- shared/naturalcrit/markdown.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index cabb73c89..39939f306 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -787,8 +787,9 @@ const processStyleTags = (string)=>{ }; }; +//Given a string representing an HTML element, extract all of its properties (id, class, style, and other attributes) const extractHTMLStyleTags = (htmlString)=>{ - const firstElementOnly = htmlString.indexOf('>') > 0 ? htmlString.substring(0, htmlString.indexOf('>')) : htmlString; + const firstElementOnly = htmlString.split('>')[0]; const id = firstElementOnly.match(/id="([^"]*)"/)?.[1] || null; const classes = firstElementOnly.match(/class="([^"]*)"/)?.[1] || null; const styles = firstElementOnly.match(/style="([^"]*)"/)?.[1] || null;