DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (666b11c33fee)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
<!DOCTYPE html>
<title>NDEFRecord constructor</title>
<link rel="help" href="https://w3c.github.io/web-nfc/#dom-ndefrecord">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/nfc-helpers.js"></script>
<script>

  test(() => {
    assert_equals(NDEFRecord.length, 1);
    assert_throws(new TypeError, () => new NDEFRecord());
  }, 'NDEFRecord constructor without init dict');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(null),
        'The record has neither type nor data.');
  }, 'NDEFRecord constructor with null init dict');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('empty', test_text_data, test_record_id, 'text/plain')),
        'mediaType does not apply for empty record type.');
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('text', test_text_data, test_record_id, 'text/plain')),
        'mediaType does not apply for text record type.');
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('url', test_url_data, test_record_id, 'text/plain')),
        'mediaType does not apply for url record type.');
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('absolute-url', test_url_data, test_record_id, 'text/plain')),
        'mediaType does not apply for absolute-url record type.');
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('unknown', test_buffer_data, test_record_id, 'application/octet-stream')),
        'mediaType does not apply for unknown record type.');
    assert_throws(new TypeError, () => new NDEFRecord(
          createRecord('foo.example.com:bar', test_buffer_data, test_record_id, 'application/octet-stream')),
        'mediaType does not apply for external record type.');
  }, 'NDEFRecord constructor should only accept mediaType for mime record type');

  test(() => {
    {
      const record = new NDEFRecord(createRecord('text', test_text_data));
      assert_equals(record.id, null, 'id');
    }
    {
      const record = new NDEFRecord(createRecord('text', test_text_data, ''));
      assert_equals(record.id, '', 'id');
    }
    {
      const dummy_id = 'https://dummy_host/mypath/myid';
      const record = new NDEFRecord(createRecord('text', test_text_data, dummy_id));
      assert_equals(record.id, dummy_id, 'id');
    }
    {
      const dummy_id = 'http://dummy_host/mypath/myid';
      const record = new NDEFRecord(createRecord('text', test_text_data, dummy_id));
      assert_equals(record.id, dummy_id, 'id');
    }
    {
      const dummy_id = 'mypath/myid';
      const record = new NDEFRecord(createRecord('text', test_text_data, dummy_id));
      assert_equals(record.id, dummy_id, 'id');
    }
  }, 'NDEFRecord constructor with custom record ids');

  test(() => {
    const record = new NDEFRecord(createTextRecord(test_text_data));
    assert_equals(record.recordType, 'text', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    assert_equals(record.encoding, 'utf-8', 'encoding');
    assert_equals(record.lang, 'en', 'lang');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_text_data,
        'data has the same content with the original dictionary');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with text record type and string data');

  test(() => {
    const encoder = new TextEncoder();
    const uint8Array = encoder.encode(test_text_data);
    const record = new NDEFRecord(createTextRecord(uint8Array.buffer));
    assert_equals(record.recordType, 'text', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    assert_equals(record.encoding, 'utf-8', 'encoding');
    assert_equals(record.lang, 'en', 'lang');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_text_data,
        'data has the same content with the original dictionary');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with text record type and arrayBuffer data');

  test(() => {
    const encoder = new TextEncoder();
    const uint8Array = encoder.encode(test_text_data);
    const record = new NDEFRecord(createTextRecord(uint8Array));
    assert_equals(record.recordType, 'text', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    assert_equals(record.encoding, 'utf-8', 'encoding');
    assert_equals(record.lang, 'en', 'lang');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_text_data,
        'data has the same content with the original dictionary');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with text record type and arrayBufferView data');

  test(() => {
    const encodings = ['utf-8', 'utf-16', 'utf-16be', 'utf-16le'];
    for (const encoding of encodings) {
      const lang = 'fr';
      const record = new NDEFRecord(createTextRecord(test_text_data, encoding, lang));
      assert_equals(record.recordType, 'text', 'recordType');
      assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
      assert_equals(record.encoding, encoding, 'encoding');
      assert_equals(record.lang, lang, 'lang');
      const decoder = new TextDecoder();
      assert_equals(decoder.decode(record.data), test_text_data,
          'data has the same content with the original dictionary');
    }
  }, 'NDEFRecord constructor with text record type, encoding, and lang');

  test(t => {
    const previous_lang = document.querySelector('html').getAttribute('lang');
    const test_lang = 'fr';
    document.querySelector('html').setAttribute('lang', test_lang);
    t.add_cleanup(() => {
      document.querySelector('html').setAttribute('lang', previous_lang);
    });
    const record = new NDEFRecord(createTextRecord(test_text_data));
    assert_equals(record.recordType, 'text', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    assert_equals(record.encoding, 'utf-8', 'encoding');
    assert_equals(record.lang, test_lang, 'lang');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_text_data,
        'data has the same content with the original dictionary');
  }, 'NDEFRecord constructor with text record type and custom document language');

  test(() => {
    const record = new NDEFRecord(createUrlRecord(test_url_data));
    assert_equals(record.recordType, 'url', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_url_data,
        'data has the same content with the original dictionary');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with url record type');

  test(() => {
    const record = new NDEFRecord(createUrlRecord(test_url_data, true));
    assert_equals(record.recordType, 'absolute-url', 'recordType');
    assert_equals(record.mediaType, null, 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(record.data), test_url_data,
        'data has the same content with the original dictionary');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with absolute-url record type');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(
          createMimeRecord("A string is not a BufferSource")),
        'Only BufferSource is allowed to be the record data.');

    let buffer = new ArrayBuffer(4);
    new Uint8Array(buffer).set([1, 2, 3, 4]);
    // Feed ArrayBuffer.
    {
      const record = new NDEFRecord(createMimeRecord(buffer));
      assert_equals(record.recordType, 'mime', 'recordType');
      assert_equals(record.mediaType, 'application/octet-stream', 'mediaType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [1, 2, 3, 4],
          'data has the same content with the original buffer');
      assert_throws('NotSupportedError', () => record.toRecords(),
          'Only smart-poster records and external type records could have embedded records.');
    }
    // Feed ArrayBufferView.
    {
      let buffer_view = new Uint8Array(buffer, 1);
      const record = new NDEFRecord(createMimeRecord(buffer_view));
      assert_equals(record.recordType, 'mime', 'recordType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [2, 3, 4],
          'data has the same content with the original buffer view');
      assert_throws('NotSupportedError', () => record.toRecords(),
          'Only smart-poster records and external type records could have embedded records.');
    }
  }, 'NDEFRecord constructor with mime record type and stream data');

  test(() => {
    const record = new NDEFRecord(createMimeRecordFromJson(test_json_data));
    assert_equals(record.recordType, 'mime', 'recordType');
    assert_equals(record.mediaType, 'application/json', 'mediaType');
    assert_equals(record.id, test_record_id, 'id');
    assert_object_equals(JSON.parse(new TextDecoder().decode(record.data)),
        test_json_data,
        'data has the same content with the original json');
    assert_throws('NotSupportedError', () => record.toRecords(),
        'Only smart-poster records and external type records could have embedded records.');
  }, 'NDEFRecord constructor with mime record type and json data');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(
          createUnknownRecord("A string is not a BufferSource")),
        'Only BufferSource is allowed to be the record data.');

    let buffer = new ArrayBuffer(4);
    new Uint8Array(buffer).set([1, 2, 3, 4]);
    // Feed ArrayBuffer.
    {
      const record = new NDEFRecord(createUnknownRecord(buffer));
      assert_equals(record.recordType, 'unknown', 'recordType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [1, 2, 3, 4],
          'data has the same content with the original buffer');
      assert_throws('NotSupportedError', () => record.toRecords(),
          'Only smart-poster records and external type records could have embedded records.');
    }
    // Feed ArrayBufferView.
    {
      let buffer_view = new Uint8Array(buffer, 1);
      const record = new NDEFRecord(createUnknownRecord(buffer_view));
      assert_equals(record.recordType, 'unknown', 'recordType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [2, 3, 4],
          'data has the same content with the original buffer view');
      assert_throws('NotSupportedError', () => record.toRecords(),
          'Only smart-poster records and external type records could have embedded records.');
    }
  }, 'NDEFRecord constructor with unknown record type');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(
        createRecord('foo.eXamPle.coM:bAr*-', "A string is not a BufferSource or NDEFMessage")),
        'Only BufferSource and NDEFMessage are allowed to be the record data.');

    let buffer = new ArrayBuffer(4);
    new Uint8Array(buffer).set([1, 2, 3, 4]);
    // Feed ArrayBuffer.
    {
      const record = new NDEFRecord(createRecord('foo.eXamPle.coM:bAr*-', buffer, test_record_id));
      assert_equals(record.recordType, 'foo.example.com:bAr*-', 'recordType');
      assert_equals(record.mediaType, null, 'mediaType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [1, 2, 3, 4],
          'data has the same content with the original buffer');
      assert_equals(record.toRecords(), null,
          'toRecords() returns null if the payload is not an NDEF message.');
    }
    // Feed ArrayBufferView.
    {
      let buffer_view = new Uint8Array(buffer, 1);
      const record = new NDEFRecord(createRecord(
            'foo.eXamPle.coM:bAr*-', buffer_view, test_record_id));
      assert_equals(record.recordType, 'foo.example.com:bAr*-', 'recordType');
      assert_equals(record.mediaType, null, 'mediaType');
      assert_equals(record.id, test_record_id, 'id');
      assert_array_equals(new Uint8Array(record.data.buffer), [2, 3, 4],
          'data has the same content with the original buffer view');
      assert_equals(record.toRecords(), null,
          'toRecords() returns null if the payload is not an NDEF message.');
    }
  }, 'NDEFRecord constructor with external record type');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(createRecord('EMptY')),
        'Unknown record type.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord('TeXt', test_text_data)),
        'Unknown record type.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord('uRL', test_url_data)),
        'Unknown record type.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord('Mime', test_buffer_data)),
        'Unknown record type.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord('sMart-PosTER', test_url_data)),
        'Unknown record type.');
  }, 'NDEFRecord constructor with record type string being treated as case sensitive');

  test(() => {
    assert_throws(new TypeError, () => new NDEFRecord(createRecord(
        ':xyz', test_buffer_data)), 'The domain should not be empty.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord(
        '[:xyz', test_buffer_data)), '"[" is not a valid FQDN.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord(
        'example.com:', test_buffer_data)), 'The type should not be empty.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord(
        'example.com:xyz~', test_buffer_data)), 'The type should not contain \'~\'.');
    assert_throws(new TypeError, () => new NDEFRecord(createRecord(
        'example.com:xyz/', test_buffer_data)), 'The type should not contain \'/\'.');
  }, 'NDEFRecord constructor with invalid external record type');

</script>