パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v2.3.0

universal_html

Flutter やサーバーサイドを含むすべてのプラットフォームで動作する 'dart:html'。クロスプラットフォーム開発および HTML/XML 処理を容易にします。

AndroidiOSmacOSWindowsLinux
30日間ダウンロード
66.2万
いいね数
516
Pub ポイント
150

パッケージをインストール

flutter pub add universal_html

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

universal_html は、モバイル、デスクトップ、サーバーサイド環境における Flutter を含むすべてのプラットフォームで動作する統一された 'dart:html' API を提供します。実行時環境にかかわらず、一貫した HTML および XML 処理を可能にし、クロスプラットフォーム開発を簡素化します。このパッケージは、DOM 操作、HTTP リクエスト、構造化コンテンツの解析に対して互換性のあるインターフェースを提供することで、Web と非 Web コンテキストのギャップを埋めます。Web とバックエンドサービス間でロジックを共有する場合に最適であり、コードの重複を減らし、開発ワークフローをスムーズにします。

用途

  • クロスプラットフォームのWebおよびサーバー論理
  • 共有HTML/XML解析
  • サーバーサイドレンダリングユーティリティ
  • Flutter アプリにおける Web コンポーネントの再利用
  • ユニバーサルなDartアプリケーションの構築

主な機能

  • プラットフォームに依存しない dart:html API
  • すべてのプラットフォームで DOM の操作をサポートしています
  • 組み込みの XML/HTML パース
  • Flutter およびサーバーとスムーズに連携します
  • 軽量で最小限の依存関係

適している対象

  • 複数のプラットフォームをターゲットにする開発者
  • HTML処理が必要なバックエンドサービス
  • Web風の機能を必要とするFlutterアプリ
  • Web とサーバー間のコード共有
  • プラットフォーム固有の条件分岐を避けるプロジェクト

注意事項

  • Web以外のプラットフォームではブラウザ固有の機能が制限されています
  • ウェブ以外の特定のDOMイベントはサポートされていません
  • サーバーサイド実行時のパフォーマンスは異なる場合があります
  • 完全なブラウザエミュレーションを目的としていない
  • 非同期操作の慎重な処理が必要です

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Pub Package package publisher Github Actions CI

Introduction

A cross-platform replacement for dart:html (or package:web):

  • Eases cross-platform development
    • You can use this package in browsers, mobile, desktop, and server-side VM, and Node.JS.
    • Just replace dart:html / package:web imports with package:universal_html/universal_html.dart. Normal dart:html will continue to be used when application run in browsers.
  • Extensive support for processing HTML and XML documents
  • EventSource streaming support
    • Cross-platform dart:html EventSource ("application/event-stream").
    • If you want to customize EventSource HTTP headers outside browsers, see EventSourceOutsideBrowser.

The project is licensed under the Apache License 2.0. Some of the source code was adopted from the original dart:html in Dart SDK, which is documented in the relevant files.

Documentation

Similar projects

Getting started

1. Add dependency

In pubspec.yaml:

dependencies:
  universal_html: ^2.3.0

2. Use

import "package:universal_html/universal_html.dart";

void main() {
  // Create a DOM tree
  final div = DivElement();
  div.append(Element.tag("h1")
    ..classes.add("greeting")
    ..appendText("Hello world!"));

  // Print outer HTML
  print(div.outerHtml);
  // --> <div><h1>Hello world</h1></div>

  // Do a CSS query
  print(div.querySelector("div > .greeting").text);
  // --> Hello world
}

Examples

Parsing HTML

Use parseHtmlDocument:

import 'package:universal_html/parsing.dart';

void main() {
  final htmlDocument = parseHtmlDocument('<html>...</html>');
}

Parsing XML

Use parseXmlDocument:

import 'package:universal_html/parsing.dart';

void main() {
  final xmlDocument = parseXmlDocument('<xml>...</xml>');
}

Scraping a website

Load a Window with WindowController:

import 'dart:io' show Cookie;
import 'package:universal_html/controller.dart';

Future main() async {
  // Load a document.
  final controller = WindowController();
  controller.defaultHttpClient.userAgent = 'My Hacker News client';
  await controller.openHttp(
    method: 'GET',
    uri: Uri.parse("https://news.ycombinator.com/"),
    onRequest: (HttpClientRequest request) {
      // Add custom headers
      request.headers.set('Authorization', 'headerValue');
      request.cookies.add(Cookie('cookieName', 'cookieValue'));
    },
    onResponse: (HttpClientResponse response) {
      print('Status code: ${response.statusCode}');
    },
  );

  // Select the top story using a CSS query
  final titleElements = controller.document.querySelectorAll(".athing > .title");
  final topStoryTitle = titleElements.first.text;

  // Print result
  print("Top Hacker News story is: $topStoryTitle");
}

EventSource

EventSource (see mozilla.org) is a browser API for reading "application/event-stream" streams. It has been supported by browsers for a long time.

import 'package:universal_html/universal_html.dart';

Future<void> main() async {
  final eventSource = EventSource('http://example.com/events');
  await for (var message in event.onMessage) {
    print('Event type: ${message.type}');
    print('Event data: ${message.data}');
  }
}

EventSource requests from real browsers are typically authenticated using cookies. If you want to add cookies or customize other HTTP headers, you need to use EventSourceOutsideBrowser:

import 'package:universal_html/universal_html.dart';
import 'dart:io' show Cookie;

Future<void> main() async {
  final eventSource = EventSource('http://example.com/events');
  
  // The following block will NOT be executed in browsers.
  // Because compiler can infer instances of EventSourceOutsideBrowser are never constructed,
  // it will not appear in Javascript either.
  if (eventSource is EventSourceOutsideBrowser) {
    eventSource.onHttpClientRequest = (eventSource, request) {
      request.headers.set('Authorization', 'example');
      request.cookies.add(Cookie('name', 'value'));
    };
    eventSource.onHttpClientResponse = (eventSource, request, response) {
      // ...
    };
  }
  
  await for (var message in eventSource.onMessage) {
    print('Event:');
    print('  type: ${message.type}');
    print('  data: ${message.data}');
  }
}

Testing

import 'package:universal_html/controller.dart';
import 'package:test/test.dart';

void main() {
  setUp(() {
    WindowController.instance = WindowController();
  });
  
  test('test #1', () {
    // ...
  });
  
  test('test #2', () {
    // ...
  });
}

カテゴリー

依存関係

依存関係一覧 9 件

ソースリポジトリ

このパッケージを使用しているプロジェクト