Opened 10 years ago

Closed 10 years ago

#1068 closed defect (fixed)

Incorrect regex in TranformInnerHTML still adds newlines after </script> [solution]

Reported by: guest Owned by: akaEdge
Priority: high Milestone: Version 1.0
Component: Documentation Version:
Severity: normal Keywords: newlines, returns, javascript, script tags
Cc:

Description

[Suggested solution below]
There is an incorrect regular expression in TransformInnerHTML.js (nightly version downloaded 23rd August 07) which attempts to compensate for the newlines (returns) added before </script> tags in the source html. The line is line 138 of the .js and reads as follows:

replace(/[\r\n]+<\/script>/g,'\n</script>');//strip returns added into scripts

This doesn't work because the regex section [\r\n]+ doesn't catch the indenting on the </script> line (the prettified html source) and so never matches. Newlines are added every time the source is viewed, eventually making the source code unmanageably long.
The solution is to modify the regex as follows:

replace(/[\r\n]+(\s+)<\/script>/g,'\n$1</script>');//strip returns properly

The suggested solution adds (\s+) (search for the spaces before </script>) and stores the spaces in $1 (in order to preserve the indentation). The replace string \n$1</script> then adds a newline, the preserved indentation, and the close script tag. I have tested this solution with various pages containing scripts, and it works.

  • Geoffrey Kantaris

Change History (2)

comment:1 Changed 10 years ago by guest

In case the code blocks above aren't legible, here they are outside code blocks (the first one is illegible in IE for some reason):
Problem line:
replace(/[\r\n]+<\/script>/g,'\n</script>');strip returns added into scripts
Solution:
replace(/[\r\n]+(\s+)<\/script>/g,'\n$1</script>');
strip returns properly

comment:2 Changed 10 years ago by wymsy

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.