Advertisement:

Author Topic: Code: [Select] function not working in recent web browsers  (Read 16498 times)

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Code: [Select] function not working in recent web browsers
« on: April 23, 2017, 08:46:41 AM »
The "Code: [Select]" function in SMF 2 has for as long as I can remember always displayed "javascript:void(0);" at the bottom left of screen in Firefox/Chromium/Chrome (and probably others) but the function still worked .. ie. when you clicked on "Select" all the text in the code box got selected.

Now it does not, you still get the "javascript:void(0);" message, but clicking "Select" does nothing .. the text in the code box does not get selected.

This seems to be related to web browser version, as if I drop back to Firefox 51 the function works, but it does not work in Firefox 53.

[SMF 2.0.13]
(and this forum .. 2.0.14)
Code: [Select]
test here
« Last Edit: July 30, 2018, 02:00:15 PM by Gwenwyfar »

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 69,123
    • StoryBB/StoryBB on GitHub
Re: Code: [Select] function not working in recent web browsers
« Reply #1 on: April 23, 2017, 08:48:44 AM »
Does your site use HTTPS?
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

Nothing turns to hate so bitter as what once was love.

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Re: Code: [Select] function not working in recent web browsers
« Reply #2 on: April 23, 2017, 08:50:40 AM »
It does yes .. but as I said the function works in Firefox 51 and earlier versions of Chromium/Chrome. It just seems to be in recent browsers.

Here's an example:
Code: [Select]
https://forum.peppermintos.com/index.php/topic,5420.msg54342.html#msg54342

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 69,123
    • StoryBB/StoryBB on GitHub
Re: Code: [Select] function not working in recent web browsers
« Reply #3 on: April 23, 2017, 08:58:20 AM »
I was half expecting you to say no. Firefox has been changing its requirements so that higher or more sensitive features require HTTPS and potentially that would have been one of them. But it's not that.

Hmm, it still works on Chrome 57, and in Firefox 52, but you're right it's broken in FF 53. I wonder what they changed that broke.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

Nothing turns to hate so bitter as what once was love.

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Re: Code: [Select] function not working in recent web browsers
« Reply #4 on: April 23, 2017, 09:08:41 AM »
You're also right, it's working here too in Chromium 57 .. so it appears to be just Firefox 53 where it's broken ???

Offline Illori

  • Project Manager
  • SMF Master
  • *
  • Posts: 49,594
Re: Code: [Select] function not working in recent web browsers
« Reply #5 on: April 23, 2017, 10:56:49 AM »
console says
Quote
IndexSizeError: Index or size is negative or greater than the allowed amount script.js?fnl (line 1366)
Code: [Select]
// Firefox at el.
else if (window.getSelection)
{
var oCurSelection = window.getSelection();
// Safari is special!
if (oCurSelection.setBaseAndExtent)
{
var oLastChild = oCodeArea.lastChild;
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
}
else
{
var curRange = document.createRange();
curRange.selectNodeContents(oCodeArea);

oCurSelection.removeAllRanges();
oCurSelection.addRange(curRange);
}
}

1366 is
Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
i wonder if we no longer need the code just for firefox.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 69,123
    • StoryBB/StoryBB on GitHub
Re: Code: [Select] function not working in recent web browsers
« Reply #6 on: April 23, 2017, 10:59:26 AM »
Would need to be tested against Safari (incl. Mobile Safari) as well if it were changed, as well as older Firefox.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

Nothing turns to hate so bitter as what once was love.

Offline Illori

  • Project Manager
  • SMF Master
  • *
  • Posts: 49,594
Re: Code: [Select] function not working in recent web browsers
« Reply #7 on: April 23, 2017, 12:38:04 PM »
interesting that code is the same in SMF 2.1 but it does not have this issue.

Offline fugenkomponist

  • Newbie
  • *
  • Posts: 1
Re: Code: [Select] function not working in recent web browsers
« Reply #8 on: May 04, 2017, 03:52:35 AM »
1366 is
Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
If you replace 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length by 1 you’ll see that Firefox selects the first line and Chromium more but not always the whole text (I assume something like “until the first closing double quote”). So it seems like this expression gives a node index out of range and different browsers count differently …

As a workaround you can switch back to the old behaviour for Firefox:
Code: [Select]
// Safari is special!
if (oCurSelection.setBaseAndExtent && !is_ff)
{
setBaseAndExtent is supported in Firefox 53 (see release notes at developer.mozilla.org/en-US/Firefox/Releases/53) so I think this is why the behaviour changed in this version.
« Last Edit: May 04, 2017, 04:26:02 AM by fugenkomponist »

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Re: Code: [Select] function not working in recent web browsers
« Reply #9 on: May 04, 2017, 12:05:37 PM »
Code:[Select] now appears to be broken in Chrome/Chromium 58 too .. at least on SMF 2.0.13 (but not here on 2.0.14 ???)

Test URL
Code: [Select]
https://forum.peppermintos.com/index.php/topic,5485.msg54966.html#msg54966
Chromium 58.0.3029.81 console reads
Code: [Select]
Uncaught DOMException: Failed to execute 'setBaseAndExtent' on 'Selection': There is no child at offset 183.
    at smfSelectText (eval at <anonymous> (https://forum.peppermintos.com/index.php/topic,5485.msg54966.html:1:1), <anonymous>:47:407)
    at HTMLAnchorElement.onclick (https://forum.peppermintos.com/index.php/topic,5485.msg54966.html#msg54966:386:485)

Chrome 58.0.3029.96 is indicating the problem lies at the line highlighted in red below
Quote
// Firefox at el.
   else if (window.getSelection)
   {
      var oCurSelection = window.getSelection();
      // Safari is special!
      if (oCurSelection.setBaseAndExtent)
      {
         var oLastChild = oCodeArea.lastChild;
         oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
      }
      else
      {
         var curRange = document.createRange();
         curRange.selectNodeContents(oCodeArea);

         oCurSelection.removeAllRanges();
         oCurSelection.addRange(curRange);
      }
   }

For clarity, this is WITHOUT fugenkomponist's workaround .. which I only just spotted, and haven't tried yet as I'm assuming it's a Firefox specific workaround (?) but the problem now isn't.
« Last Edit: May 04, 2017, 12:46:02 PM by PCNetSpec »

Offline Gluz

  • Native Language Support Specialist
  • Sr. Member
  • *
  • Posts: 985
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #10 on: May 06, 2017, 11:48:08 PM »
I've seen this, and after looking through the code I see that this part:

Code: [Select]
// Safari is special!
if (oCurSelection.setBaseAndExtent)
{
var oLastChild = oCodeArea.lastChild;
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
}

Is intended for Safari, but since Firefox 52 they added this:"setBaseAndExtent"

I tested something by adding is_safari to that if, similar to the change fugenkomponist did but to check only for Safari instead filter out only Firefox, since that part is intended for Safari. That makes it work in Firefox 53 and Chrome 58 as intended.

The code looks like this:
Code: [Select]
// Safari is special!
if (oCurSelection.setBaseAndExtent && is_safari)
{
var oLastChild = oCodeArea.lastChild;
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
}

Those browsers are adding support for setBaseAndExtent but is broken, and all software that uses that is getting this errors.
Need help with PHP, HTML, CSS, Themes or MODs?

Quote
Think in spanish, habla en inglés.

Offline Linkjay

  • Jr. Member
  • **
  • Posts: 206
  • Gender: Male
  • eh
    • My Homepage.
Re: Code: [Select] function not working in recent web browsers
« Reply #11 on: May 07, 2017, 01:20:56 AM »
Broken here too. I am on FF 53.
I play games in my free time and volunteer my knowledge and support to the gaming communities of the internet.

You can contact me by these methods:
Use my Contact Script • PM me here • Add me on Steam

Offline tfs

  • Full Member
  • ***
  • Posts: 424
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #12 on: July 06, 2017, 08:56:16 PM »
Has there been any change on this issue? I'm using SMF 2.14 and FF 54.0.1 (32-bit), and the select option is not working. It works in IE. My site is using HTTPS exclusively.
A good tree cannot bring forth evil fruit, neither can an evil tree bring forth good fruit.

Offline FragaCampos

  • Sophist Member
  • *****
  • Posts: 1,281
  • Gender: Male
    • docsPT
Re: Code: [Select] function not working in recent web browsers
« Reply #13 on: July 11, 2017, 05:51:57 PM »
I'm using FF 54.0.1 (32-bit) and SMF 2.0.13 and the select option doesn't work either.

Offline tfs

  • Full Member
  • ***
  • Posts: 424
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #14 on: August 10, 2017, 08:37:44 PM »
Looks like it's also broken on my iPhone with Safari.
A good tree cannot bring forth evil fruit, neither can an evil tree bring forth good fruit.

Offline GravuTrad

  • Senior Translator
  • SMF Hero
  • *
  • Posts: 8,602
  • Gender: Male
  • One of the french SMF translators
Re: Code: [Select] function not working in recent web browsers
« Reply #15 on: August 11, 2017, 05:50:07 AM »
I'm using FF 54.0.1 (32-bit) and SMF 2.0.13 and the select option doesn't work either.

Same thing
On a toujours besoin d'un plus petit que soi! (Petit!Petit!)


Think about Search function before posting.
Pensez à la fonction Recherche avant de poster.

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Re: Code: [Select] function not working in recent web browsers
« Reply #16 on: September 21, 2017, 12:38:03 PM »
Any movement on this issue ?

It seems to be broken in both Firefox 55 and Chromium 60 .. although it is still working in Chrome 58 on this forum (SMF 2.0.14), but not on my forum (SMF 2.0.13) :-\
« Last Edit: September 21, 2017, 12:48:21 PM by PCNetSpec »

Offline tfs

  • Full Member
  • ***
  • Posts: 424
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #17 on: November 15, 2017, 11:10:57 AM »
Still broken on Firefox Quantum 57.0 (x64)

I miss this feature!
A good tree cannot bring forth evil fruit, neither can an evil tree bring forth good fruit.

Offline Krawieco

  • Newbie
  • *
  • Posts: 1
Re: Code: [Select] function not working in recent web browsers
« Reply #18 on: January 21, 2018, 03:08:56 AM »
Still broken on Firefox Quantum 57.0 (x64)

I miss this feature!

Hey tfs, did you get it working yet? Because I haven't been able to.  :-[

Offline butch2k

  • Full Member
  • ***
  • Posts: 416
  • Gender: Male
    • @butch2k on Twitter
    • Planète Roliste
Re: Code: [Select] function not working in recent web browsers
« Reply #19 on: January 21, 2018, 04:08:54 AM »
The "Code: [Select]" function in SMF 2 has for as long as I can remember always displayed "javascript:void(0);" at the bottom left of screen in Firefox/Chromium/Chrome (and probably others) but the function still worked .. ie. when you clicked on "Select" all the text in the code box got selected.

Now it does not, you still get the "javascript:void(0);" message, but clicking "Select" does nothing .. the text in the code box does not get selected.

This seems to be related to web browser version, as if I drop back to Firefox 51 the function works, but it does not work in Firefox 53.

[SMF 2.0.13]
(and this forum .. 2.0.14)
Code: [Select]
test here
I do not use the same code anymore but i encountered a similar issue with mine.
IIRC you should not use textContent.length which is the content length measured in characters BUT the number of text lines (number of nodes). It used to work even if the number used was superior but not anymore, you should use a number <= to the number of lines in the text being selected.

Try this
Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, oCodeArea.childElementCount);
instead of

Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
Let me know if it works as i'm unable to test it right now.

Online tinoest

  • Development Contributor
  • Jr. Member
  • *
  • Posts: 228
Re: Code: [Select] function not working in recent web browsers
« Reply #20 on: January 21, 2018, 12:34:56 PM »
Try this
Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, oCodeArea.childElementCount);
instead of

Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
Let me know if it works as i'm unable to test it right now.

That appears to work correctly.

Offline butch2k

  • Full Member
  • ***
  • Posts: 416
  • Gender: Male
    • @butch2k on Twitter
    • Planète Roliste
Re: Code: [Select] function not working in recent web browsers
« Reply #21 on: January 21, 2018, 01:58:16 PM »
Thanks a lot  :)
It should work with most browsers (except the IEs before IE9)

Offline tfs

  • Full Member
  • ***
  • Posts: 424
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #22 on: January 21, 2018, 11:36:22 PM »
Still broken on Firefox Quantum 57.0 (x64)

I miss this feature!

Hey tfs, did you get it working yet? Because I haven't been able to.  :-[

Still broken. I've long given up on it being fixed. It seems to be something that isn't important enough to warrant a patch. I do see people posting possible fixes, though I don't think I have seen anyone post what actual file they propose changing. :)
A good tree cannot bring forth evil fruit, neither can an evil tree bring forth good fruit.

Offline butch2k

  • Full Member
  • ***
  • Posts: 416
  • Gender: Male
    • @butch2k on Twitter
    • Planète Roliste
Re: Code: [Select] function not working in recent web browsers
« Reply #23 on: January 22, 2018, 01:19:56 AM »
Function smfSelectText() in script.js

Offline tfs

  • Full Member
  • ***
  • Posts: 424
  • Gender: Male
Re: Code: [Select] function not working in recent web browsers
« Reply #24 on: January 22, 2018, 03:25:56 PM »
I do not use the same code anymore but i encountered a similar issue with mine.
IIRC you should not use textContent.length which is the content length measured in characters BUT the number of text lines (number of nodes). It used to work even if the number used was superior but not anymore, you should use a number <= to the number of lines in the text being selected.

Try this
Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, oCodeArea.childElementCount);
instead of

Code: [Select]
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
Let me know if it works as i'm unable to test it right now.

The above change to [\Themes\default\scripts\script.js] works for a single line of code, but with a code section that has multiple lines it only selects the first line.

Code: [Select]
Single Line Works
Code: [Select]
Multiple Lines
Do not work.
Only selects line one
A good tree cannot bring forth evil fruit, neither can an evil tree bring forth good fruit.

Offline butch2k

  • Full Member
  • ***
  • Posts: 416
  • Gender: Male
    • @butch2k on Twitter
    • Planète Roliste
Re: Code: [Select] function not working in recent web browsers
« Reply #25 on: January 22, 2018, 04:03:37 PM »
This should work fine with FF and Chrome

Code: [Select]
function smfSelectText(oCurElement, bActOnElement)
{
// The place we're looking for is one div up, and next door - if it's auto detect.
if (typeof(bActOnElement) == 'boolean' && bActOnElement)
var oCodeArea = document.getElementById(oCurElement);
else
var oCodeArea = oCurElement.parentNode.nextSibling;

if (typeof(oCodeArea) != 'object' || oCodeArea == null)
return false;

// Start off with my favourite, internet explorer.
if ('createTextRange' in document.body)
{
var oCurRange = document.body.createTextRange();
oCurRange.moveToElementText(oCodeArea);
oCurRange.select();
}
// Firefox at el.
else if (window.getSelection)
{
var oCurSelection = window.getSelection();
// Safari is special!
if (oCurSelection.selectAllChildren)
{
oCurSelection.selectAllChildren(oCodeArea);
}
else
{
var curRange = document.createRange();
curRange.selectNodeContents(oCodeArea);

oCurSelection.removeAllRanges();
oCurSelection.addRange(curRange);
}
}

return false;
}

Note
I replaced setBaseAndExtent by selectAllChildren because Chrome's behavior differs heavily from FF.

Indeed, according to setBaseAndExtent MDN (Mozilla) spec and introduced with FF 53
focusOffset
The number of child nodes from the start of the focus node that should be included in the selection. So for example, if the value is 0 the whole node is excluded. If the value is 1, the first child node is included. And so on.


If you got three lines of text in the code block there are 5 nodes: 3 for the text and two for the BRs, if you set focusOffset to 5 FF select all three lines

The official Spec, on which Chrome is based, is not that clear on what the focusOffset really is when both text nodes and tags are mixed within the content container. And clearly Chrome implemented it very differently from FF. So setBaseAndExtent is a no go at this time.



« Last Edit: January 22, 2018, 04:42:59 PM by butch2k »

Offline nesquik91

  • Newbie
  • *
  • Posts: 3
Re: Code: [Select] function not working in recent web browsers
« Reply #26 on: February 06, 2018, 12:40:33 PM »
Hello!

which is the"php" file where this code should be modified?

Thanks.

Nucky

Offline butch2k

  • Full Member
  • ***
  • Posts: 416
  • Gender: Male
    • @butch2k on Twitter
    • Planète Roliste
Re: Code: [Select] function not working in recent web browsers
« Reply #27 on: February 06, 2018, 03:45:36 PM »
Hello!

which is the"php" file where this code should be modified?

Thanks.

Nucky
It's not a php file, it's script.js

Offline nesquik91

  • Newbie
  • *
  • Posts: 3
Re: Code: [Select] function not working in recent web browsers
« Reply #28 on: February 06, 2018, 03:47:58 PM »
Hy,

which js script?

Offline Illori

  • Project Manager
  • SMF Master
  • *
  • Posts: 49,594
Re: Code: [Select] function not working in recent web browsers
« Reply #29 on: February 06, 2018, 03:49:02 PM »
the file name is script.js

Offline nesquik91

  • Newbie
  • *
  • Posts: 3
Re: Code: [Select] function not working in recent web browsers
« Reply #30 on: February 06, 2018, 03:53:14 PM »
Ok. I understand. I have an smf forum and the post does not work with the selection function and I thought it would explain ...

Offline GravuTrad

  • Senior Translator
  • SMF Hero
  • *
  • Posts: 8,602
  • Gender: Male
  • One of the french SMF translators
Re: Code: [Select] function not working in recent web browsers
« Reply #31 on: February 06, 2018, 07:38:17 PM »
Located here

Quote
The above change to \Themes\default\scripts\script.js works for a single line of code, but with a code section that has multiple lines it only selects the first line.
On a toujours besoin d'un plus petit que soi! (Petit!Petit!)


Think about Search function before posting.
Pensez à la fonction Recherche avant de poster.

Offline PCNetSpec

  • Newbie
  • *
  • Posts: 8
Re: Code: [Select] function not working in recent web browsers
« Reply #32 on: March 06, 2018, 04:54:44 PM »
This should work fine with FF and Chrome

Code: [Select]
function smfSelectText(oCurElement, bActOnElement)
{
// The place we're looking for is one div up, and next door - if it's auto detect.
if (typeof(bActOnElement) == 'boolean' && bActOnElement)
var oCodeArea = document.getElementById(oCurElement);
else
var oCodeArea = oCurElement.parentNode.nextSibling;

if (typeof(oCodeArea) != 'object' || oCodeArea == null)
return false;

// Start off with my favourite, internet explorer.
if ('createTextRange' in document.body)
{
var oCurRange = document.body.createTextRange();
oCurRange.moveToElementText(oCodeArea);
oCurRange.select();
}
// Firefox at el.
else if (window.getSelection)
{
var oCurSelection = window.getSelection();
// Safari is special!
if (oCurSelection.selectAllChildren)
{
oCurSelection.selectAllChildren(oCodeArea);
}
else
{
var curRange = document.createRange();
curRange.selectNodeContents(oCodeArea);

oCurSelection.removeAllRanges();
oCurSelection.addRange(curRange);
}
}

return false;
}

Note
I replaced setBaseAndExtent by selectAllChildren because Chrome's behavior differs heavily from FF.

Indeed, according to setBaseAndExtent MDN (Mozilla) spec and introduced with FF 53
focusOffset
The number of child nodes from the start of the focus node that should be included in the selection. So for example, if the value is 0 the whole node is excluded. If the value is 1, the first child node is included. And so on.


If you got three lines of text in the code block there are 5 nodes: 3 for the text and two for the BRs, if you set focusOffset to 5 FF select all three lines

The official Spec, on which Chrome is based, is not that clear on what the focusOffset really is when both text nodes and tags are mixed within the content container. And clearly Chrome implemented it very differently from FF. So setBaseAndExtent is a no go at this time.

Worked perfectly, many thanks butch2k :)