diff --git a/app/assets/javascripts/highlightTags.js b/app/assets/javascripts/highlightTags.js index bc533d4b7..3658c94c4 100644 --- a/app/assets/javascripts/highlightTags.js +++ b/app/assets/javascripts/highlightTags.js @@ -11,6 +11,13 @@ this.start = function(textarea) { + let visibleTextbox; + + this.highlightPlaceholders = ( + typeof textarea.data('highlightPlaceholders') === 'undefined' || + !!textarea.data('highlightPlaceholders') + ); + this.$textbox = $(textarea) .wrap(`
@@ -20,13 +27,19 @@ `)) .on("input", this.update); - this.initialHeight = this.$textbox.height(); + visibleTextbox = this.$textbox.clone().appendTo("body").css({ + position: 'absolute', + visibility: 'hidden', + display: 'block' + }); + this.initialHeight = visibleTextbox.height(); this.$background.css({ - 'width': this.$textbox.outerWidth(), 'border-width': this.$textbox.css('border-width') }); + visibleTextbox.remove(); + this.$textbox .trigger("input"); @@ -34,6 +47,8 @@ this.resize = () => { + this.$background.width(this.$textbox.outerWidth()); + this.$textbox.height( Math.max( this.initialHeight, @@ -47,17 +62,23 @@ }; - this.escapedMessage = () => $('').text(this.$textbox.val()).html(); + this.contentEscaped = () => $('').text(this.$textbox.val()).html(); - this.replacePlaceholders = () => this.$background.html( - this.escapedMessage().replace( - tagPattern, (match, name, separator, value) => value && separator ? - `((${name}??${value}))` : - `((${name}${value}))` - ) + this.contentReplaced = () => this.contentEscaped().replace( + tagPattern, (match, name, separator, value) => value && separator ? + `((${name}??${value}))` : + `((${name}${value}))` ); - this.update = () => this.replacePlaceholders() && this.resize(); + this.update = () => { + + this.$background.html( + this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped() + ); + + this.resize(); + + }; }; diff --git a/app/templates/components/textbox.html b/app/templates/components/textbox.html index fc14d6dc4..27f1ba378 100644 --- a/app/templates/components/textbox.html +++ b/app/templates/components/textbox.html @@ -4,6 +4,7 @@ hint=False, highlight_tags=False, autofocus=False, + autosize=False, colour_preview=False, help_link=None, help_link_text=None, @@ -47,7 +48,8 @@ %} {{ field( class=field_class, - data_module='highlight-tags' if highlight_tags else '', + data_module='highlight-tags' if highlight_tags or autosize else '', + data_highlight_placeholders='true' if highlight_tags else 'false', rows=rows|string, **kwargs ) }} diff --git a/tests/javascripts/highlightTags.test.js b/tests/javascripts/highlightTags.test.js index cadac0b9b..a14779230 100644 --- a/tests/javascripts/highlightTags.test.js +++ b/tests/javascripts/highlightTags.test.js @@ -125,6 +125,34 @@ describe('Highlight tags', () => { }); + describe("The element's width should match even when the textbox is initially hidden", () => { + + beforeEach(() => { + + let setDisplayPropertyOfFormGroups = function(property) { + Array.prototype.forEach.call( + document.getElementsByClassName('form-group'), + element => element.style.display = property + ); + }; + + setDisplayPropertyOfFormGroups('none'); + + window.GOVUK.modules.start(); + + setDisplayPropertyOfFormGroups('block'); + + }); + + test("If the textbox is an