11define ( function ( require , exports , module ) {
22 const HTMLInstrumentation = require ( "LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation" ) ;
33 const LiveDevMultiBrowser = require ( "LiveDevelopment/LiveDevMultiBrowser" ) ;
4+ const CodeMirror = require ( "thirdparty/CodeMirror/lib/codemirror" ) ;
45
56 /**
67 * this is a helper function to find the content boundaries in HTML
@@ -82,36 +83,42 @@ define(function (require, exports, module) {
8283 return ;
8384 }
8485
85- // this will give us the start pos and end pos of the DOM element in the source code
86- // can be referenced using range.from and range.to
87- const range = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
88- if ( ! range ) {
86+ // get the start range from the getPositionFromTagId function
87+ // and we get the end range from the findMatchingTag function
88+ // NOTE: we cannot get the end range from getPositionFromTagId
89+ // because on non-beautified code getPositionFromTagId may not provide correct end position
90+ const startRange = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
91+ const endRange = CodeMirror . findMatchingTag ( editor . _codeMirror , editor . getCursorPos ( ) ) ;
92+
93+ if ( ! startRange || ! endRange ) {
8994 return ;
9095 }
96+ const startPos = startRange . from ;
97+ const endPos = endRange . close . to ;
9198
9299 // this is the actual source code for the element that we need to duplicate
93- const text = editor . getTextBetween ( range . from , range . to ) ;
100+ const text = editor . getTextBetween ( startPos , endPos ) ;
94101 // this is the indentation on the line
95- const indent = editor . getTextBetween ( { line : range . from . line , ch : 0 } , range . from ) ;
102+ const indent = editor . getTextBetween ( { line : startPos . line , ch : 0 } , startPos ) ;
96103
97104 editor . document . batchOperation ( function ( ) {
98105 // make sure there is only indentation and no text before it
99106 if ( indent . trim ( ) === "" ) {
100107 // this is the position where we need to insert
101108 // we're giving the char as 0 because since we insert a new line using '\n'
102- // that's why writing any char value will not work, as the line is empty
109+ // that's why writing any char value will not work, as the line is emptys
103110 // and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
104111 // So, the logic is to just append the indent before the text at this insertPos
105112 const insertPos = {
106- line : range . from . line + ( range . to . line - range . from . line + 1 ) ,
113+ line : startPos . line + ( endPos . line - startPos . line + 1 ) ,
107114 ch : 0
108115 } ;
109116
110- editor . replaceRange ( "\n" , range . to ) ;
117+ editor . replaceRange ( "\n" , endPos ) ;
111118 editor . replaceRange ( indent + text , insertPos ) ;
112119 } else {
113120 // if there is some text, we just add the duplicated text right next to it
114- editor . replaceRange ( text , range . from ) ;
121+ editor . replaceRange ( text , startPos ) ;
115122 }
116123 } ) ;
117124 }
@@ -132,21 +139,27 @@ define(function (require, exports, module) {
132139 return ;
133140 }
134141
135- // this will give us the start pos and end pos of the DOM element in the source code
136- // can be referenced using range.from and range.to
137- const range = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
138- if ( ! range ) {
142+ // get the start range from the getPositionFromTagId function
143+ // and we get the end range from the findMatchingTag function
144+ // NOTE: we cannot get the end range from getPositionFromTagId
145+ // because on non-beautified code getPositionFromTagId may not provide correct end position
146+ const startRange = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
147+ const endRange = CodeMirror . findMatchingTag ( editor . _codeMirror , editor . getCursorPos ( ) ) ;
148+
149+ if ( ! startRange || ! endRange ) {
139150 return ;
140151 }
152+ const startPos = startRange . from ;
153+ const endPos = endRange . close . to ;
141154
142155 editor . document . batchOperation ( function ( ) {
143- editor . replaceRange ( "" , range . from , range . to ) ;
156+ editor . replaceRange ( "" , startPos , endPos ) ;
144157
145158 // since we remove content from the source, we want to clear the extra line
146- if ( range . from . line !== 0 ) {
147- const prevLineText = editor . getLine ( range . from . line - 1 ) ;
159+ if ( startPos . line !== 0 && ! ( editor . getLine ( startPos . line ) . trim ( ) ) ) {
160+ const prevLineText = editor . getLine ( startPos . line - 1 ) ;
148161 const chPrevLine = prevLineText ? prevLineText . length : 0 ;
149- editor . replaceRange ( "" , { line : range . from . line - 1 , ch : chPrevLine } , range . from ) ;
162+ editor . replaceRange ( "" , { line : startPos . line - 1 , ch : chPrevLine } , startPos ) ;
150163 }
151164 } ) ;
152165 }
0 commit comments