Necesita un gurú js que me ayude con esto
- mindfullsilence
- Professor


- Registrado: Ago 04, 2008
- Mensajes: 846
- Status: Offline
Así que Ive decidió que chupar en javascript. Im así trabajando en un proyecto personal mejor. Im tratando de crear un script autoclose voluntad un elemento html que ha sido escrito en un textarea. Ive llegado tan cerca y, a continuación, me golpeó un muro de ladrillo. Puede ver el ejemplo aquí: demo.
Es lo que debería para suceder es que la secuencia de comandos se ejecuta cada vez que alguien golpea la backslash("/"). Después de que se desencadene el evento, debe anexar todo lo que es el último elemento abierto.
Im cargar mi texto en la función hasta donde está situado el cursor. A continuación, encontrar todos los elementos html y les pegue en una matriz que se inserta en otra función. La segunda función coincide cada elemento en pares y escupe el resto (la etiqueta abierta).
Puede ver la secuencia de comandos: script
Mi problema es que si incrustar un par de elementos y Inicio cerrarlas, termino con algo como "<///p>" en lugar de "</p>".
¿Alguna idea?
Es lo que debería para suceder es que la secuencia de comandos se ejecuta cada vez que alguien golpea la backslash("/"). Después de que se desencadene el evento, debe anexar todo lo que es el último elemento abierto.
Im cargar mi texto en la función hasta donde está situado el cursor. A continuación, encontrar todos los elementos html y les pegue en una matriz que se inserta en otra función. La segunda función coincide cada elemento en pares y escupe el resto (la etiqueta abierta).
Puede ver la secuencia de comandos: script
Mi problema es que si incrustar un par de elementos y Inicio cerrarlas, termino con algo como "<///p>" en lugar de "</p>".
¿Alguna idea?
Use your words like arrows to shoot toward your goal.
- Anonymous
- Bot


- Registrado: 25 Feb 2008
- Mensajes: ?
- Loc: Ozzuland
- Status: Online
Agosto 4th, 2011, 8:29 pm
- SpooF
- ٩๏̯͡๏۶


- Registrado: May 22, 2004
- Mensajes: 3415
- Loc: Richland, WA
- Status: Offline
Que a través de este conjunto. Creo que funciona.
JAVASCRIPT Código: [ Select ]
function priorElement(html,cursor_pos){
var re = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi
var matches = html.match(re,html)
var elements = [];
var positions = [];
// Run through all the matches
for ( x in matches )
{
// Strip all the randon stuff so we just have the element.
var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
// If it has a trailing backslash ignore it
if(stripped_element.match(/\w\//)) // may want to look for these instead like in your script area|base|basefont|br|hr|input|img|link|meta
continue;
// Get rid of the `>` so we can match Ex. <a href="" class=""> turns, we need to
// matchs `<a` and not `<a>`
var stripped_element = stripped_element.substring(0,stripped_element.length-1)
positions.push(html.search(stripped_element));
elements.push(stripped_element);
}
if(positions && elements) {
var element = 0;
for(var i in positions)
{
if(positions[i] > cursor_pos)
{
// Find the element right before our cursor
element = i - 1;
break;
}
}
if(elements[element] == undefined)
return;
// return just the element, a, center, div, span...
return elements[element].replace(/[\<\/\>]/,'');
}
return;
}
var re = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi
var matches = html.match(re,html)
var elements = [];
var positions = [];
// Run through all the matches
for ( x in matches )
{
// Strip all the randon stuff so we just have the element.
var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
// If it has a trailing backslash ignore it
if(stripped_element.match(/\w\//)) // may want to look for these instead like in your script area|base|basefont|br|hr|input|img|link|meta
continue;
// Get rid of the `>` so we can match Ex. <a href="" class=""> turns, we need to
// matchs `<a` and not `<a>`
var stripped_element = stripped_element.substring(0,stripped_element.length-1)
positions.push(html.search(stripped_element));
elements.push(stripped_element);
}
if(positions && elements) {
var element = 0;
for(var i in positions)
{
if(positions[i] > cursor_pos)
{
// Find the element right before our cursor
element = i - 1;
break;
}
}
if(elements[element] == undefined)
return;
// return just the element, a, center, div, span...
return elements[element].replace(/[\<\/\>]/,'');
}
return;
}
- function priorElement(html,cursor_pos){
- var re = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi
- var matches = html.match(re,html)
- var elements = [];
- var positions = [];
- // Run through all the matches
- for ( x in matches )
- {
- // Strip all the randon stuff so we just have the element.
- var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
- // If it has a trailing backslash ignore it
- if(stripped_element.match(/\w\//)) // may want to look for these instead like in your script area|base|basefont|br|hr|input|img|link|meta
- continue;
- // Get rid of the `>` so we can match Ex. <a href="" class=""> turns, we need to
- // matchs `<a` and not `<a>`
- var stripped_element = stripped_element.substring(0,stripped_element.length-1)
- positions.push(html.search(stripped_element));
- elements.push(stripped_element);
- }
- if(positions && elements) {
- var element = 0;
- for(var i in positions)
- {
- if(positions[i] > cursor_pos)
- {
- // Find the element right before our cursor
- element = i - 1;
- break;
- }
- }
- if(elements[element] == undefined)
- return;
- // return just the element, a, center, div, span...
- return elements[element].replace(/[\<\/\>]/,'');
- }
- return;
- }
#define NULL (::rand() % 2)
- mindfullsilence
- Professor


- Registrado: Ago 04, 2008
- Mensajes: 846
- Status: Offline
Quería que los elementos a ser cerrado en sucesión. Por lo que si he integrado elementos, la función cerrará en orden. Ejemplo:
Si tuviera que escribir "< /" el resultado sería:
Ive adaptado su código, de simulación y casi tienen que trabajar. El único problema es que con elementos incrustados tal como se muestra más arriba, el resultado es como sigue:
HTML Código: [ Select ]
<div>
<p>
<a>
<p>
<a>
- <div>
- <p>
- <a>
Si tuviera que escribir "< /" el resultado sería:
- Por primera vez: un
- Segundo tiempo: p
- Tercer tiempo: div
Ive adaptado su código, de simulación y casi tienen que trabajar. El único problema es que con elementos incrustados tal como se muestra más arriba, el resultado es como sigue:
- Por primera vez: un
- Segundo tiempo: una
- Tercer tiempo: p
- Cuarto tiempo: div
Use your words like arrows to shoot toward your goal.
- mindfullsilence
- Professor


- Registrado: Ago 04, 2008
- Mensajes: 846
- Status: Offline
Ive hizo algunos cambios en mi código que sigue una lógica mejor pero todavía estoy atascado con prácticamente el mismo problema.
Heres el código que tengo hasta ahora:
El problema está en la tercera etiqueta html incrustado. En lugar de este resultado:
Im obtener esto:
Heres el código que tengo hasta ahora:
JAVASCRIPT Código: [ Select ]
function unclosed(html,cursor_pos){
//Remove any text after the cursor
html = html.substr(0,cursor_pos);
var re = /<[^<]+?>/gi
var matches = html.match(re,html)
var openers = [];
var closers = [];
var unclosed = [];
// Run through all the matches
for ( x in matches )
{
// Strip all the random stuff so we just have the element.
var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
// If it has a trailing backslash ignore it, since it's slef-closing
if(stripped_element.match(/\w\//))
continue;
// Get rid of the `>` so we can match, we need to match `<a` and not `<a>`
var stripped_element = stripped_element.substring(0,stripped_element.length-1);
stripped_element = stripped_element.replace("<","");
// now we check to see if it is a closing element or not
if(stripped_element.match(/\//))
{
//if it is, stick it in the "closers" array
closers.push(stripped_element);
}
else
{
//otherwise put it in the "openers" array
openers.push(stripped_element);
}
}
// if there are just as many closing tags as there are opening tags
// than all tags are closed, so return false.
if(openers.length == closers.length || openers.length == 0)
{
return false;
}
// if the closers array is empty, push the openers to the
// "unclosed" array and return the last entry
else if(closers.length == 0 && openers.length > 0) {
for ( x in openers )
{
unclosed.push(openers[x]);
}
return unclosed.pop();
}
// now we check for matches between opening and closing tags.
else
{
// go through each entry in the "openers" array
for (x in openers)
{
hasClose = new RegExp("\/"+openers[x],"gi");
// see if theres a match for it in the "closers" array
for (i in closers)
{
// if there is, remove the match from both arrays
// and constinue to the next entry.
if(closers[i].match(hasClose))
{
closers.splice(i,1);
openers.splice(x,1);
break;
}
// if there's not, add the "openers" entry to the
// "unclosed" array
else
{
unclosed.push(openers[x]);
}
}
}
// now return the last entry in the "unclosed" array.
return unclosed.pop();
}
}
//Remove any text after the cursor
html = html.substr(0,cursor_pos);
var re = /<[^<]+?>/gi
var matches = html.match(re,html)
var openers = [];
var closers = [];
var unclosed = [];
// Run through all the matches
for ( x in matches )
{
// Strip all the random stuff so we just have the element.
var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
// If it has a trailing backslash ignore it, since it's slef-closing
if(stripped_element.match(/\w\//))
continue;
// Get rid of the `>` so we can match, we need to match `<a` and not `<a>`
var stripped_element = stripped_element.substring(0,stripped_element.length-1);
stripped_element = stripped_element.replace("<","");
// now we check to see if it is a closing element or not
if(stripped_element.match(/\//))
{
//if it is, stick it in the "closers" array
closers.push(stripped_element);
}
else
{
//otherwise put it in the "openers" array
openers.push(stripped_element);
}
}
// if there are just as many closing tags as there are opening tags
// than all tags are closed, so return false.
if(openers.length == closers.length || openers.length == 0)
{
return false;
}
// if the closers array is empty, push the openers to the
// "unclosed" array and return the last entry
else if(closers.length == 0 && openers.length > 0) {
for ( x in openers )
{
unclosed.push(openers[x]);
}
return unclosed.pop();
}
// now we check for matches between opening and closing tags.
else
{
// go through each entry in the "openers" array
for (x in openers)
{
hasClose = new RegExp("\/"+openers[x],"gi");
// see if theres a match for it in the "closers" array
for (i in closers)
{
// if there is, remove the match from both arrays
// and constinue to the next entry.
if(closers[i].match(hasClose))
{
closers.splice(i,1);
openers.splice(x,1);
break;
}
// if there's not, add the "openers" entry to the
// "unclosed" array
else
{
unclosed.push(openers[x]);
}
}
}
// now return the last entry in the "unclosed" array.
return unclosed.pop();
}
}
- function unclosed(html,cursor_pos){
- //Remove any text after the cursor
- html = html.substr(0,cursor_pos);
- var re = /<[^<]+?>/gi
- var matches = html.match(re,html)
- var openers = [];
- var closers = [];
- var unclosed = [];
- // Run through all the matches
- for ( x in matches )
- {
- // Strip all the random stuff so we just have the element.
- var stripped_element = matches[x].replace(/\s[^\/>]+/g,'');
- // If it has a trailing backslash ignore it, since it's slef-closing
- if(stripped_element.match(/\w\//))
- continue;
- // Get rid of the `>` so we can match, we need to match `<a` and not `<a>`
- var stripped_element = stripped_element.substring(0,stripped_element.length-1);
- stripped_element = stripped_element.replace("<","");
- // now we check to see if it is a closing element or not
- if(stripped_element.match(/\//))
- {
- //if it is, stick it in the "closers" array
- closers.push(stripped_element);
- }
- else
- {
- //otherwise put it in the "openers" array
- openers.push(stripped_element);
- }
- }
- // if there are just as many closing tags as there are opening tags
- // than all tags are closed, so return false.
- if(openers.length == closers.length || openers.length == 0)
- {
- return false;
- }
- // if the closers array is empty, push the openers to the
- // "unclosed" array and return the last entry
- else if(closers.length == 0 && openers.length > 0) {
- for ( x in openers )
- {
- unclosed.push(openers[x]);
- }
- return unclosed.pop();
- }
- // now we check for matches between opening and closing tags.
- else
- {
- // go through each entry in the "openers" array
- for (x in openers)
- {
- hasClose = new RegExp("\/"+openers[x],"gi");
- // see if theres a match for it in the "closers" array
- for (i in closers)
- {
- // if there is, remove the match from both arrays
- // and constinue to the next entry.
- if(closers[i].match(hasClose))
- {
- closers.splice(i,1);
- openers.splice(x,1);
- break;
- }
- // if there's not, add the "openers" entry to the
- // "unclosed" array
- else
- {
- unclosed.push(openers[x]);
- }
- }
- }
- // now return the last entry in the "unclosed" array.
- return unclosed.pop();
- }
- }
El problema está en la tercera etiqueta html incrustado. En lugar de este resultado:
HTML Código: [ Select ]
<div><p><a>
</a></p></div>
</a></p></div>
- <div><p><a>
- </a></p></div>
Im obtener esto:
HTML Código: [ Select ]
<div><p><a>
</a></p></p>
</a></p></p>
- <div><p><a>
- </a></p></p>
Use your words like arrows to shoot toward your goal.
Página 1 de 1
Para responder a este tema que necesita para ingresar o registrarse. Es gratis.
Publicar Información
- Total de mensajes en este tema: 4 mensajes
- Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 136 invitados
- No puede abrir nuevos temas en este Foro
- No puede responder a temas en este Foro
- No puede editar sus mensajes en este Foro
- No puede borrar sus mensajes en este Foro
- No puede enviar adjuntos en este Foro
