Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6102879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:53+00:00 2026-05-23T13:39:53+00:00

I have some HTML I need to parse. Basically I’m walking through the dom

  • 0

I have some HTML I need to parse.

Basically I’m walking through the dom of a given element. Grabbing text nodes, and element nodes.

As I come across text nodes, I print them into a different element character by character. Each character is placed into it’s own span, with its own style, which was taken from any element node found with a style attached.

So when an element node is found, it’s style is applied to any text node detected until another element node is found and the old style is replaced with the new one.

The code below works. If you have a sentence or a short paragraph in the source element it reproduces the text accurately in less than a second. The longer the text gets the longer it takes (duh).

Interestingly, the more text that is already in the destination element, the longer it takes. So if I’ve ran this function 10 times on the same source element, with the same body of text being processed, it will run slower the 10th time through than the 1st time through, presumably because it’s harder to render the text in an element that already has content.

Anyway, I really need to find a way to make this thing run faster.

Lastly, here is an example HTML snippet this thing might need to process:

<span style='blah: blah;'> Some text </span><span>Even more text </span> <p> stuff </p>

The resulting HTML would be:

<span style='blah: blah;'>S</span>
<span style='blah: blah;'>o</span>
<span style='blah: blah;'>m</span>
<span style='blah: blah;'>e</span>
<span style='blah: blah;'> </span> 
<span style='blah: blah;'>t</span>
<span style='blah: blah;'>e</span>
<span style='blah: blah;'>x</span>
<span style='blah: blah;'>t</span> 
.......

Nothing fancy.

Here’s the code:

Code:

ed.rta_to_arr_paste = function(ele, cur_style) {

    var child_arr = ele.childNodes;

    if(!(is_set(cur_style))) {
        cur_style = {};
    }

    for(var i = 0; i < child_arr.length; i++) {
        if(child_arr[i].nodeType == 1) {
            if(cur_style != child_arr[i].style) {
                cur_style = child_arr[i].style;
            }
        } else if(child_arr[i].nodeType == 3) {

            for(var n = 0; n < child_arr[i].nodeValue.length; n++) {

                var span = ed.add_single_char(child_arr[i].nodeValue.charAt(n), cur_style);
            }
        }
        ed.rta_to_arr_paste(child_arr[i], cur_style);
    }

}

EDIT:
One example of a system like this being used is in google docs.

When a user pastes text into the document, it’s first rendered off screen, then processed with a function similar (I’m assuming) to this one. It then reprints the text in the document.
It all happens extremely fast (unless the text is very long).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:39:54+00:00Added an answer on May 23, 2026 at 1:39 pm

    It seems you are directly inserting the new elements into the DOM tree, so I think you can get the best improvement by not doing that.

    Avoid inserting a lot of elements one by one. Every time you insert an element, the browser has to recalculate the layout of the page and this takes time.

    Instead, add the nodes to an element not in the DOM, the best would be using a DocumentFragment, which can be created via document.createDocumentFragment.

    Then all you have to do is to insert this fragment and the browser only has to do one recalculation.

    Update:

    What you could also try is to use regular expressions to convert the text into the span elements.

    var html = value.replace(/(.)/g, "<span>$1</span>")
    

    At least in my naive test (not sure if the testcases are good this way), it performs much better than creating span elements and adding them to the document fragment:

    Update 2: I adjusted the tests to also set the generated elements/string as content of an element and sadly, this takes away all the speed of using replace. But it might still be worth testing it:

    http://jsperf.com/regex-vs-loop


    You should also avoid repeated property access:

    ed.rta_to_arr_paste = function(ele, cur_style) {
    
        var child_arr = ele.childNodes;
    
        if(!(is_set(cur_style))) {
            cur_style = {};
        }
    
        for(var i = 0, l = child_arr; i <l; i++) {
            var child = child_arr[i];
            if(child.nodeType == 1) {
                // this will always be true, because `el.style` returns an object
                // so comparing it does not make sense. Maybe just override it always
                if(cur_style != child.style) { 
                    cur_style = child.style;
                }
                // doesn't need to be called for other nodes
                ed.rta_to_arr_paste(child, cur_style); 
            } 
            else if(child.nodeType == 3) {
                var value = child.nodeValue;
                for(var n = 0, ln = value.length; n < ln; n++) {
                    ed.add_single_char(value.charAt(n), cur_style);
                }
            }       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML textbox that contains some SQL code that I need executed.
I have some HTML I am trying to parse. There are cases where the
I have some html that creates a dropdown list. The list has text values
I have such files to parse (from scrapping) with Python: some HTML and JS
i need some help to parse a html, extracting everything starting with http://, containing
I have some HTML that looks like this: <ul class=faq> <li class=open> <a class=question
I have some HTML menus, which I show completely when a user clicks on
I have some HTML that displays fine on FireFox3/Opera/Safari but not with IE7. The
I have some HTML and jQuery that slides a div up and down to
So I have some html like so: <div id=avatar_choices> <label for=id_choice_0> <input id=id_choice_0 type=radio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.