Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal type error with duplicate attributes and generateSpans: true #56

Open
refi64 opened this issue Dec 4, 2017 · 7 comments
Open

Comments

@refi64
Copy link

refi64 commented Dec 4, 2017

Repro:

import 'package:html/parser.dart' as html;

void main(List<String> args) {
  var dom = html.parse('<div duplicate duplicate>', generateSpans: true);
  print(dom.querySelector('div').attributeSpans);
}

This results in:

Unhandled exception:
type 'ParseErrorToken' is not a subtype of type 'StartTagToken' in type cast where
  ParseErrorToken is from package:html/src/token.dart
  StartTagToken is from package:html/src/token.dart

#0      Object._as (dart:core-patch/object_patch.dart:73)
#1      Node._ensureAttributeSpans (package:html/dom.dart:286:35)
#2      Node.attributeSpans (package:html/dom.dart:173:5)
#3      main (file:///khome/ryan/langtest/dart/html_bug/bin/html_bug.dart:5:34)
#4      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:263)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)
@kevmoo
Copy link
Member

kevmoo commented Jan 6, 2018

@kirbyfan64 – what would you expect? Just ignore the duplicate attributes?

The code change isn't hard – just not sure it's the right thing to do.

@jmesserly ?

diff --git a/lib/dom.dart b/lib/dom.dart
index fb0d458..53c8851 100644
--- a/lib/dom.dart
+++ b/lib/dom.dart
@@ -284,6 +284,10 @@ abstract class Node {
         generateSpans: true, attributeSpans: true);

     tokenizer.moveNext();
+
+    while (tokenizer.current is ParseErrorToken) {
+      tokenizer.moveNext();
+    }
     var token = tokenizer.current as StartTagToken;

     if (token.attributeSpans == null) return; // no attributes

@refi64
Copy link
Author

refi64 commented Jan 6, 2018

I mean, I don't mind if it throws an error...just an intentional one, not an "internal" type error like this.

@jmesserly
Copy link
Contributor

Good catch on that bug! And that fix LGTM.

@kevmoo
Copy link
Member

kevmoo commented Jan 8, 2018 via email

@jmesserly
Copy link
Contributor

I'll fix to throw – at least it will throw prettier

yeah either way. The *Span APIs are provided by this package (rather than being part of the real DOM spec), so we can decide the behavior. Typically HTML does try pretty hard to recover a DOM tree of some sort, even in the face of parse errors. So I think your first patch above is my slight preference.

@kevmoo
Copy link
Member

kevmoo commented Jan 8, 2018 via email

@jmesserly
Copy link
Contributor

Hrm...I I think I'll improve the existing error and then we can discuss.

sounds good to me!

kevmoo added a commit that referenced this issue Jan 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants