DOMParser



  • [url]http://domparsing.spec.whatwg.org/#the-domparser-interface[/url]

    The parseFromString(str, type) method must run these steps, depending on type:

    [...]

    "text/xml"
    "application/xml"
    "application/xhtml+xml"
    "image/svg+xml"
    1. Parse str with a namespace-enabled XML parser.
    2. If the previous step didn't return an error, return the newly created document and terminate these steps.
    3. Let document be a newly-created XMLDocument.
    4. Let root be a new Element, with its local name set to "parsererror" and its namespace set to "http://www.mozilla.org/newlayout/xml/parsererror.xml".
    5. At this point user agents may append nodes to root, for example to describe the nature of the error.
    6. Append root to document.
    7. Return document.

    Yes, JavaScript has exceptions, why do you ask?

    [url=https://bugzilla.mozilla.org/show_bug.cgi?id=45566]Mozilla created this mess, ignored it for ten years, and then refused to fix it because it would be "web-incompatible".[/url]

    Bonus points: [url=https://bugs.webkit.org/show_bug.cgi?id=13057]WebKit doesn't even implement this correctly[/url].

    And yes, this is the only way to parse XML that browsers (i.e. not IE) support.



  • @spamcourt said:

    And yes, this is the only way to parse XML that browsers (i.e. not IE) support.

    When did JavaScript stop being turing-complete?



  • @spamcourt said:


    And yes, this is the only way to parse XML that browsers (i.e. not IE) support.

     

     Does your browser not support jQuery?

     



  • @Ben L. said:

    @spamcourt said:
    And yes, this is the only way to parse XML that browsers (i.e. not IE) support.

    When did JavaScript stop being turing-complete?

    If we're going to be pedantic dickweeds, JavaScript was never Turing-complete to begin with. You need infinite memory for that. Real-world computers are merely finite-state machines.

    But otherwise, have fun reimplementing DOM from scratch, trying to get every parsing detail right, and enjoy your shitty performance.

    @fire2k said:

    Does your browser not support jQuery?

    [url="https://github.com/jquery/jquery/blob/cb37994d76afb45efc3b606546349ed4e695c053/src/ajax/parseXML.js"]jQuery just uses the same object.[/url]



  • @Ben L. said:

    When did JavaScript stop being turing-complete?

    I pretend turing-complete languages don't exist in order to keep my sanity.


  • FoxDev

    @spamcourt said:

    If we're going to be pedantic dickweeds, JavaScript was never Turing-complete to begin with. You need infinite memory for that.

    Almost but not quite. 'Turing-complete' means it can theoretically be used to implement a Universal Turing Machine. Obviously you can't do it because memory is finite, but that's just a practicality.

     

    Weirdly, XSLT is Turing-complete :S

     



  • @RaceProUK said:

    @spamcourt said:

    If we're going to be pedantic dickweeds, JavaScript was never Turing-complete to begin with. You need infinite memory for that.

    Almost but not quite. 'Turing-complete' means it can theoretically be used to implement a Universal Turing Machine. Obviously you can't do it because memory is finite, but that's just a practicality.

     

    Weirdly, XSLT is Turing-complete :S

     

     

    So are C++ Templates and X86-Page faults

     





  • Ok, that's weird. I tried parsing a string with mismatched tags in the Firefox Javascript console, and it indicates that a Javascript error occurred, but I can't seem to catch the error by wrapping it in a try...catch block. It still says that the error occurred, but it doesn't execute the code inside the catch block.

    09:43:15.037try {
      (new DOMParser).parseFromString("<p></i>", "text/xml");
    } catch (e) {
      console.log(e.name + ": " + e.message);
    }
    09:43:15.040[object XMLDocument]
    09:43:15.041[img]data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKBAMAAAB/HNKOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAC1QTFRF+5UA+5cG+5gI+5gJ+5kK/LZR/LhV/LlY/Ltc/Lxf/L5j/uvR/u7X/vDc////W91H/wAAAD9JREFUCNdjeAcCDO/WvLv5juEF81mHPIY3CiaM+xjeHWKQBopfYbB5x/DWQYj5HMNzxj0KcQzvst8dA4qDAADICh/TK48BuwAAAABJRU5ErkJggg==[/img]mismatched tag. Expected: </p>.


  • @anotherusername said:

    Ok, that's weird. I tried parsing a string with mismatched tags in the Firefox Javascript console, and it indicates that a Javascript error occurred, but I can't seem to catch the error by wrapping it in a try...catch block. It still says that the error occurred, but it doesn't execute the code inside the catch block.

    That's because it's not a JS error; that's what this thread is about. I would guess FF is just being friendly in printing the invalid result in an eye-catching manner.





  • @Arnavion said:

    @anotherusername said:
    Ok, that's weird. I tried parsing a string with mismatched tags in the Firefox Javascript console, and it indicates that a Javascript error occurred, but I can't seem to catch the error by wrapping it in a try...catch block. It still says that the error occurred, but it doesn't execute the code inside the catch block.

    That's because it's not a JS error; that's what this thread is about. I would guess FF is just being friendly in printing the invalid result in an eye-catching manner.

    Yeah, but the weird part was that the console indicated that a Javascript error occurred, when it didn't.



  • @spamcourt said:

    If we're going to be pedantic dickweeds, JavaScript was never Turing-complete to begin with. You need infinite memory for that. Real-world computers are merely finite-state machines.

    If we're going to be pedantic dickweeds, JavaScript doesn't specify that memory must be finite, or that it must be run on a real-world computer.


Log in to reply