{"id":13803295,"url":"https://github.com/terrier989/universal_html","last_synced_at":"2025-05-13T15:33:02.632Z","repository":{"id":193291788,"uuid":"688499283","full_name":"terrier989/universal_html","owner":"terrier989","description":"Cross-platform 'dart:html'.","archived":false,"fork":true,"pushed_at":"2023-09-07T14:42:23.000Z","size":1748,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-11-18T19:42:26.307Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/universal_html","language":"Dart","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dint-dev/universal_html","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/terrier989.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["terrier989"],"open_collective":["cryptography"]}},"created_at":"2023-09-07T13:24:20.000Z","updated_at":"2023-09-07T13:25:43.000Z","dependencies_parsed_at":"2023-09-07T15:13:45.614Z","dependency_job_id":null,"html_url":"https://github.com/terrier989/universal_html","commit_stats":null,"previous_names":["terrier989/universal_html"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terrier989%2Funiversal_html","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terrier989%2Funiversal_html/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terrier989%2Funiversal_html/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terrier989%2Funiversal_html/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/terrier989","download_url":"https://codeload.github.com/terrier989/universal_html/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253970551,"owners_count":21992523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T01:00:28.022Z","updated_at":"2025-05-13T15:33:02.199Z","avatar_url":"https://github.com/terrier989.png","language":"Dart","readme":"[![Pub Package](https://img.shields.io/pub/v/universal_html.svg)](https://pub.dartlang.org/packages/universal_html)\n[![package publisher](https://img.shields.io/pub/publisher/universal_html.svg)](https://pub.dev/packages/universal_html/publisher)\n[![Github Actions CI](https://github.com/dint-dev/universal_html/workflows/Dart%20CI/badge.svg)](https://github.com/dint-dev/universal_html/actions)\n\n# Introduction\nA cross-platform `dart:html`:\n  * __Eases cross-platform development__\n    * You can use this package in browsers, mobile, desktop, and server-side VM, and Node.JS.\n    * Just replace `dart:html` imports with `package:universal_html/html.dart`. Normal\n      _dart:html_ will continue to be used when application run in browsers.\n  * __Extensive support for processing HTML and XML documents__\n    * Parse, manipulate, and print [DOM nodes](https://api.dart.dev/stable/2.19.3/dart-html/Node-class.html).\n    * Find DOM nodes with [querySelectorAll](https://api.dart.dev/stable/2.19.3/dart-html/querySelectorAll.html)\n      and other CSS query methods.\n  * __EventSource streaming support__\n    * Cross-platform _dart:html_ `EventSource` (\"application/event-stream\").\n    * If you want to customize EventSource HTTP headers outside browsers, see\n      [EventSourceOutsideBrowser](https://pub.dev/documentation/universal_html/latest/universal_html/EventSourceOutsideBrowser-class.html).\n\nThe project is licensed under the [Apache License 2.0](LICENSE). Some of the source code was adopted\nfrom the original [dart:html](https://github.com/dart-lang/sdk/tree/master/tools/dom) in Dart SDK,\nwhich is documented in the relevant files.\n\n## Documentation\n  * [API reference](https://pub.dev/documentation/universal_html/latest/)\n  * [Github project](https://github.com/dint-dev/universal_html)\n    * We appreciate feedback, issue reports, and pull requests.\n\n## Similar projects\n  * [universal_io](https://pub.dev/packages/universal_io) (cross-platform _dart:io_)\n  * [jsdom](https://www.npmjs.com/package/jsdom) (DOM implementation in Javascript)\n\n# Getting started\n## 1. Add dependency\nIn `pubspec.yaml`:\n```yaml\ndependencies:\n  universal_html: ^2.2.4\n```\n\n## 2. Use\n```dart\nimport \"package:universal_html/html.dart\";\n\nvoid main() {\n  // Create a DOM tree\n  final div = DivElement();\n  div.append(Element.tag(\"h1\")\n    ..classes.add(\"greeting\")\n    ..appendText(\"Hello world!\"));\n\n  // Print outer HTML\n  print(div.outerHtml);\n  // --\u003e \u003cdiv\u003e\u003ch1\u003eHello world\u003c/h1\u003e\u003c/div\u003e\n\n  // Do a CSS query\n  print(div.querySelector(\"div \u003e .greeting\").text);\n  // --\u003e Hello world\n}\n```\n\n# Examples\n## Parsing HTML\nUse [parseHtmlDocument](https://pub.dev/documentation/universal_html/latest/universal_html.parsing/parseHtmlDocument.html):\n\n```dart\nimport 'package:universal_html/parsing.dart';\n\nvoid main() {\n  final htmlDocument = parseHtmlDocument('\u003chtml\u003e...\u003c/html\u003e');\n}\n```\n\n## Parsing XML\nUse [parseXmlDocument](https://pub.dev/documentation/universal_html/latest/universal_html.parsing/parseXmlDocument.html):\n\n```dart\nimport 'package:universal_html/parsing.dart';\n\nvoid main() {\n  final xmlDocument = parseXmlDocument('\u003cxml\u003e...\u003c/xml\u003e');\n}\n```\n\n## Scraping a website\nLoad a _Window_ with [WindowController](https://pub.dev/documentation/universal_html/latest/universal_html.controller/WindowController-class.html):\n\n```dart\nimport 'dart:io' show Cookie;\nimport 'package:universal_html/controller.dart';\n\nFuture main() async {\n  // Load a document.\n  final controller = WindowController();\n  controller.defaultHttpClient.userAgent = 'My Hacker News client';\n  await controller.openHttp(\n    method: 'GET',\n    uri: Uri.parse(\"https://news.ycombinator.com/\"),\n    onRequest: (HttpClientRequest request) {\n      // Add custom headers\n      request.headers.set('Authorization', 'headerValue');\n      request.cookies.add(Cookie('cookieName', 'cookieValue'));\n    },\n    onResponse: (HttpClientResponse response) {\n      print('Status code: ${response.statusCode}');\n    },\n  );\n\n  // Select the top story using a CSS query\n  final titleElements = controller.document.querySelectorAll(\".athing \u003e .title\");\n  final topStoryTitle = titleElements.first.text;\n\n  // Print result\n  print(\"Top Hacker News story is: $topStoryTitle\");\n}\n```\n\n## EventSource\n`EventSource` ([see mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/EventSource))\nis a browser API for reading \"application/event-stream\" streams. It has been supported by browsers\nfor a long time.\n\n```dart\nimport 'package:universal_html/html.dart';\n\nFuture\u003cvoid\u003e main() async {\n  final eventSource = EventSource('http://example.com/events');\n  await for (var message in event.onMessage) {\n    print('Event type: ${message.type}');\n    print('Event data: ${message.data}');\n  }\n}\n```\n\nEventSource requests from real browsers are typically authenticated using cookies.\nIf you want to add cookies or customize other HTTP headers, you need to use\n[EventSourceOutsideBrowser](https://pub.dev/documentation/universal_html/latest/universal_html/EventSourceOutsideBrowser-class.html):\n```dart\nimport 'package:universal_html/universal_html.dart';\nimport 'dart:io' show Cookie;\n\nFuture\u003cvoid\u003e main() async {\n  final eventSource = EventSource('http://example.com/events');\n  \n  // The following block will NOT be executed in browsers.\n  // Because compiler can infer instances of EventSourceOutsideBrowser are never constructed,\n  // it will not appear in Javascript either.\n  if (eventSource is EventSourceOutsideBrowser) {\n    eventSource.onHttpClientRequest = (eventSource, request) {\n      request.headers.set('Authorization', 'example');\n      request.cookies.add(Cookie('name', 'value'));\n    };\n    eventSource.onHttpClientResponse = (eventSource, request, response) {\n      // ...\n    };\n  }\n  \n  await for (var message in eventSource.onMessage) {\n    print('Event:');\n    print('  type: ${message.type}');\n    print('  data: ${message.data}');\n  }\n}\n```\n\n## Testing\n```dart\nimport 'package:universal_html/controller.dart';\nimport 'package:test/test.dart';\n\nvoid main() {\n  setUp(() {\n    WindowController.instance = WindowController();\n  });\n  \n  test('test #1', () {\n    // ...\n  });\n  \n  test('test #2', () {\n    // ...\n  });\n}\n```","funding_links":["https://github.com/sponsors/terrier989","https://opencollective.com/[\"cryptography\"]"],"categories":["Cross-platform development"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterrier989%2Funiversal_html","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fterrier989%2Funiversal_html","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterrier989%2Funiversal_html/lists"}